Forum: Ruby on Rails Meta programming question for Ruby gurus

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
934f858e451cf9b771996b2940cd696b?d=identicon&s=25 Raj Singh (dorelal)
on 2007-03-25 10:51
(Received via mailing list)
@person.foo             # method name is foo
@person.baaz         # method name is baaz


I have a need to pass the name of the attribute and not the value of
the attribute

def process(input)
   puts input
end


process(@person.foo)  # result should be 'foo' and not the value of foo

how do I pass the value 'foo' the attribute name and not the value of
attribute to another method.

Any help?
9aafe2636a115bbdc137c93445054f22?d=identicon&s=25 Nasir Khan (Guest)
on 2007-03-25 11:35
(Received via mailing list)
Perhaps some context would be helpful in this case, but short of that I
am
assuming you want to invoke the named method later.
You can either pass around the symbols as method names (:foo or :baaz in
your case) and later use send()

e.g

class A
   def hello
     puts "hello"
   end
 end

a = A.new

a.send :hello   #  send message to the object

or alternatively you can use the Method object  like

 m = a.method(:hello)
 m.call


HTH
D337e2cf7c6d03caac6988d83e0e7f66?d=identicon&s=25 Michael Guterl (Guest)
on 2007-03-25 17:07
(Received via mailing list)
Hi,

On 3/25/07, Neeraj Kumar <neeraj.jsr@gmail.com> wrote:
>    puts input
> end
>
>
> process(@person.foo)  # result should be 'foo' and not the value of foo
>
> how do I pass the value 'foo' the attribute name and not the value of
> attribute to another method.
>
> Any help?


Are you looking for something like this?

module Kernel
 private
    def this_method_name
      caller[0] =~ /`([^']*)'/ and $1
    end
end

class Foo
 def bar
   this_method_name
 end

 def baaz
   this_method_name
 end
end

puts Foo.new.bar      # => bar
puts Foo.new.baaz   # => baaz

>
>
HTH,
Michael Guterl
934f858e451cf9b771996b2940cd696b?d=identicon&s=25 Raj Singh (dorelal)
on 2007-03-25 17:17
(Received via mailing list)
Sorry I should have been much clearer.

The context is that I need the name of the column for the given
attribute

Person table has name,email and age.

class Foo
   def bar(input)
       #not sure what goes on here
   end
end


Now

@user = User.new

@foo = Foo.new

@foo.bar(@user.age or some form or it)  # output shoud the column name
'age'


Thanks.
588ab1c0a5610a7e160a3b101abb91e6?d=identicon&s=25 MichaelLatta (Guest)
on 2007-03-25 18:09
(Received via mailing list)
I do not think you are going to be able to use the API you are
thinking about.

You are going to have to generate the string before you call the
method as in:

@foo.bar("user.age")

That is why all the association methods take symbols not references.
Ruby does not allow you to change the semantics of the calling
expression.

Michael
9aafe2636a115bbdc137c93445054f22?d=identicon&s=25 Nasir Khan (Guest)
on 2007-03-25 19:36
(Received via mailing list)
So if I understand you right then you are probably trying to map the
value
of the column back to the column name.

eg.If you have a table like

id, name,   age

1,  "Alice",  25
2,  "Bob" ,  35

Then given row for id 2, if you have a Fixnum 35 you want to be able to
get
string "age". Is that right?

I am not a Rails expert but you could perhaps use column_names method on
AR.


your bar could look like

def bar(active_record, value)
  active_record.attribute_names.each do |x|
     return active_record.column_for_attribute(x).name  if
active_record[x]
== value
  end
end

and call it with  bar(@user, @user.age) as in your example.

There is a big caveat though and that is since we are comparing values
there
could be more than one column of the same value and this snippet matches
the
first match.

Without going into specifics of what you need you should perhaps look
for
some alternate approach which could come from your domain use case.

- nasir
This topic is locked and can not be replied to.