Dave Howell
1/26/2006 9:18:00 PM
On Jan 26, 2006, at 12:59, John Carter wrote:
> OK, so this trick cannot be used everywhere.
>
> Suppose @@memo is a brand spanking new empty Hash object.
>
> What does @@memo["pink"] return?
>
> By default it returns nil.
>
> But you can make it return whatever you damn well feel like.
>
> @@memo = Hash.new{|hash,key| hash[key] = Car.new(key)}
>
> @@memo["pink"]
>
> Will say, oo, I have no "pink" element, I better go off and make one
> then, fortunately I have a block on my "new" so I know how to do that.
>
> ps.
>
> If everybody in your country had a pink car, what would you have?
Ha ha. Except that if everybody in my country had a
Car.create_car("pink") car, then the entire country would have exactly
one car. I can't figure out what it's good for; any example I can think
of in my head is just a complicated way of doing something simple.
Either "well, I'll just keep the object around in the first place," or
"no, I would have to have my very OWN pink car, not be FlexCar-ing it
with other parts of the program"