String as attribute

Hi

I have an object “a_row” with columns first, last, second

to retrieve the value first I can do a_row.first but I need to do it a
different way like this

a_row.get_value_for_colum(“first”)

and in the object class I would have

def get_value_for_column(attribute)
return attribute
end

but clearly i have an error attribute variable or method not defined.

or I was thinking of doing
self.#{attribute} but big error

***** I wanted to know if it is possible to achieve what I would like
to do
as the first solution is impossible to do for various reasons


Thank You

On May 5, 4:53 pm, Anthony W. [email protected]
wrote:

***** I wanted to know if it is possible to achieve what I would like
to do
as the first solution is impossible to do for various reasons


well there’s always read_attribute (and remember that
foo.read_attribute(:bar) is the same as foo[:bar] )

Fred

Frederick C. wrote:

On May 5, 4:53�pm, Anthony W. [email protected]
wrote:

***** �I wanted to know if it is possible to achieve what I would like
to do
� � � �as the first solution is impossible to do for various reasons


well there’s always read_attribute (and remember that
foo.read_attribute(:bar) is the same as foo[:bar] )

Fred

Hi,

thank you, just discovered read_attribute a few minutes ago!!

again thank you

I think rather than doing read_attribute, you should do

def get_value_for_column(attribute)
send(attribute)
end

because if you have

def something
read_attribute(:first) + read_attribute(:second)
end

then get_value_for_column(“something”) would not work in case of
read_attribute in get_value_for_column

-Arpit J.

On 5 May 2009, at 15:04, arpit [email protected] wrote:

I think rather than doing read_attribute, you should do

def get_value_for_column(attribute)
send(attribute)
end

Sort of depends what problem you are trying to use (eg send won’t work
if you had a legacy schema with a column name that isn’t a legal name
for a ruby method).

Fred

yeah you are right. Thanks for clarifying :slight_smile:

Frederick C. wrote:

Sort of depends what problem you are trying to use (eg send won’t work
if you had a legacy schema with a column name that isn’t a legal name
for a ruby method).

s/isn’t/is

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs