# Dynamically assigning instance variables (redux)

**URL:** <https://rubytalk.org/t/dynamically-assigning-instance-variables-redux/31994>\
**Category:** ruby-talk\
**Created:** [17 October 2006 02:35 UTC](https://rubytalk.org/t/dynamically-assigning-instance-variables-redux/31994 "2006-10-17T02:35:12Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![BIl\_Kleb1](https://avatars.discourse-cdn.com/v4/letter/b/a587f6/32.png) [@BIl\_Kleb1](https://rubytalk.org/u/BIl_Kleb1)\
**Post date:** [17 October 2006 02:35 UTC](https://rubytalk.org/t/dynamically-assigning-instance-variables-redux/31994/1 "2006-10-17T02:35:12Z")

</div>

I need a modern answer to Daniel Berger's 2002 post,

&nbsp;&nbsp;[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/39534](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/39534)

&nbsp;&nbsp;Is there a way to dynamically assign instance variables?

&nbsp;&nbsp;class SomeClass  
&nbsp;&nbsp;&nbsp;&nbsp;def initialize(hash)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hash.each do |key,val|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@#{key} = val # Doesn't work  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end

Actually, I'd like the assignment and make them attr\_readers too.

Nearly meta-programming clueless,

> **···**
>
> --  
> Bil Kleb  
> [http://fun3d.larc.nasa.gov](http://fun3d.larc.nasa.gov)

---

<div class="post-metadata">

**Author:** ![Wilson\_Bilkovich](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/wilson_bilkovich/32/8247_2.png) [@Wilson\_Bilkovich](https://rubytalk.org/u/Wilson_Bilkovich)\
**Post date:** [17 October 2006 02:42 UTC](https://rubytalk.org/t/dynamically-assigning-instance-variables-redux/31994/2 "2006-10-17T02:42:44Z")

</div>

Here's one way:  
[http://rubyquiz.com/quiz67.html](http://rubyquiz.com/quiz67.html)

If you just want to set instance variables, you can do:  
def initialize(has)  
&nbsp;&nbsp;hash.each do |key,val|  
&nbsp;&nbsp;&nbsp;&nbsp;instance\_variable\_set "@#{key}", val  
&nbsp;&nbsp;end  
end

> **···**
>
> On 10/16/06, Bil Kleb \<Bil.Kleb@nasa.gov\> wrote:
> 
> > I need a modern answer to Daniel Berger's 2002 post,
> > 
> > &nbsp;&nbsp;[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/39534](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/39534)
> > 
> > &nbsp;&nbsp;Is there a way to dynamically assign instance variables?
> > 
> > &nbsp;&nbsp;class SomeClass  
> > &nbsp;&nbsp;&nbsp;&nbsp;def initialize(hash)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hash.each do |key,val|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@#{key} = val # Doesn't work  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;end

---

<div class="post-metadata">

**Author:** ![Jeremy\_Tregunna](https://avatars.discourse-cdn.com/v4/letter/j/e99b99/32.png) [@Jeremy\_Tregunna](https://rubytalk.org/u/Jeremy_Tregunna)\
**Post date:** [17 October 2006 02:45 UTC](https://rubytalk.org/t/dynamically-assigning-instance-variables-redux/31994/3 "2006-10-17T02:45:20Z")

</div>

> Is there a way to dynamically assign instance variables?
> 
> class SomeClass  
> &nbsp;&nbsp;&nbsp;def initialize(hash)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hash.each do |key,val|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@#{key} = val # Doesn't work  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;end  
> end

Replace:

@#{key} = val

with:

self.instance\_variable\_set(:@"#{key}", val)

> Actually, I'd like the assignment and make them attr\_readers too.
> 
> Nearly meta-programming clueless,

Not sure on this one, havn't gone too deep into metaprogramming in ruby.

> **···**
>
> On 06-10-16, at 22:35, Bil Kleb wrote:
> 
> --  
> Jeremy Tregunna  
> jtregunna@blurgle.ca

---

<div class="post-metadata">

**Author:** ![Gavin\_Kistner2](https://avatars.discourse-cdn.com/v4/letter/g/b9e5f3/32.png) [@Gavin\_Kistner2](https://rubytalk.org/u/Gavin_Kistner2)\
**Post date:** [17 October 2006 03:45 UTC](https://rubytalk.org/t/dynamically-assigning-instance-variables-redux/31994/4 "2006-10-17T03:45:10Z")

</div>

Bil Kleb wrote:

> &nbsp;&nbsp;Is there a way to dynamically assign instance variables?

[...snip...]

> Actually, I'd like the assignment and make them attr\_readers too.

class Foo  
&nbsp;&nbsp;def initialize( hash={} )  
&nbsp;&nbsp;&nbsp;&nbsp;hash.each{ |key,value|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var\_name = "@#{key}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instance\_variable\_set( var\_name, value )  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;singleton\_class = class \<\< self; self; end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;singleton\_class.class\_eval{  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;define\_method( key ){  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instance\_variable\_get( var\_name )  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;define\_method( "#{key}=" ){ |new\_value|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instance\_variable\_set( var\_name, new\_value )  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;end  
end

f = Foo.new :name=\>'whee', :age=\>12  
p f, f.age  
#=\> #\<Foo:0x32b900 @age=12, @name="whee"\>  
#=\> 12  
f.age=32  
p f  
#=\> #\<Foo:0x32b900 @age=32, @name="whee"\>

---

<div class="post-metadata">

**Author:** ![Gavin\_Kistner2](https://avatars.discourse-cdn.com/v4/letter/g/b9e5f3/32.png) [@Gavin\_Kistner2](https://rubytalk.org/u/Gavin_Kistner2)\
**Post date:** [17 October 2006 04:30 UTC](https://rubytalk.org/t/dynamically-assigning-instance-variables-redux/31994/5 "2006-10-17T04:30:11Z")

</div>

Phrogz wrote:

> class Foo  
> &nbsp;&nbsp;def initialize( hash={} )  
> &nbsp;&nbsp;&nbsp;&nbsp;hash.each{ |key,value|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var\_name = "@#{key}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instance\_variable\_set( var\_name, value )  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;singleton\_class = class \<\< self; self; end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;singleton\_class.class\_eval{  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;define\_method( key ){  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instance\_variable\_get( var\_name )  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;define\_method( "#{key}=" ){ |new\_value|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instance\_variable\_set( var\_name, new\_value )  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;end  
> end

Note that the above defines distinct readers/writers for each instance:

&nbsp;&nbsp;f1 = Foo.new :name=\>'whee', :age=\>12  
&nbsp;&nbsp;f2 = Foo.new :chicken=\>'gorkbo'  
&nbsp;&nbsp;p f1, f2  
&nbsp;&nbsp;#=\> #\<Foo:0x32b900 @age=12, @name="whee"\>  
&nbsp;&nbsp;#=\> #\<Foo:0x32b360 @chicken="gorkbo"\>

&nbsp;&nbsp;p f1.name  
&nbsp;&nbsp;#=\> "whee"

&nbsp;&nbsp;p f2.name  
&nbsp;&nbsp;#=\> NoMethodError: undefined method `name' for #\<Foo:0x32b34c  
@chicken="gorkbo"\>  
&nbsp;&nbsp;#=\> at top level in tmp.rb at line 26

You can also open up the main class (instead of the singleton class) to  
define the methods shared by all instances, or store the hash as an  
instance variable and use method\_missing to dynamically catch and  
report values as appropriate.

Here's an example of that for Hash objects in general:

class Hash  
&nbsp;&nbsp;# Causes \*all\* Hash objects to behave similar to Object literals in  
JS,  
&nbsp;&nbsp;# with their keys accessible via dot notation.  
&nbsp;&nbsp;# foo = { 'name'=\>'Gavin', 'age'=\>33 }  
&nbsp;&nbsp;# foo.weight = 180  
&nbsp;&nbsp;# p foo.name, foo.weight  
&nbsp;&nbsp;# #=\> "Gavin"  
&nbsp;&nbsp;# #=\> 180

> **···**
>
> #  
> &nbsp;&nbsp;# p foo.zzzz  
> &nbsp;&nbsp;# #=\> NoMethodError: undefined method `zzzz' for  
> &nbsp;&nbsp;# #=\> {"name"=\>"Gavin", "weight"=\>180, "age"=\>33}:Hash  
> &nbsp;&nbsp;alias\_method :\_\_mMm\_\_, :method\_missing  
> &nbsp;&nbsp;def method\_missing(meth,\*args)  
> &nbsp;&nbsp;&nbsp;&nbsp;if /=$/ =~ (meth=meth.id2name)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Name ends with an equals sign; shove value to the hash  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self[meth[0...-1] ] = (args.length\<2 ? args[0] : args)  
> &nbsp;&nbsp;&nbsp;&nbsp;else  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Retrieve the value  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if self.key?( meth )  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self[meth]  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Let the real method\_missing handle/pass on up  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\_\_mMm\_\_( meth.intern, \*args )  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end  
> end
> 
> TIMTOWTDI.

---

<div class="post-metadata">

**Author:** ![Matt9](https://avatars.discourse-cdn.com/v4/letter/m/ea5d25/32.png) [@Matt9](https://rubytalk.org/u/Matt9)\
**Post date:** [17 October 2006 04:45 UTC](https://rubytalk.org/t/dynamically-assigning-instance-variables-redux/31994/6 "2006-10-17T04:45:05Z")

</div>

Wow, that was way cool. I think we can make it shorter (esp since he  
only wants attr\_readers) and faster (no point generating the singleton  
class every time thru the loop):

class Foo  
&nbsp;&nbsp;def initialize( hash={} )  
&nbsp;&nbsp;&nbsp;&nbsp;singleton\_class = class \<\< self; self; end  
&nbsp;&nbsp;&nbsp;&nbsp;hash.each do |key,value|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instance\_variable\_set( "@" + key, value )  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;singleton\_class.class\_eval { attr\_reader key }  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end

f = Foo.new "name"=\>'whee', "age"=\>12  
p f.age, f.name # =\> works  
f.name = "no, it's a reader" # =\> error, as desired

But pls note that I'm just imitating; the self-returning singleton class  
was totally beyond me! m.

> **···**
>
> Phrogz \<gavin@refinery.com\> wrote:
> 
> > Bil Kleb wrote:  
> > \> Is there a way to dynamically assign instance variables?  
> > [...snip...]  
> > \> Actually, I'd like the assignment and make them attr\_readers too.
> > 
> > class Foo  
> > &nbsp;&nbsp;def initialize( hash={} )  
> > &nbsp;&nbsp;&nbsp;&nbsp;hash.each{ |key,value|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var\_name = "@#{key}"  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instance\_variable\_set( var\_name, value )  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;singleton\_class = class \<\< self; self; end  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;singleton\_class.class\_eval{  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;define\_method( key ){  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instance\_variable\_get( var\_name )  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;define\_method( "#{key}=" ){ |new\_value|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instance\_variable\_set( var\_name, new\_value )  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> > &nbsp;&nbsp;&nbsp;&nbsp;}  
> > &nbsp;&nbsp;end  
> > end
> 
> --  
> matt neuburg, phd = matt@tidbits.com, [Matt Neuburg’s Home Page](http://www.tidbits.com/matt/)  
> Tiger - [http://www.takecontrolbooks.com/tiger-customizing.html](http://www.takecontrolbooks.com/tiger-customizing.html)  
> AppleScript - [http://www.amazon.com/gp/product/0596102119](http://www.amazon.com/gp/product/0596102119)  
> Read TidBITS! It's free and smart. [http://www.tidbits.com](http://www.tidbits.com)

---

<div class="post-metadata">

**Author:** ![Ryan\_Davis1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/ryan_davis1/32/1848_2.png) [@Ryan\_Davis1](https://rubytalk.org/u/Ryan_Davis1)\
**Post date:** [17 October 2006 05:37 UTC](https://rubytalk.org/t/dynamically-assigning-instance-variables-redux/31994/7 "2006-10-17T05:37:24Z")

</div>

> Bil Kleb wrote:
> 
> > &nbsp;&nbsp;Is there a way to dynamically assign instance variables?
> 
> [...snip...]
> 
> > Actually, I'd like the assignment and make them attr\_readers too.
> 
> class Foo  
> &nbsp;&nbsp;def initialize( hash={} )  
> &nbsp;&nbsp;&nbsp;&nbsp;hash.each{ |key,value|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var\_name = "@#{key}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instance\_variable\_set( var\_name, value )  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;singleton\_class = class \<\< self; self; end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;singleton\_class.class\_eval{  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;define\_method( key ){  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instance\_variable\_get( var\_name )  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;define\_method( "#{key}=" ){ |new\_value|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instance\_variable\_set( var\_name, new\_value )  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;end  
> end

I find this much too complex with no real need for using singleton\_classes (ever, really). Try:

class Foo  
&nbsp;&nbsp;&nbsp;def initialize( hash={} )  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hash.each do |ivar, val|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.class.send :attr\_accessor, ivar unless respond\_to? ivar  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;send "#{ivar}=", val  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end  
end

The primary benefit of writing code like this is that you can know what it does in half a glance with no extra thinking. I should also point out that calling a define\_method method is about 2x slower than an attr\_\* method. (not a big deal in this case but take a look at rails' primary\_key and think about that impact)

> **···**
>
> On Oct 16, 2006, at 8:45 PM, Phrogz wrote:
> 
> > f = Foo.new :name=\>'whee', :age=\>12  
> > p f, f.age  
> > #=\> #\<Foo:0x32b900 @age=12, @name="whee"\>  
> > #=\> 12  
> > f.age=32  
> > p f  
> > #=\> #\<Foo:0x32b900 @age=32, @name="whee"\>

---

<div class="post-metadata">

**Author:** ![Gavin\_Kistner2](https://avatars.discourse-cdn.com/v4/letter/g/b9e5f3/32.png) [@Gavin\_Kistner2](https://rubytalk.org/u/Gavin_Kistner2)\
**Post date:** [17 October 2006 13:15 UTC](https://rubytalk.org/t/dynamically-assigning-instance-variables-redux/31994/8 "2006-10-17T13:15:06Z")

</div>

Ryan Davis wrote:

> class Foo  
> &nbsp;&nbsp;&nbsp;def initialize( hash={} )  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hash.each do |ivar, val|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.class.send :attr\_accessor, ivar unless respond\_to? ivar  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;send "#{ivar}=", val  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;end  
> end

This is distinctly different, however, in that it adds the accessor  
methods to the Foo class, not the instances. I have no idea what Mr.  
Kleb wanted, but given that he's adding accessors based on hashes  
passed to each instance, my guess was that he would want a unique set  
per instance.

&nbsp;&nbsp;f1 = Foo.new :name=\>'whee'  
&nbsp;&nbsp;f2 = Foo.new :age=\>12  
&nbsp;&nbsp;p f1.age  
&nbsp;&nbsp;#=\> nil

Is the above result desirable, Bill?

---

<div class="post-metadata">

**Author:** ![Gavin\_Kistner2](https://avatars.discourse-cdn.com/v4/letter/g/b9e5f3/32.png) [@Gavin\_Kistner2](https://rubytalk.org/u/Gavin_Kistner2)\
**Post date:** [17 October 2006 13:15 UTC](https://rubytalk.org/t/dynamically-assigning-instance-variables-redux/31994/9 "2006-10-17T13:15:08Z")

</div>

matt neuburg wrote:

> Wow, that was way cool. I think we can make it shorter (esp since he  
> only wants attr\_readers) and faster (no point generating the singleton  
> class every time thru the loop):
> 
> class Foo  
> &nbsp;&nbsp;def initialize( hash={} )  
> &nbsp;&nbsp;&nbsp;&nbsp;singleton\_class = class \<\< self; self; end  
> &nbsp;&nbsp;&nbsp;&nbsp;hash.each do |key,value|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instance\_variable\_set( "@" + key, value )  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;singleton\_class.class\_eval { attr\_reader key }  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end  
> end

/slaps forehead  
Of course, very nice 🙂

---

<div class="post-metadata">

**Author:** ![Ara.T.Howard6](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@Ara.T.Howard6](https://rubytalk.org/u/Ara.T.Howard6)\
**Post date:** [17 October 2006 15:04 UTC](https://rubytalk.org/t/dynamically-assigning-instance-variables-redux/31994/10 "2006-10-17T15:04:34Z")

</div>

or, with attributes.rb:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;harp:~ \> cat a.rb  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;require 'rubygems'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;require 'attributes'

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;class Foo  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def initialize(h={}) h.each{|k,v| attributes k =\> v} end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foo = Foo.new 'a' =\> 4, :k =\> 2  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p foo.a  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p foo.k

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;harp:~ \> ruby a.rb  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;4  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2

😉

-a

> **···**
>
> On Tue, 17 Oct 2006, Phrogz wrote:
> 
> > matt neuburg wrote:
> > 
> > > Wow, that was way cool. I think we can make it shorter (esp since he  
> > > only wants attr\_readers) and faster (no point generating the singleton  
> > > class every time thru the loop):
> > > 
> > > class Foo  
> > > &nbsp;&nbsp;def initialize( hash={} )  
> > > &nbsp;&nbsp;&nbsp;&nbsp;singleton\_class = class \<\< self; self; end  
> > > &nbsp;&nbsp;&nbsp;&nbsp;hash.each do |key,value|  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instance\_variable\_set( "@" + key, value )  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;singleton\_class.class\_eval { attr\_reader key }  
> > > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > > &nbsp;&nbsp;end  
> > > end
> > 
> > /slaps forehead  
> > Of course, very nice 🙂
> 
> --  
> my religion is very simple. my religion is kindness. -- the dalai lama

---

<div class="post-metadata">

**Author:** ![BIl\_Kleb1](https://avatars.discourse-cdn.com/v4/letter/b/a587f6/32.png) [@BIl\_Kleb1](https://rubytalk.org/u/BIl_Kleb1)\
**Post date:** [17 October 2006 17:45 UTC](https://rubytalk.org/t/dynamically-assigning-instance-variables-redux/31994/11 "2006-10-17T17:45:09Z")

</div>

Phrogz wrote:

> I have no idea what Mr.  
> Kleb wanted, but given that he's adding accessors based on hashes  
> passed to each instance, my guess was that he would want a unique set  
> per instance.
> 
> &nbsp;&nbsp;f1 = Foo.new :name=\>'whee'  
> &nbsp;&nbsp;f2 = Foo.new :age=\>12  
> &nbsp;&nbsp;p f1.age #=\> nil
> 
> Is the above result desirable, Bill?

I don't know about Mr. Kleb and Bill, but as far as Bil's  
concerned, that's precisely what I'm after (although I  
didn't understand this subtlety when I originally asked).

Regards,

> **···**
>
> --  
> Bil Kleb, PhD, MBA  
> [http://fun3d.larc.nasa.gov](http://fun3d.larc.nasa.gov)
