[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

undefined method 'a=' when initializing an object

bparanj@gmail.com

6/21/2007 5:05:00 PM

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.

11 Answers

Gregory Brown

6/21/2007 7:01:00 PM

0

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?

bparanj@gmail.com

6/21/2007 7:16:00 PM

0

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

Gregory Brown

6/21/2007 7:21:00 PM

0

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.

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.

rking

6/21/2007 7:27:00 PM

0

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.

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

bparanj@gmail.com

6/21/2007 7:27:00 PM

0

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

fformby

1/19/2010 7:51:00 AM

0

On Thu, 21 Jan 2010 00:30:42 +0900, "Fartingo" <farnindo@att.net>
wrote:

>
>"The Revd" <fformby@smythe.com> wrote in message
>news:4b555810.2670343@news.x-privat.org...
>> On Wed, 20 Jan 2010 19:40:06 +0900, "Farnindo" <farnindo@att.net>
>> wrote:
>>
>>>You still suck and lick your thumb, don't you?
>>
>> WE ask the questions, you ignorant jewloving asshole.
>
>Foul! You expect too much from your shit
>Muzzie asshole.!
>lol!


English isn't your native language, is it, you illiterate piece of
shit?

LOL

The Peeler

1/19/2010 12:40:00 PM

0

On Tue, 19 Jan 2010 07:50:57 GMT, The Retd wrote:

>>> On Wed, 20 Jan 2010 19:40:06 +0900, "Farnindo" <farnindo@att.net>
>>> wrote:
>>>
>>>>You still suck and lick your thumb, don't you?
>>>
>>> WE ask the questions, you ignorant jewloving asshole.
>>
>>Foul! You expect too much from your shit
>>Muzzie asshole.!
>>lol!
>
> English isn't your native language, is it, you illiterate piece of
> shit?
>
> LOL

You keep having serious comprehension problems, don't you, Retd? Maybe
increase your medication dose? <BG>

The Peeler

1/19/2010 12:40:00 PM

0

On Tue, 19 Jan 2010 06:58:44 GMT, The Retd wrote:

> On Wed, 20 Jan 2010 19:40:06 +0900, "Farnindo" <farnindo@att.net>
> wrote:
>
>>You still suck and lick your thumb, don't you?
>
> WE ask the questions, you ignorant jewloving asshole.
>

Just HOW many people are there on Usenet already that enjoy beating the shit
out of you every day, dumb psycho? LMAO!

fformby

1/20/2010 10:15:00 AM

0

On Thu, 21 Jan 2010 20:22:37 +0900, "Fartingo@att.net> wrote:

>
>"The Revd" <fformby@smythe.com> wrote in message
>news:4b556433.5777453@news.x-privat.org...
>> On Thu, 21 Jan 2010 00:30:42 +0900, "Fartingo" <farnindo@att.net>
>
>
>Hey Muzzie asshole, here's what happened
>after your vaginal transplant
>
> http://home.att.net/~extremebubble128/nin...

Hey, jew cocksucker, I don't do links.

>Bwhahahahahhhhahahahahhaahahahhahahaaaa
>Anyway, my native language is HTML

It shows. You're one illiterate asshole.

Farnindo

1/21/2010 11:23:00 AM

0


"The Revd" <fformby@smythe.com> wrote in message
news:4b556433.5777453@news.x-privat.org...
> On Thu, 21 Jan 2010 00:30:42 +0900, "Fartingo" <farnindo@att.net>


Hey Muzzie asshole, here's what happened
after your vaginal transplant

http://home.att.net/~extremebubble128/nin...


Bwhahahahahhhhahahahahhaahahahhahahaaaa
Anyway, my native language is HTML