From: Matthew Smillie Date: 2006-01-04T00:06:15+09:00 Subject: Re: More on the fundamentals... On Jan 3, 2006, at 14:51, John Maclean wrote: > Please have a look at the comments below to see that I'm > understanding things correctly; You've made a couple of fundamental mistakes regarding how methods are defined. The basic schema is: def method_name(parameter_name) Where you can have a whole list of parameters, or none at all. See the revised (and very verbose comments. Hope this helps. matt smillie. Also, I've changed the indented to two spaces, since that's the Ruby norm. class Greeter # open class Greeter (it's not necessarily *new*) def initialize(name) # defining the initialise method, which is called when a new # object is created. The method takes one parameter, 'name'. # Example (such as: foo = Greeter.new("Mark"). @name = name # define an instance variable called '@name' and assign it # the value of the parameter 'name' end def say(phrase) # defining a new method called 'say' with a parameter # called 'phrase'. puts "#{phrase}, #{@name}" # The double quotes define a string, and the #{} sections are # used for interpolation (substitution). end end > > #!/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 > @name = name > # we define a new @instance variable called name > end > def say(phrase) > # creating a new method called phrase... > puts "#{phrase}, #{@name}" > # ... which uses doube quotes to substitute > end > end > > # 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 > # above > g1.say("Hello") > g2.say("Wotcha") > g3.say("Ire!") > g4.say("Elake") > > -- > John Maclean > MSc (DIC) > 07739 171 531 > >