Ruby Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesRuby Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 18th, 2007, 10:01 PM
lotus_anima lotus_anima is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2005
Posts: 10 lotus_anima User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 14 m 23 sec
Reputation Power: 0
ERB "undefined local variable or method"

Here is a snippet of my Ruby...

Code:
  def serve_html(sock)
    browser = sock
    
    template = ERB.new(load_template_as_string('test.rhtml'), 0, '%<>')
    send_this = "Hello, world!"
    
    
    browser.write("HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n")
    browser.write(template.result)
    
    close_socket(sock)
  end


And here is a snippet from test.rhtml...
Code:
<body><%= send_this %></body>


But I get...
Code:
(erb):10: undefined local variable or method `send_this' for main:Object (NameError)


What gives? Mine is nearly identical to the ERB docs.

Reply With Quote
  #2  
Old May 27th, 2007, 09:13 AM
voipfc voipfc is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2006
Posts: 19 voipfc User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 49 m 40 sec
Reputation Power: 0
listing
PHP Code:
require 'erb'

  
def serve_html(file)

    
send_this "Hello world 2"
    
data File.read(file)
    
template ERB.new(data0'%<>')
    
send_this "Hello, world 3!"

    
puts send_this
    puts
("HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n")
    
puts(template.result)


  
end
send_this
="Hello world 1!"
serve_html('test.rhtml'


execution

Quote:
Hello, world 3!
HTTP/1.0 200 OK
Content-type: text/html

<body>Hello world 1!</body>

Comment out the send_this="Hello world 1" at the end of the listing and the error will appear. It appears that when the puts is xecuted template.result is out of scope.

It has something to do with bindings - I am new to ruby - don't ask me why.

This is what I used to get your code to work. It involves something known as bindings and is in the ERb examples
PHP Code:
require 'erb'

  
def serve_html(file)
    
binding
    send_this 
"Hello world"
    
data File.read(file)
    
template ERB.new(data0'%<>')

    
puts("HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n")
    
puts(template.result(b))


  
end
serve_html
('test.rhtml'

Note the bracketed (b) in the template.result call.

Reply With Quote
  #3  
Old July 13th, 2011, 10:02 AM
rubyist rubyist is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2011
Posts: 1 rubyist User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 m 39 sec
Reputation Power: 0
Instance variable

Even if this thread isn't that new, it may help some other users:
Ruby separates between local and instance variables by the given name. Local variables start with a letter or often with a '_' and can be addressed only within the class. Instance variables start with the '@'-symbol and can be addressed from outside the class. So try it with an '@send_this' (both in the controller and in the view) and it should work.

Reply With Quote
  #4  
Old July 13th, 2011, 10:55 AM
IMP1 IMP1 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2011
Posts: 5 IMP1 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 40 m 45 sec
Reputation Power: 0
Quote:
Originally Posted by rubyist
Even if this thread isn't that new, it may help some other users:
Ruby separates between local and instance variables by the given name. Local variables start with a letter or often with a '_' and can be addressed only within the class. Instance variables start with the '@'-symbol and can be addressed from outside the class. So try it with an '@send_this' (both in the controller and in the view) and it should work.
When rubyist says "class", he means "method or block". Instance variable are available throughout the class.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesRuby Programming > ERB "undefined local variable or method"

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap