[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

hash key to var name of value hash key value

pere.noel

8/10/2006 6:36:00 AM


i'd like to now if their is a simpler way to do that :

IN_NEWS=@h['IN_NEWS']

because here i do have to know their is a key of name "IN_NEWS"

want i want is to affet the value of all the @h['key_name'] to the
variable key_name ???

(as far as i remember well it's possible in php then i think this is
also possible in ruby ))))
--
une bévue
5 Answers

Alex Young

8/10/2006 7:12:00 AM

0

Une bévue wrote:
> i'd like to now if their is a simpler way to do that :
>
> IN_NEWS=@h['IN_NEWS']
>
> because here i do have to know their is a key of name "IN_NEWS"
>
> want i want is to affet the value of all the @h['key_name'] to the
> variable key_name ???
>
> (as far as i remember well it's possible in php then i think this is
> also possible in ruby ))))
Thinking out loud...

hash.each_key{|k|
eval("#{k}=hash[#{k}]")
}

There may be a better way, though...

--
Alex

Martin DeMello

8/10/2006 8:11:00 AM

0

On 8/10/06, Alex Young <alex@blackkettle.org> wrote:
>
> hash.each_key{|k|
> eval("#{k}=hash[#{k}]")
> }

hash.each_pair {|k,v|
Object.const_set(k, v)
}

martin

pere.noel

8/10/2006 11:10:00 AM

0

Martin DeMello <martindemello@gmail.com> wrote:

>
> hash.each_pair {|k,v|
> Object.const_set(k, v)
> }

fine thanxs to both of them !!!
--
une bévue

Ara.T.Howard

8/10/2006 2:02:00 PM

0

pere.noel

8/10/2006 4:06:00 PM

0

<ara.t.howard@noaa.gov> wrote:

>
> define_method('key_name'){ @h['key_name' }

ok fine, may it's the best ?
--
une bévue