From: Yohanes Santoso Date: 2006-01-04T11:14:56+09:00 Subject: Re: More on the fundamentals... John Maclean writes: > In this line of code can anyone tell me why "food" is a method? I > thought that they are preceded by a dot. It is not a method, it's a variable, very possibly a block-local one. > > __ an array __ the method __ parameters for code block > / / / > [cornflakes, yam, rice].each { |food| eat food} > / \ > code block __/ \__block args > ^^^ is parsed as: [corflakes, yam, rice].each {|food| eat(food)} YS. > > On Tue, 3 Jan 2006 23:51:15 +0900 > 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 >> @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