I"m reading in a YAML file using the yaml library in Ruby. My YAML
looks something like this:
config:
value1: Something here
value2: Something else
As I loop from the YAML like:
config["config"].each { |key, value| }
How could I set the key as an instance variable with the value of the
value of the key... resulting in:
@value1 = "Something here"
@value2 = "Something else"
Any thoughts?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Adam Cohen
2007-Jul-25 18:05 UTC
Re: Creating instance variables while looping over a hash
config["config"].each {|key,value|
instance_variable_set("@#{key}", value)};
you''ll probably want to remove spaces and convert odd chars in the key
to make sure it follows instance variable naming guidelines.
Adam
On 7/25/07, blaine <jangchub-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:>
> I"m reading in a YAML file using the yaml library in Ruby. My YAML
> looks something like this:
>
> config:
> value1: Something here
> value2: Something else
>
> As I loop from the YAML like:
>
> config["config"].each { |key, value| }
>
> How could I set the key as an instance variable with the value of the
> value of the key... resulting in:
>
> @value1 = "Something here"
> @value2 = "Something else"
>
> Any thoughts?
>
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Thank you so much Adam. Tim On Jul 25, 2:05 pm, "Adam Cohen" <bionicboo...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> config["config"].each {|key,value| instance_variable_set("@#{key}", value)}; > > you''ll probably want to remove spaces and convert odd chars in the key > to make sure it follows instance variable naming guidelines. > > Adam > > On 7/25/07, blaine <jangc...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > I"m reading in a YAML file using the yaml library in Ruby. My YAML > > looks something like this: > > > config: > > value1: Something here > > value2: Something else > > > As I loop from the YAML like: > > > config["config"].each { |key, value| } > > > How could I set the key as an instance variable with the value of the > > value of the key... resulting in: > > > @value1 = "Something here" > > @value2 = "Something else" > > > Any thoughts?--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---