From: "David A. Black" Date: 2008-01-16T07:43:06+09:00 Subject: Re: Why @hash ||= Hash.new in Set#initialize? Hi -- On Wed, 16 Jan 2008, Kornelius Kalnbach wrote: > David A. Black wrote: >> I noticed this: >> >> def initialize(enum = nil, &block) >> @hash ||= Hash.new >> >> in set.rb. I'm wondering why the assignment is ||= rather than just =. >> All the tests pass without the ||. > I don't know...but maybe somebody wants to inherit from set, like this: > > class MySet < Set > def initialize(*args, &block) > @hash = HashWithSpecialFeatures.new > super(*args, &block) > end > end > > In this case, @hash wouldn't be overwritten. I guess it would have that effect, but I've seen lots of instance variable initialization in classes that might be subclassed, and I don't remember ever seeing a conditional one. David -- Training for 2008! Ruby on Rails training by David A. Black/Ruby Power and Light, LLC: * Intro to Rails, New York, NY, February 4-7 2008 * Advancing With Rails, New York, NY, February 11-14 2008 Hosted by Exceed Education. See http://www.rubypal.com for details!