How to put hash values in function

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

On Feb 7, 2008 2:23 AM, shawn bright [email protected] 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

On Feb 7, 2008 4:08 AM, Jano S. [email protected] 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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs