Setting an instance variable

Silly question:

If I have a string `str’, is there a way to set an instance variable with
that name other than evaling a string? For example:

str = 'var_name’
val = [‘string in an array’]

eval “@#{str} = #{val}” # Is there another way to do this?

p @var_name # --> [‘string in an array’]

Just curious if it’s possible,

Chris

Hi,

If I have a string `str’, is there a way to set an instance variable with
that name other than evaling a string? For example:

No methods like Module#const_set for instance variables.

str = ‘var_name’
val = [‘string in an array’]

eval “@#{str} = #{val}” # Is there another way to do this?

Using `val’ directly instead of #{val} would be better.

···

At Fri, 7 Feb 2003 05:14:35 +0900, Chris Pine wrote:


Nobu Nakada