[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

how to put hash values in function

shawn bright

2/7/2008 1:23:00 AM

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

3 Answers

Jano Svitok

2/7/2008 3:09:00 AM

0

On Feb 7, 2008 2:23 AM, shawn bright <nephish@gmail.com> 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

> thanks
>
> sk
>
>

Jano Svitok

2/7/2008 3:10:00 AM

0

On Feb 7, 2008 4:08 AM, Jano Svitok <jan.svitok@gmail.com> wrote:
> On Feb 7, 2008 2:23 AM, shawn bright <nephish@gmail.com> 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

def do_something(*args)
p args
end

(I've hit enter too soon...)

Jano

shawn bright

2/7/2008 3:23:00 AM

0

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

On Feb 6, 2008 9:09 PM, Jano Svitok <jan.svitok@gmail.com> wrote:
>
> On Feb 7, 2008 4:08 AM, Jano Svitok <jan.svitok@gmail.com> wrote:
> > On Feb 7, 2008 2:23 AM, shawn bright <nephish@gmail.com> 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
>
> def do_something(*args)
> p args
> end
>
> (I've hit enter too soon...)
>
> Jano
>
>