# Method problem

**URL:** <https://www.ruby-forum.com/t/method-problem/122661>\
**Category:** Ruby\
**Created:** [December 10, 2007, 2:22pm UTC](https://www.ruby-forum.com/t/method-problem/122661 "2007-12-10T14:22:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d9b06d/32.png) [@johnsmith](https://www.ruby-forum.com/u/johnsmith)\
**Post date:** [December 10, 2007, 2:22pm UTC](https://www.ruby-forum.com/t/method-problem/122661/1 "2007-12-10T14:22:24Z")

</div>

Hi there

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

many thanks

code:  
class Reference

@data

add\_field(field, value)  
if key already exits  
push value onto it  
else  
@data[field]=value  
end

get(field)  
@data[field]  
end  
end

---

<div class="post-metadata">

**Author:** ![johnsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d9b06d/32.png) [@johnsmith](https://www.ruby-forum.com/u/johnsmith)\
**Post date:** [December 10, 2007, 3:12pm UTC](https://www.ruby-forum.com/t/method-problem/122661/2 "2007-12-10T15:12:14Z")

</div>

2007/12/10, Johnathan S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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

---

<div class="post-metadata">

**Author:** ![johnsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d9b06d/32.png) [@johnsmith](https://www.ruby-forum.com/u/johnsmith)\
**Post date:** [December 10, 2007, 5:01pm UTC](https://www.ruby-forum.com/t/method-problem/122661/3 "2007-12-10T17:01:18Z")

</div>

hi and thanks for your help its much appreciated

i would really prefer if i got this working inside the class however.  
are you able to assist?

thanks

---

<div class="post-metadata">

**Author:** ![johnsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d9b06d/32.png) [@johnsmith](https://www.ruby-forum.com/u/johnsmith)\
**Post date:** [December 12, 2007, 7:12pm UTC](https://www.ruby-forum.com/t/method-problem/122661/4 "2007-12-12T19:12:16Z")

</div>

On 10.12.2007 17:01, Johnathan S. wrote:

> hi and thanks for your help its much appreciated
> 
> i would really prefer if i got this working inside the class however.  
> are you able to assist?

What do you mean? The solution works inside and outside of classes.

robert

---

<div class="post-metadata">

**Author:** ![johnsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d9b06d/32.png) [@johnsmith](https://www.ruby-forum.com/u/johnsmith)\
**Post date:** [December 12, 2007, 10:05am UTC](https://www.ruby-forum.com/t/method-problem/122661/5 "2007-12-12T10:05:55Z")

</div>

Johnathan S. wrote:

> hi and thanks for your help its much appreciated
> 
> i would really prefer if i got this working inside the class however.  
> are you able to assist?
> 
> thanks

class Foo  
def initialize  
@authors = Hash.new {|h,k| h[k] = []}  
end  
def add(field, value)  
if @authors.has\_key?(field)  
@authors[field] \<\< value  
else  
@authors[field] = value  
end  
end  
def   
@authors[field]  
end  
end

Perhaps something like that?

I just quickly typed that up whilst trying to work and talk to my boss,  
so my apologies if it doesn’t work or there are spelling mistakes

Regards,  
Lee
