# Re: what is the ruby way to do this?

**URL:** <https://www.ruby-forum.com/t/re-what-is-the-ruby-way-to-do-this/49626>\
**Category:** Ruby\
**Created:** [November 23, 2005, 12:01am UTC](https://www.ruby-forum.com/t/re-what-is-the-ruby-way-to-do-this/49626 "2005-11-23T00:01:14Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![daniels](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3ab097/32.png) [@daniels](https://www.ruby-forum.com/u/daniels)\
**Post date:** [November 23, 2005, 12:01am UTC](https://www.ruby-forum.com/t/re-what-is-the-ruby-way-to-do-this/49626/1 "2005-11-23T00:01:14Z")

</div>

> thank you. this assumes that the hash’s default value is nil.  
> but this is a good enough assumption for my purposes. i was  
> hoping to avoid creating one-element arrays and still have  
> the simplicity of a single line solution but i guess this  
> cant be done. thanks for your help.

You can always convert it out at the end.

hash = Hash.new{|h,k|h[k]=[]}  
hash[“monkey”] \<\< “banana”  
hash[“monkey”] \<\< “sugar”  
hash[“dog”] \<\< “water”  
hash = hash.inject({}) {|s,x|k,v=\*x; s[k] = (v.size == 1 ? v[0] : v); s}  
=\> {“dog”=\>“water”, “monkey”=\>[“banana”, “sugar”]}  
#####################################################################################  
This email has been scanned by MailMarshal, an email content filter.  
#####################################################################################
