# Dynamically reference instance vars

**URL:** <https://rubytalk.org/t/dynamically-reference-instance-vars/60661>\
**Category:** ruby-talk\
**Created:** [30 October 2010 02:14 UTC](https://rubytalk.org/t/dynamically-reference-instance-vars/60661 "2010-10-30T02:14:50Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Greg\_Willits](https://avatars.discourse-cdn.com/v4/letter/g/cab0a1/32.png) [@Greg\_Willits](https://rubytalk.org/u/Greg_Willits)\
**Post date:** [30 October 2010 02:14 UTC](https://rubytalk.org/t/dynamically-reference-instance-vars/60661/1 "2010-10-30T02:14:50Z")

</div>

If I need to dynamically reference instance vars, is this the only way  
to do it (var set example)?

&nbsp;&nbsp;my\_object.send(:instance\_variable\_set, "@#{iname}", ivalue)

I expected something more elegant, but this is the only way I can get it  
to work. No biggie, just curious.

More complete example below.

-- gw

class Shape  
&nbsp;&nbsp;attr\_accessor :size, :fill\_color, :line\_color, :line\_width  
&nbsp;&nbsp;def initialize  
&nbsp;&nbsp;&nbsp;&nbsp;@size = ""  
&nbsp;&nbsp;&nbsp;&nbsp;@fill\_color = ""  
&nbsp;&nbsp;&nbsp;&nbsp;@line\_color = ""  
&nbsp;&nbsp;&nbsp;&nbsp;@line\_width = ""  
&nbsp;&nbsp;end  
end

my\_shape = Shape.new

shape\_details = {  
&nbsp;&nbsp;:size =\> 'small',  
&nbsp;&nbsp;:fill\_color =\> 'red',  
&nbsp;&nbsp;:line\_color =\> 'black',  
&nbsp;&nbsp;:line\_width =\> '2'}

shape\_details.each do |iname, ivalue|  
&nbsp;&nbsp;my\_shape.send(:instance\_variable\_set, "@#{iname}", ivalue)  
end

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Jeremy\_Bopp](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/jeremy_bopp/32/1937_2.png) [@Jeremy\_Bopp](https://rubytalk.org/u/Jeremy_Bopp)\
**Post date:** [30 October 2010 02:31 UTC](https://rubytalk.org/t/dynamically-reference-instance-vars/60661/2 "2010-10-30T02:31:38Z")

</div>

How about backing the accessors with a hash that also has an accessor?  
That way you could merge in a hash of settings or set/get them neatly by  
name:

class Shape  
&nbsp;&nbsp;attr\_reader :details

&nbsp;&nbsp;def initialize  
&nbsp;&nbsp;&nbsp;&nbsp;@details = {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:size =\> "",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:fill\_color =\> "",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:line\_color =\> "",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:line\_width =\> ""  
&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;end

&nbsp;&nbsp;def size  
&nbsp;&nbsp;&nbsp;&nbsp;@details[:size]  
&nbsp;&nbsp;end

&nbsp;&nbsp;def size=(size)  
&nbsp;&nbsp;&nbsp;&nbsp;@details[:size] = size  
&nbsp;&nbsp;end

&nbsp;&nbsp;def fill\_color  
&nbsp;&nbsp;&nbsp;&nbsp;@details[:fill\_color]  
&nbsp;&nbsp;end

&nbsp;&nbsp;def fill\_color=(fill\_color)  
&nbsp;&nbsp;&nbsp;&nbsp;@details[:fill\_color] = fill\_color  
&nbsp;&nbsp;end

&nbsp;&nbsp;def line\_color  
&nbsp;&nbsp;&nbsp;&nbsp;@details[:line\_color]  
&nbsp;&nbsp;end

&nbsp;&nbsp;def line\_color=(line\_color)  
&nbsp;&nbsp;&nbsp;&nbsp;@details[:line\_color] = line\_color  
&nbsp;&nbsp;end

&nbsp;&nbsp;def line\_width  
&nbsp;&nbsp;&nbsp;&nbsp;@details[:line\_width]  
&nbsp;&nbsp;end

&nbsp;&nbsp;def line\_width=(line\_width)  
&nbsp;&nbsp;&nbsp;&nbsp;@details[:line\_width] = line\_width  
&nbsp;&nbsp;end  
end

my\_shape = Shape.new

shape\_details = {  
&nbsp;&nbsp;:size =\> 'small',  
&nbsp;&nbsp;:fill\_color =\> 'red',  
&nbsp;&nbsp;:line\_color =\> 'black',  
&nbsp;&nbsp;:line\_width =\> '2'  
}

my\_shape.details.merge!(shape\_details)

-Jeremy

> **···**
>
> On 10/29/2010 09:14 PM, Greg Willits wrote:
> 
> > If I need to dynamically reference instance vars, is this the only way  
> > to do it (var set example)?
> > 
> > &nbsp;&nbsp;my\_object.send(:instance\_variable\_set, "@#{iname}", ivalue)
> > 
> > I expected something more elegant, but this is the only way I can get it  
> > to work. No biggie, just curious.
> > 
> > More complete example below.
> > 
> > -- gw
> > 
> > class Shape  
> > &nbsp;&nbsp;attr\_accessor :size, :fill\_color, :line\_color, :line\_width  
> > &nbsp;&nbsp;def initialize  
> > &nbsp;&nbsp;&nbsp;&nbsp;@size = ""  
> > &nbsp;&nbsp;&nbsp;&nbsp;@fill\_color = ""  
> > &nbsp;&nbsp;&nbsp;&nbsp;@line\_color = ""  
> > &nbsp;&nbsp;&nbsp;&nbsp;@line\_width = ""  
> > &nbsp;&nbsp;end  
> > end
> > 
> > my\_shape = Shape.new
> > 
> > shape\_details = {  
> > &nbsp;&nbsp;:size =\> 'small',  
> > &nbsp;&nbsp;:fill\_color =\> 'red',  
> > &nbsp;&nbsp;:line\_color =\> 'black',  
> > &nbsp;&nbsp;:line\_width =\> '2'}
> > 
> > shape\_details.each do |iname, ivalue|  
> > &nbsp;&nbsp;my\_shape.send(:instance\_variable\_set, "@#{iname}", ivalue)  
> > end

---

<div class="post-metadata">

**Author:** ![Greg\_Willits](https://avatars.discourse-cdn.com/v4/letter/g/cab0a1/32.png) [@Greg\_Willits](https://rubytalk.org/u/Greg_Willits)\
**Post date:** [30 October 2010 19:27 UTC](https://rubytalk.org/t/dynamically-reference-instance-vars/60661/3 "2010-10-30T19:27:42Z")

</div>

Appreciate the effort, but the point has been lost. Don't focus on the  
hash or mass assignment, that was just a device to create an example.

The question is whether there's a shorter way to do this:

&nbsp;&nbsp;my\_object.send(:instance\_variable\_set, "@#{iname}", ivalue)

In another language I used, I could simply do the equivalent of

&nbsp;&nbsp;myobject.iname = ivalue

that's because the syntax was like this  
&nbsp;&nbsp;#myobject-\>#iname = ivalue

Where # denotes a local var, so it was obvious to the parser that #iname  
was not a method name, whereas with Ruby it's not obvious.

I was hoping in Ruby that at least this was possible:

&nbsp;&nbsp;myobject.send(iname) = ivalue  
&nbsp;&nbsp;--or--  
&nbsp;&nbsp;myobject.send(iname, ivalue)

but that doesn't work. I could maybe force it to work by manually  
creating setter methods, but that's not an elegant solution either, I'd  
rather have the occassional use of instance\_variable\_set than craft  
setters.

Anyway, not a biggie, just a curiosity.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![John\_Mair](https://avatars.discourse-cdn.com/v4/letter/j/ac91a4/32.png) [@John\_Mair](https://rubytalk.org/u/John_Mair)\
**Post date:** [30 October 2010 20:06 UTC](https://rubytalk.org/t/dynamically-reference-instance-vars/60661/4 "2010-10-30T20:06:44Z")

</div>

You realize that instance\_variable\_set isn't private right?

So you can simply go:

&nbsp;&nbsp;&nbsp;&nbsp;my\_object.instance\_variable\_set("@#{iname}", ivalue)

Greg Willits wrote in post #958159:

> **···**
>
> > If I need to dynamically reference instance vars, is this the only way  
> > to do it (var set example)?
> > 
> > &nbsp;&nbsp;my\_object.send(:instance\_variable\_set, "@#{iname}", ivalue)
> > 
> > I expected something more elegant, but this is the only way I can get it  
> > to work. No biggie, just curious.
> > 
> > More complete example below.
> > 
> > -- gw
> > 
> > class Shape  
> > &nbsp;&nbsp;attr\_accessor :size, :fill\_color, :line\_color, :line\_width  
> > &nbsp;&nbsp;def initialize  
> > &nbsp;&nbsp;&nbsp;&nbsp;@size = ""  
> > &nbsp;&nbsp;&nbsp;&nbsp;@fill\_color = ""  
> > &nbsp;&nbsp;&nbsp;&nbsp;@line\_color = ""  
> > &nbsp;&nbsp;&nbsp;&nbsp;@line\_width = ""  
> > &nbsp;&nbsp;end  
> > end
> > 
> > my\_shape = Shape.new
> > 
> > shape\_details = {  
> > &nbsp;&nbsp;:size =\> 'small',  
> > &nbsp;&nbsp;:fill\_color =\> 'red',  
> > &nbsp;&nbsp;:line\_color =\> 'black',  
> > &nbsp;&nbsp;:line\_width =\> '2'}
> > 
> > shape\_details.each do |iname, ivalue|  
> > &nbsp;&nbsp;my\_shape.send(:instance\_variable\_set, "@#{iname}", ivalue)  
> > end
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Andrew\_Wagner](https://avatars.discourse-cdn.com/v4/letter/a/22d042/32.png) [@Andrew\_Wagner](https://rubytalk.org/u/Andrew_Wagner)\
**Post date:** [30 October 2010 12:28 UTC](https://rubytalk.org/t/dynamically-reference-instance-vars/60661/5 "2010-10-30T12:28:46Z")

</div>

C'mon, this is ruby! Revel in it!

class Shape  
&nbsp;&nbsp;attr\_reader :details  
&nbsp;&nbsp;def self.keys  
&nbsp;&nbsp;&nbsp;&nbsp;[:size, :fill\_color, :line\_color, :line\_color]  
&nbsp;&nbsp;end

&nbsp;&nbsp;def initialize  
&nbsp;&nbsp;&nbsp;&nbsp;@details = {}  
&nbsp;&nbsp;&nbsp;&nbsp;self.class.keys.each {|key| @details[key] = "" }  
&nbsp;&nbsp;end

&nbsp;&nbsp;keys.each do |key|  
&nbsp;&nbsp;&nbsp;&nbsp;define\_method key do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@details[key]  
&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;define\_method "#{key}=" do |input|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@details[key] = input  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end

my\_shape = Shape.new

shape\_details = {  
:size =\> 'small',  
:fill\_color =\> 'red',  
:line\_color =\> 'black',  
:line\_width =\> '2'  
}

my\_shape.details.merge!(shape\_details)

> **···**
>
> On Fri, Oct 29, 2010 at 10:31 PM, Jeremy Bopp \<jeremy@bopp.net\> wrote:
> 
> > On 10/29/2010 09:14 PM, Greg Willits wrote:  
> > \> If I need to dynamically reference instance vars, is this the only way  
> > \> to do it (var set example)?  
> > \>  
> > \> my\_object.send(:instance\_variable\_set, "@#{iname}", ivalue)  
> > \>  
> > \> I expected something more elegant, but this is the only way I can get it  
> > \> to work. No biggie, just curious.  
> > \>  
> > \> More complete example below.  
> > \>  
> > \> -- gw  
> > \>  
> > \>  
> > \> class Shape  
> > \> attr\_accessor :size, :fill\_color, :line\_color, :line\_width  
> > \> def initialize  
> > \> @size = ""  
> > \> @fill\_color = ""  
> > \> @line\_color = ""  
> > \> @line\_width = ""  
> > \> end  
> > \> end  
> > \>  
> > \> my\_shape = Shape.new  
> > \>  
> > \> shape\_details = {  
> > \> :size =\> 'small',  
> > \> :fill\_color =\> 'red',  
> > \> :line\_color =\> 'black',  
> > \> :line\_width =\> '2'}  
> > \>  
> > \> shape\_details.each do |iname, ivalue|  
> > \> my\_shape.send(:instance\_variable\_set, "@#{iname}", ivalue)  
> > \> end
> > 
> > How about backing the accessors with a hash that also has an accessor?  
> > That way you could merge in a hash of settings or set/get them neatly by  
> > name:
> > 
> > class Shape  
> > attr\_reader :details
> > 
> > def initialize  
> > &nbsp;&nbsp;&nbsp;@details = {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:size =\> "",  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:fill\_color =\> "",  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:line\_color =\> "",  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:line\_width =\> ""  
> > &nbsp;&nbsp;&nbsp;}  
> > end
> > 
> > &nbsp;&nbsp;def size  
> > &nbsp;&nbsp;&nbsp;@details[:size]  
> > end
> > 
> > def size=(size)  
> > &nbsp;&nbsp;&nbsp;@details[:size] = size  
> > end
> > 
> > def fill\_color  
> > &nbsp;&nbsp;&nbsp;@details[:fill\_color]  
> > end
> > 
> > def fill\_color=(fill\_color)  
> > &nbsp;&nbsp;&nbsp;@details[:fill\_color] = fill\_color  
> > end
> > 
> > def line\_color  
> > &nbsp;&nbsp;&nbsp;@details[:line\_color]  
> > end
> > 
> > def line\_color=(line\_color)  
> > &nbsp;&nbsp;&nbsp;@details[:line\_color] = line\_color  
> > end
> > 
> > def line\_width  
> > &nbsp;&nbsp;&nbsp;@details[:line\_width]  
> > end
> > 
> > def line\_width=(line\_width)  
> > &nbsp;&nbsp;&nbsp;@details[:line\_width] = line\_width  
> > &nbsp;&nbsp;end  
> > end
> > 
> > my\_shape = Shape.new
> > 
> > shape\_details = {  
> > :size =\> 'small',  
> > :fill\_color =\> 'red',  
> > :line\_color =\> 'black',  
> > :line\_width =\> '2'  
> > }
> > 
> > my\_shape.details.merge!(shape\_details)
> > 
> > -Jeremy

---

<div class="post-metadata">

**Author:** ![Jesus\_Gabriel\_y\_Gala](https://avatars.discourse-cdn.com/v4/letter/j/96bed5/32.png) [@Jesus\_Gabriel\_y\_Gala](https://rubytalk.org/u/Jesus_Gabriel_y_Gala)\
**Post date:** [30 October 2010 19:33 UTC](https://rubytalk.org/t/dynamically-reference-instance-vars/60661/6 "2010-10-30T19:33:49Z")

</div>

Crafting setters is pretty simple, though:

class A  
attr\_writer :a  
end

a = A.new  
a.a = 3

Jesus.

> **···**
>
> On Sat, Oct 30, 2010 at 9:27 PM, Greg Willits \<lists@gregwillits.ws\> wrote:
> 
> > Appreciate the effort, but the point has been lost. Don't focus on the  
> > hash or mass assignment, that was just a device to create an example.
> > 
> > The question is whether there's a shorter way to do this:
> > 
> > my\_object.send(:instance\_variable\_set, "@#{iname}", ivalue)
> > 
> > In another language I used, I could simply do the equivalent of
> > 
> > myobject.iname = ivalue
> > 
> > that's because the syntax was like this  
> > #myobject-\>#iname = ivalue
> > 
> > Where # denotes a local var, so it was obvious to the parser that #iname  
> > was not a method name, whereas with Ruby it's not obvious.
> > 
> > I was hoping in Ruby that at least this was possible:
> > 
> > myobject.send(iname) = ivalue  
> > --or--  
> > myobject.send(iname, ivalue)
> > 
> > but that doesn't work. I could maybe force it to work by manually  
> > creating setter methods, but that's not an elegant solution either, I'd  
> > rather have the occassional use of instance\_variable\_set than craft  
> > setters.
> > 
> > Anyway, not a biggie, just a curiosity.

---

<div class="post-metadata">

**Author:** ![Tony\_Arcieri4](https://avatars.discourse-cdn.com/v4/letter/t/c77e96/32.png) [@Tony\_Arcieri4](https://rubytalk.org/u/Tony_Arcieri4)\
**Post date:** [30 October 2010 19:49 UTC](https://rubytalk.org/t/dynamically-reference-instance-vars/60661/7 "2010-10-30T19:49:20Z")

</div>

> The question is whether there's a shorter way to do this:
> 
> my\_object.send(:instance\_variable\_set, "@#{iname}", ivalue)
> 
> In another language I used, I could simply do the equivalent of
> 
> myobject.iname = ivalue

This breaks the encapsulation that objects are supposed to provide for their  
hidden state. I have to say I \*like\* the fact that  
instance\_variable\_get/instance\_variable\_set are long and unwieldy. By using  
them you hopefully remind yourself that you're doing something naughty by  
touching an object's private parts.

> I was hoping in Ruby that at least this was possible:
> 
> myobject.send(iname) = ivalue  
> --or--  
> myobject.send(iname, ivalue)
> 
> but that doesn't work. I could maybe force it to work by manually  
> creating setter methods, but that's not an elegant solution either, I'd  
> rather have the occassional use of instance\_variable\_set than craft  
> setters.

It's Ruby, a lot is possible! You could add some method\_missing magic so:

&nbsp;&nbsp;myobj.iv\_foobar = 42

thunks to:

&nbsp;&nbsp;instance\_variable\_set(:@foobar, 42)

I would strongly recommend against that sort of thing though.

> **···**
>
> On Sat, Oct 30, 2010 at 1:27 PM, Greg Willits \<lists@gregwillits.ws\> wrote:
> 
> --  
> Tony Arcieri  
> Medioh! A Kudelski Brand

---

<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:** [30 October 2010 19:57 UTC](https://rubytalk.org/t/dynamically-reference-instance-vars/60661/8 "2010-10-30T19:57:41Z")

</div>

You're confusing methods and variables. They're not the same, but you're on the right track with the code above, but you'd be calling the getter, not the setter. Check it:

class X  
&nbsp;&nbsp;attr\_accessor :x # creates x and x= methods  
end

o = X.new  
o.send("x=", 42)  
p x

> **···**
>
> On Oct 30, 2010, at 12:27 , Greg Willits wrote:
> 
> > I was hoping in Ruby that at least this was possible:
> > 
> > myobject.send(iname) = ivalue  
> > --or--  
> > myobject.send(iname, ivalue)

---

<div class="post-metadata">

**Author:** ![Jeremy\_Bopp](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/jeremy_bopp/32/1937_2.png) [@Jeremy\_Bopp](https://rubytalk.org/u/Jeremy_Bopp)\
**Post date:** [30 October 2010 15:40 UTC](https://rubytalk.org/t/dynamically-reference-instance-vars/60661/9 "2010-10-30T15:40:04Z")

</div>

Excellent point! Thanks!

-Jeremy

> **···**
>
> On 10/30/2010 07:28 AM, Andrew Wagner wrote:
> 
> > C'mon, this is ruby! Revel in it!
> > 
> > class Shape  
> > &nbsp;&nbsp;attr\_reader :details  
> > &nbsp;&nbsp;def self.keys  
> > &nbsp;&nbsp;&nbsp;&nbsp;[:size, :fill\_color, :line\_color, :line\_color]  
> > &nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;def initialize  
> > &nbsp;&nbsp;&nbsp;&nbsp;@details = {}  
> > &nbsp;&nbsp;&nbsp;&nbsp;self.class.keys.each {|key| @details[key] = "" }  
> > &nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;keys.each do |key|  
> > &nbsp;&nbsp;&nbsp;&nbsp;define\_method key do  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@details[key]  
> > &nbsp;&nbsp;&nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;define\_method "#{key}=" do |input|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@details[key] = input  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;end  
> > end
> > 
> > my\_shape = Shape.new
> > 
> > shape\_details = {  
> > :size =\> 'small',  
> > :fill\_color =\> 'red',  
> > :line\_color =\> 'black',  
> > :line\_width =\> '2'  
> > }
> > 
> > my\_shape.details.merge!(shape\_details)

---

<div class="post-metadata">

**Author:** ![Greg\_Willits](https://avatars.discourse-cdn.com/v4/letter/g/cab0a1/32.png) [@Greg\_Willits](https://rubytalk.org/u/Greg_Willits)\
**Post date:** [30 October 2010 20:11 UTC](https://rubytalk.org/t/dynamically-reference-instance-vars/60661/10 "2010-10-30T20:11:32Z")

</div>

Ryan Davis wrote in post #958245:

> > I was hoping in Ruby that at least this was possible:
> > 
> > myobject.send(iname) = ivalue  
> > --or--  
> > myobject.send(iname, ivalue)
> 
> You're confusing methods and variables. They're not the same, but you're  
> on the right track with the code above, but you'd be calling the getter,  
> not the setter. Check it:
> 
> class X  
> &nbsp;&nbsp;attr\_accessor :x # creates x and x= methods  
> end
> 
> o = X.new  
> o.send("x=", 42)  
> p x

I wasn't confusing them, I was hoping Ruby's send would be flexible  
enough to work with both of them (in conjunction with the accessors  
being defined). What I didn't think of in this context was Ruby's x=  
being considered the setter method and not just x, which I knew, and  
should have recognized -- so yep, that was the ticket. You win! 🙂  
Sorry, no prizes ☹

Thanks.

-- gw

> **···**
>
> > On Oct 30, 2010, at 12:27 , Greg Willits wrote:
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [31 October 2010 12:40 UTC](https://rubytalk.org/t/dynamically-reference-instance-vars/60661/11 "2010-10-31T12:40:13Z")

</div>

Here are two other approaches using widely underused class Struct and that differ in the way they deal with values not present in the Hash:

Shape = Struct.new :size, :fill\_color, :line\_color, :line\_width do  
&nbsp;&nbsp;&nbsp;def self.from\_hash\_1(h)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sh = new  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;h.each {|k,v| sh[k] = v}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sh  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;def self.from\_hash\_2(h)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sh = new  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;members.each {|m| sh[m] = h[m]}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sh  
&nbsp;&nbsp;&nbsp;end  
end

irb(main):015:0\* s1 = Shape.from\_hash\_1(  
irb(main):016:1\* :size =\> 'small',  
irb(main):017:1\* :fill\_color =\> 'red',  
irb(main):018:1\* :line\_color =\> 'black',  
irb(main):019:1\* :line\_width =\> '2'  
irb(main):020:1\> )  
=\> #\<struct Shape size="small", fill\_color="red", line\_color="black", line\_width="2"\>  
irb(main):021:0\> s2 = Shape.from\_hash\_2(  
irb(main):022:1\* :size =\> 'small',  
irb(main):023:1\* :fill\_color =\> 'red',  
irb(main):024:1\* :line\_color =\> 'black',  
irb(main):025:1\* :line\_width =\> '2'  
irb(main):026:1\> )  
=\> #\<struct Shape size="small", fill\_color="red", line\_color="black", line\_width="2"\>

If you like that better you can as well define

def Shape(h)  
&nbsp;&nbsp;&nbsp;sh = Shape.new  
&nbsp;&nbsp;&nbsp;# logic from above  
&nbsp;&nbsp;&nbsp;sh  
end

Then you can do

s1 = Shape(  
&nbsp;&nbsp;&nbsp;:size =\> 'small',  
&nbsp;&nbsp;&nbsp;:fill\_color =\> 'red',  
&nbsp;&nbsp;&nbsp;:line\_color =\> 'black',  
&nbsp;&nbsp;&nbsp;:line\_width =\> '2'  
)

Kind regards

&nbsp;&nbsp;robert

> **···**
>
> On 30.10.2010 22:11, Greg Willits wrote:
> 
> > Ryan Davis wrote in post #958245:
> > 
> > > On Oct 30, 2010, at 12:27 , Greg Willits wrote:
> > > 
> > > > I was hoping in Ruby that at least this was possible:
> > > > 
> > > > &nbsp;&nbsp;myobject.send(iname) = ivalue  
> > > > &nbsp;&nbsp;--or--  
> > > > &nbsp;&nbsp;myobject.send(iname, ivalue)
> > > 
> > > You're confusing methods and variables. They're not the same, but you're  
> > > on the right track with the code above, but you'd be calling the getter,  
> > > not the setter. Check it:
> > > 
> > > class X  
> > > &nbsp;&nbsp;&nbsp;attr\_accessor :x # creates x and x= methods  
> > > end
> > > 
> > > o = X.new  
> > > o.send("x=", 42)  
> > > p x
> > 
> > I wasn't confusing them, I was hoping Ruby's send would be flexible  
> > enough to work with both of them (in conjunction with the accessors  
> > being defined). What I didn't think of in this context was Ruby's x=  
> > being considered the setter method and not just x, which I knew, and  
> > should have recognized -- so yep, that was the ticket. You win! 🙂  
> > Sorry, no prizes ☹
> 
> --  
> remember.guy do |as, often| as.you\_can - without end  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)
