Your code does create three instances of Cat, but they're thrown away
immediately, since you don't use them. Array#each always return the receiver
(in your case, the array ["Felix", "Garfield", "Heathcliff"]), regardless of
the return value of the block. If you want to obtain an array with the three
instances of Cat, use Array#map instead:
cats = toons.map{|n| Cat.new(name)}
I hope this helps
Stefano
···
On Wednesday 30 April 2008, Frisco Del Rosario wrote:
I'm curious as to why the following does not create three instances of
class Cat:
class Cat
def initialize(name) @name = name
end
end
Your code does create three instances of Cat, but they're thrown away
immediately, since you don't use them. Array#each always return the receiver
(in your case, the array ["Felix", "Garfield", "Heathcliff"]), regardless of
the return value of the block. If you want to obtain an array with the three
instances of Cat, use Array#map instead:
cats = toons.map{|n| Cat.new(name)}
Make that Cat.new(n)
David
--
Rails training from David A. Black and Ruby Power and Light:
INTRO TO RAILS June 9-12 Berlin
ADVANCING WITH RAILS June 16-19 Berlin
INTRO TO RAILS June 24-27 London (Skills Matter)
See http://www.rubypal.com for details and updates!
You watch a talk show recently? They're doing one next month on a
normal, happy heterosexual couple, assuming they can find one.
~ -- Professor Ralph Noble, RPI, Psychology of Motivation, Fall 1991