William James
1/20/2007 8:06:00 PM
Rob Biedenharn wrote:
> On Jan 20, 2007, at 1:15 PM, Phrogz wrote:
>
> > Robert Klemme wrote:
> >> Since you're using a Hash the block constructor is the most elegant
> >> solution IMHO:
> >>
> >> def initialize
> >> @foo = Hash.new {|h,k| h[k]=[]}
> >> ...
> >> end
> >
> > To be clear for those unfamiliar with this: any time you ask for the
> > value of a key that doesn't exist, the block will be called (which
> > creates a new empty array). This means you can never see this
> > particular Hash has a key, since:
> > if @foo[ :bar ]
> > will always succeed, and create a (possibly unwanted) array
> > instance in
> > the process.
>
> That's only because you're not checking for the presence of a key
> correctly:
>
> >> myhash = Hash.new { |h,k| h[k] = Array.new }
> => {}
> >> if myhash[:oops]
> >> puts "nothing"
> >> end
> nothing
> => nil
> >> myhash
> => {:oops=>[]}
> >> if myhash.has_key? :missing
I prefer to use include?, since it also works on arrays:
irb(main):006:0> ['foo','bar'].include? 'bar'
=> true