You are not logged in.
Pages: 1
So i am following: http://www.ruby-lang.org/en/documentation/quickstart/2/
And have created the class "Greeter", and am trying to call several things back from that class.
Program code i am executing (section of it), full code @ http://pastebin.ca/668279
class Greeter
def initialize(name = "World")
@name = name
end
def say_hi
puts "Hi #{name}!"
end
def say_bye
puts"Bye #{name}, come back soony!"
end
end
g = Greeter.new("Pat")
g.say_hi
g.say_bye
The execution and result I get:
crooksey >> 07:48 PM ~/ruby $ ruby basic.rb
Hello Worldy!
Hello luke
Hello World!
basic.rb:25:in `say_hi': undefined local variable or method `name' for #<Greeter:0xb7d16ebc @name="Pat"> (NameError)
from basic.rb:34
I am just confused on why I am getting this error, i also comment my code as a write it, so when I look back it easily jogs my memory
Arch Linux since 2006
Python Web Developer + Sys Admin (Gentoo/BSD)
Offline
So i am following: http://www.ruby-lang.org/en/documentation/quickstart/2/
And have created the class "Greeter", and am trying to call several things back from that class.
Program code i am executing (section of it), full code @ http://pastebin.ca/668279
class Greeter def initialize(name = "World") @name = name end def say_hi puts "Hi #{name}!" end def say_bye puts"Bye #{name}, come back soony!" end end g = Greeter.new("Pat") g.say_hi g.say_bye
The execution and result I get:
crooksey >> 07:48 PM ~/ruby $ ruby basic.rb Hello Worldy! Hello luke Hello World! basic.rb:25:in `say_hi': undefined local variable or method `name' for #<Greeter:0xb7d16ebc @name="Pat"> (NameError) from basic.rb:34
I am just confused on why I am getting this error, i also comment my code as a write it, so when I look back it easily jogs my memory
Change your #{name} to #{@name}
Last edited by SleepingGiant08 (2007-08-23 20:56:18)
Registered Linux User #439761
Offline
Thanks, really appreciated
Arch Linux since 2006
Python Web Developer + Sys Admin (Gentoo/BSD)
Offline
Thanks, really appreciated
No problem, I'm still learning Ruby... and Rails too
Registered Linux User #439761
Offline
Pages: 1