Thanks for the help Robert. Following is a scenario I am trying to implemen=
t:
I want to create File objects based on a hash. The 'fruits' hash will
be created based on user input (yaml file), so I don't know how long
the hash will be beforehand. For each key in this hash I would like to
instantiate a File object with object/var name same as hash key. In
future I may have a subclass of File class say UserFile class. The
UserFile objects will be instantiated in a similar manner as File
object, in addition they will get hash values passed on as variables.

--
neubyr.


On Wed, Apr 20, 2011 at 2:36 AM, Robert Klemme
<shortcutter / googlemail.com> wrote:
> On Wed, Apr 20, 2011 at 8:07 AM, neubyr <neubyr / gmail.com> wrote:
>> How do I assign hash's key as an object name? =A0For example I have a ha=
sh as:
>>
>> =A0fruits =3D {"apple" =3D> ["green", "red"], "melons" =3D> ["water", "m=
usk"] }
>>
>> I would like to create a File or String objects by reading above hash,
>> something like
>>
>> =A0fruits.keys.each {|k| "#{k}" =3D String.new}
>>
>> Any hints or suggestions on how to do this will be really helpful.
>
> There is usually no point in dynamically generating local variable
> names - which is what you are trying to attempt. =A0The reason is that
> your code needs to use them before they are known - otherwise it
> cannot see them. =A0Example:
>
> 09:35:55 ~$ ruby19 b.rb
> name is foo
> attempt 1
> [:name, :e, :b, :foo]
> 123
> attempt 2
> [:name, :e, :b, :foo]
> 123
> attempt 3
> [:name, :e, :b, :foo]
> 123
> 09:35:57 ~$ cat -n b.rb
> =A0 =A0 1
> =A0 =A0 2 =A0name=3D'foo'
> =A0 =A0 3 =A0puts "name is #{name}"
> =A0 =A0 4
> =A0 =A0 5 =A0puts "attempt 1"
> =A0 =A0 6
> =A0 =A0 7
> =A0 =A0 8 =A0begin
> =A0 =A0 9 =A0 =A0eval "#{name}=3D123"
> =A0 =A010 =A0 =A0p local_variables
> =A0 =A011 =A0 =A0eval "puts #{name}"
> =A0 =A012 =A0rescue Exception =3D> e
> =A0 =A013 =A0 =A0puts e
> =A0 =A014 =A0end
> =A0 =A015
> =A0 =A016 =A0puts "attempt 2"
> =A0 =A017
> =A0 =A018 =A0b =3D binding
> =A0 =A019 =A0eval "#{name}=3D123", b
> =A0 =A020 =A0p local_variables
> =A0 =A021 =A0eval "puts #{name}", b
> =A0 =A022
> =A0 =A023 =A0puts "attempt 3"
> =A0 =A024
> =A0 =A025 =A0foo=3Dnil
> =A0 =A026 =A0eval "#{name}=3D123"
> =A0 =A027 =A0p local_variables
> =A0 =A028 =A0eval "puts #{name}"
> 09:36:05 ~$
>
> Note there are some special cases where it can make sense. =A0These
> typically involve meta programming (e.g. ERB does it).
>
> What are you trying to accomplish?
>
> Kind regards
>
> robert
>
> --
> remember.guy do |as, often| as.you_can - without end
> http://blog.rubybestpractices.com/
>
>