undefined method 'a=' when initializing an object

B

Bala Paranj

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.
 
G

Gregory Brown

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?
 
B

Bala Paranj

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.
 
G

Gregory Brown

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.

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.
 
R

rking

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.

Is this what you're looking for?

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

# or
just:
obj2 = OpenStruct.new
example_data
p obj2
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top