# How to put hash values in function

**URL:** <https://www.ruby-forum.com/t/how-to-put-hash-values-in-function/128362>\
**Category:** Ruby\
**Created:** [February 7, 2008, 2:24am UTC](https://www.ruby-forum.com/t/how-to-put-hash-values-in-function/128362 "2008-02-07T02:24:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [February 7, 2008, 2:24am UTC](https://www.ruby-forum.com/t/how-to-put-hash-values-in-function/128362/1 "2008-02-07T02:24:04Z")

</div>

Hey there,

i have an example function from documentation.

it looks like this

do\_something(SomeValue, 2 =\> ‘value’, 3 =\> ‘value’, 4 =\> ‘value’,  
someVar, someOtherVar)

now if i have a hash that has more or less values than the 3 shown  
above, how could i pass those to the function where the index =\> value  
is placed into the function ?

exp:  
if i have myHash = {2 =\> ‘value’, 3 =\> ‘value’, 4 =\> ‘value’}

how could i put these values in the array ?

thanks

sk

---

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [February 7, 2008, 4:09am UTC](https://www.ruby-forum.com/t/how-to-put-hash-values-in-function/128362/2 "2008-02-07T04:09:36Z")

</div>

On Feb 7, 2008 2:23 AM, shawn bright [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hey there,
> 
> i have an example function from documentation.
> 
> it looks like this
> 
> do\_something(SomeValue, 2 =\> ‘value’, 3 =\> ‘value’, 4 =\> ‘value’,  
> someVar, someOtherVar)

This doesn’t work in ruby 1.8 - the hash arguments must be the last  
ones (maybe there’s an exception, but I don’t remember now).

> now if i have a hash that has more or less values than the 3 shown  
> above, how could i pass those to the function where the index =\> value  
> is placed into the function ?
> 
> exp:  
> if i have myHash = {2 =\> ‘value’, 3 =\> ‘value’, 4 =\> ‘value’}
> 
> how could i put these values in the array ?

If you call do\_something(SomeValue, myHash) it’s the same as  
do\_something(SomeValue, 2 =\> ‘value’, 3 =\> ‘value’, 4 =\> ‘value’)

you can check it yourself using

---

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [February 7, 2008, 4:10am UTC](https://www.ruby-forum.com/t/how-to-put-hash-values-in-function/128362/3 "2008-02-07T04:10:46Z")

</div>

On Feb 7, 2008 4:08 AM, Jano S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> This doesn’t work in ruby 1.8 - the hash arguments must be the last  
> If you call do\_something(SomeValue, myHash) it’s the same as  
> do\_something(SomeValue, 2 =\> ‘value’, 3 =\> ‘value’, 4 =\> ‘value’)
> 
> you can check it yourself using

def do\_something(\*args)  
p args  
end

(I’ve hit enter too soon…)

Jano

---

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [February 7, 2008, 4:24am UTC](https://www.ruby-forum.com/t/how-to-put-hash-values-in-function/128362/4 "2008-02-07T04:24:15Z")

</div>

wow, thanks, very handy, and it did work.  
i thought that i had tried this earlier, but i guess i had something  
else wrong earlier.

great info, thanks Jano.

sk
