String as attribute


#1

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


#2

On May 5, 4:53 pm, Anthony W. removed_email_address@domain.invalid
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


#3

Frederick C. wrote:

On May 5, 4:53�pm, Anthony W. removed_email_address@domain.invalid
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


#4

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.


#5

On 5 May 2009, at 15:04, arpit removed_email_address@domain.invalid 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


#6

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