# Undefined method 'a=' when initializing an object

**URL:** <https://rubytalk.org/t/undefined-method-a-when-initializing-an-object/38513>\
**Category:** ruby-talk\
**Created:** [21 June 2007 17:05 UTC](https://rubytalk.org/t/undefined-method-a-when-initializing-an-object/38513 "2007-06-21T17:05:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bala\_Paranj](https://avatars.discourse-cdn.com/v4/letter/b/41988e/32.png) [@Bala\_Paranj](https://rubytalk.org/u/Bala_Paranj)\
**Post date:** [21 June 2007 17:05 UTC](https://rubytalk.org/t/undefined-method-a-when-initializing-an-object/38513/1 "2007-06-21T17:05:22Z")

</div>

I have an array of strings that contains the values for attributes of  
an object. I have another array that contains the list of attributes  
(strings) for an object.

I am creating the class by:

obj = class\_name.new

which gives me a valid object for the given object name.

I want to do something like:

obj.attribute1 = value1 and so on by looping through the arrays.

When I run eval("obj.#{a} = #{value}"), I get the undefined method  
'a=' error message. Please help. TIA.

---

<div class="post-metadata">

**Author:** ![Greg\_Brown1](https://avatars.discourse-cdn.com/v4/letter/g/258eb7/32.png) [@Greg\_Brown1](https://rubytalk.org/u/Greg_Brown1)\
**Post date:** [21 June 2007 19:01 UTC](https://rubytalk.org/t/undefined-method-a-when-initializing-an-object/38513/2 "2007-06-21T19:01:27Z")

</div>

Please do not use eval() unless you have a good reason to! It's very  
dangerous if you're not careful.

What you're looking for is:

obj.send("#{a}=", value)

Still, you're going to need to show more code for people to be able to  
help you. Where is a being defined, what is obj?

> **···**
>
> On 6/21/07, Bala Paranj \<bparanj@gmail.com\> wrote:
> 
> > When I run eval("obj.#{a} = #{value}"), I get the undefined method  
> > 'a=' error message. Please help. TIA.

---

<div class="post-metadata">

**Author:** ![Ryan\_King](https://avatars.discourse-cdn.com/v4/letter/r/67e7ee/32.png) [@Ryan\_King](https://rubytalk.org/u/Ryan_King)\
**Post date:** [21 June 2007 19:30 UTC](https://rubytalk.org/t/undefined-method-a-when-initializing-an-object/38513/3 "2007-06-21T19:30:09Z")

</div>

Is this what you're looking for?

require  
'ostruct'  
example\_data =  
{  
&nbsp;&nbsp;'ba' =\>  
'ilar',  
&nbsp;&nbsp;'la' =\>  
'bamba',  
&nbsp;&nbsp;'pa' =\>  
'thological',  
&nbsp;&nbsp;'ranj' =\>  
'eofmotion(?)',  
}  
obj =  
OpenStruct.new  
example\_data.each do |key,  
value\>  
&nbsp;&nbsp;obj.send "#{key}=",  
value  
end  
p  
obj

# or  
just:  
obj2 = OpenStruct.new  
example\_data  
p obj2

> **···**
>
> On Jun 21, 10:05 am, "Bala Paranj" \<bpar...@gmail.com\> wrote:
> 
> > I have an array of strings that contains the values for attributes of  
> > an object. I have another array that contains the list of attributes  
> > (strings) for an object.

---

<div class="post-metadata">

**Author:** ![Bala\_Paranj](https://avatars.discourse-cdn.com/v4/letter/b/41988e/32.png) [@Bala\_Paranj](https://rubytalk.org/u/Bala_Paranj)\
**Post date:** [21 June 2007 19:15 UTC](https://rubytalk.org/t/undefined-method-a-when-initializing-an-object/38513/4 "2007-06-21T19:15:50Z")

</div>

I have an array that contains list of attributes of a ActiveRecord  
object. I have another array that contains list of values for those  
attributes.

So for a Foo object,

attributes\_list = ["first\_name", "last\_name"]  
values\_list = ["Bugs", "Bunny"]

I have instantiated the class by:

class\_name = class\_string.constantize  
obj = class\_name.new

Then I just loop up through those arrays and set values to the attributes:

obj.attribute1 = value1

I get:

undefined method `attribute1=' for #\<Foo:0x3326da8\>

How do I set values for the attributes? TIA.

> **···**
>
> On 6/21/07, Gregory Brown \<gregory.t.brown@gmail.com\> wrote:
> 
> > On 6/21/07, Bala Paranj \<bparanj@gmail.com\> wrote:
> > 
> > \> When I run eval("obj.#{a} = #{value}"), I get the undefined method  
> > \> 'a=' error message. Please help. TIA.
> > 
> > Please do not use eval() unless you have a good reason to! It's very  
> > dangerous if you're not careful.
> > 
> > What you're looking for is:
> > 
> > obj.send("#{a}=", value)
> > 
> > Still, you're going to need to show more code for people to be able to  
> > help you. Where is a being defined, what is obj?

---

<div class="post-metadata">

**Author:** ![Greg\_Brown1](https://avatars.discourse-cdn.com/v4/letter/g/258eb7/32.png) [@Greg\_Brown1](https://rubytalk.org/u/Greg_Brown1)\
**Post date:** [21 June 2007 19:21 UTC](https://rubytalk.org/t/undefined-method-a-when-initializing-an-object/38513/5 "2007-06-21T19:21:22Z")

</div>

Are you trying to set the values on an instance of Foo, or on a model instance?

If it's an instance of a class Foo, look at the documentation for attr\_accessor.

> **···**
>
> On 6/21/07, Bala Paranj \<bparanj@gmail.com\> wrote:
> 
> > I have an array that contains list of attributes of a ActiveRecord  
> > object. I have another array that contains list of values for those  
> > attributes.
> > 
> > So for a Foo object,
> > 
> > attributes\_list = ["first\_name", "last\_name"]  
> > values\_list = ["Bugs", "Bunny"]
> > 
> > I have instantiated the class by:
> > 
> > class\_name = class\_string.constantize  
> > obj = class\_name.new
> > 
> > Then I just loop up through those arrays and set values to the attributes:
> > 
> > obj.attribute1 = value1
> > 
> > I get:
> > 
> > undefined method `attribute1=' for #\<Foo:0x3326da8\>
> > 
> > How do I set values for the attributes? TIA.

---

<div class="post-metadata">

**Author:** ![Bala\_Paranj](https://avatars.discourse-cdn.com/v4/letter/b/41988e/32.png) [@Bala\_Paranj](https://rubytalk.org/u/Bala_Paranj)\
**Post date:** [21 June 2007 19:27 UTC](https://rubytalk.org/t/undefined-method-a-when-initializing-an-object/38513/6 "2007-06-21T19:27:26Z")

</div>

Thanks Greg, your suggestion worked. Ruby community support rocks!

> **···**
>
> On 6/21/07, Gregory Brown \<gregory.t.brown@gmail.com\> wrote:
> 
> > On 6/21/07, Bala Paranj \<bparanj@gmail.com\> wrote:  
> > \> I have an array that contains list of attributes of a ActiveRecord  
> > \> object. I have another array that contains list of values for those  
> > \> attributes.  
> > \>
