2007/12/10, Johnathan Smith <stu_09 / hotmail.com>:
> im writing a class which produces refrences in a hash
> however im trying to deal with multiple authors
> so i want to write an if statement where
>
> if the key already exists
>   push value onto it
>
> im unsure of how to do this so any help would be much appreciated

The easiest and probably most appropriate idiom is to use the block
form of Hash.new:

irb(main):001:0> authors = Hash.new {|h,k| h[k] = []}
=> {}
irb(main):002:0> authors[:foo] << "bar"
=> ["bar"]
irb(main):003:0> authors[:foo] << "baz"
=> ["bar", "baz"]
irb(main):004:0> authors[:bar] << "foo"
=> ["foo"]
irb(main):005:0> authors
=> {:foo=>["bar", "baz"], :bar=>["foo"]}

Note, this will make *all* Hash values Arrays but this is easier to
deal with anyway.  Otherwise you would have to write code that deals
with single and multiple values in every bit of code that uses this.

Kind regards

robert

-- 
use.inject do |as, often| as.you_can - without end