tags:

views:

75

answers:

3

Is the following possible in any way? I keep running into a odd number list for Hash

def thores_hammer(bling)
  hammer_bling = { bling }
end

thores_hammer :rubys    => 5,
              :emeralds => 5,
              :souls    => 333

Thanks ahead of time.

+1  A: 

Try this:

def thores_hammer(bling)
  hammer_bling = bling
end

thores_hammer Hash[:rubys => 5, :emeralds => 5, :souls => 333]
Petros
+3  A: 

The reason you're running into an error is that the Hash is implicitly created when the thores_hammer method is invoked - so when you do { bling } you're creating a hash with only one key (which is itself a hash) and no value. Thus the error.

All you need to do is drop the curly braces:

irb> def thores_hammer(bling)
       hammer_bling = bling
     end
#=> nil
irb> thores_hammer :rubys    => 5,
                   :emeralds => 5,
                   :souls    => 333
#=> {:rubys=>5, :emeralds=>5, :souls=>333}
rampion
Magic! Twice you have saved me
rubynewbie
I think im going to spend more time in irb
rubynewbie
+2  A: 

What you may be intending to do is make a copy of the Hash which could be done as:

def thores_hammer(bling)
  hammer_bling = bling.dup
end

It might be a good idea to make a copy if you're intending to use the Hash for a long period of time and aren't sure if modifying the copy the method was given is a good idea because it could be used in other places.

Also, there are several different uses for curly braces within Ruby such as declaring blocks.

tadman