I am new to ruby, and have a simple question.
In the tutorial, I came across
class AddressBook
def each
@persons.each { |p| yield p }
end
end
defining the method each on the Class AddressBook (an array of hashes;
the addressbook is an array of persons, each person is a hash;
his name, address, etc.)
Question 1.
Is this the same as the following?
class AddressBook
def each
@persons.each
do |p|
yield p
end
end
end
Question 2. Is my terminology correct? If not, is the meaning
clear enough?
Van
class AddressBook
def each
@persons.each { |p| yield p }
end
end
defining the method each on the Class AddressBook (an array of hashes;
the addressbook is an array of persons, each person is a hash;
his name, address, etc.)
Actually, an instance of the AddressBook class contains an array of persons
(in the @persons instance variable); it is not actually an array (it could be,
if it was declared “class AddressBook < Array”, and used “self” instead of
“@persons”).
Question 1.
Is this the same as the following?
class AddressBook
def each
@persons.each
do |p|
yield p
end
end
end
Almost. It’s true that {…} and do…end are synonyms (although
they differ in precedence). But do, like {, has to be on the same
line as the method call; @persons.each on a line by itself is a syntax error.
Question 2. Is my terminology correct? If not, is the meaning
clear enough?
Your terminology is fine!
-Mark
···
On Tue, Dec 09, 2003 at 06:47:38PM -0800, Van Jacques wrote: