Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > hash key to var name of value hash key value

Reply
Thread Tools

hash key to var name of value hash key value

 
 
Une bévue
Guest
Posts: n/a
 
      08-10-2006

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
 
Reply With Quote
 
 
 
 
Alex Young
Guest
Posts: n/a
 
      08-10-2006
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

 
Reply With Quote
 
 
 
 
Martin DeMello
Guest
Posts: n/a
 
      08-10-2006
On 8/10/06, Alex Young <(E-Mail Removed)> wrote:
>
> hash.each_key{|k|
> eval("#{k}=hash[#{k}]")
> }


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

martin

 
Reply With Quote
 
Une bévue
Guest
Posts: n/a
 
      08-10-2006
Martin DeMello <(E-Mail Removed)> wrote:

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


fine thanxs to both of them !!!
--
une bévue
 
Reply With Quote
 
ara.t.howard@noaa.gov
Guest
Posts: n/a
 
      08-10-2006
On Thu, 10 Aug 2006, [ISO-8859-1] Une b=E9vue wrote:

>
> i'd like to now if their is a simpler way to do that :
>
> IN_NEWS=3D@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 ))))



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

-a
--=20
to foster inner awareness, introspection, and reasoning is more efficient t=
han
meditation and prayer.
- h.h. the 14th dali lama

 
Reply With Quote
 
Une bévue
Guest
Posts: n/a
 
      08-10-2006
<(E-Mail Removed)> wrote:

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


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

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Difference between Session["var"] and Session("var") thomson ASP .Net 10 06-20-2005 01:02 PM
how access to hash which contains hash and sclar var? kenneth Perl Misc 1 12-11-2004 03:30 PM
Threads.. Session var lost, App var ok Alvin Bruney ASP .Net 1 12-02-2003 01:56 AM
sort multi-key hash by value and print out with key value pairs Antonio Quinonez Perl Misc 2 08-14-2003 10:56 PM



Advertisments