From: dblack@... Date: 2006-01-04T00:02:04+09:00 Subject: Re: More on the fundamentals... Hi -- On Tue, 3 Jan 2006, John Maclean wrote: > Please have a look at the comments below to see that I'm > understanding things correctly; > > #!/usr/bin/ruby > #Tue Jan 3 14:04:28 GMT 2006 > class Greeter > # a new class cllaed Greeter > def initialize(name) > # creating a new method called name There's no method called name. > @name = name > # we define a new @instance variable called name Move the @ from @instance to name and you're set :-) > end > def say(phrase) > # creating a new method called phrase... There's no method called phrase. > puts "#{phrase}, #{@name}" > # ... which uses doube quotes to substitute > end > end It seems that you're confusing instance variable assignment with method creation. They're unrelated. You can, of course, write a method that returns the current value of an instance variable: def name @name end and you can even get Ruby to do it semi-automatically for you: attr_reader :name # creates the above "name" method But @name = name is just a variable assignment. > # say hello > g1 = Greeter.new("jayeola") > g2 = Greeter.new("buddy") > g3 = Greeter.new("vimmer") > g4 = Greeter.new("slammer") > # we have just created four new (object) instances belonging to the class > # Greeter. They can now use the instance variables that have been created I would say "instances of the class Greeter". > # above > g1.say("Hello") > g2.say("Wotcha") > g3.say("Ire!") > g4.say("Elake") Have another look at the attr_* family of methods, and keep in mind that they're essentially shortcuts for writing your own get-and/or-set methods (using instance variables), and I think that part of it will fall into place. David -- David A. Black dblack@wobblini.net "Ruby for Rails", from Manning Publications, coming April 2006! http://www.manning.com/books/black