# Meta programming question for Ruby gurus

**URL:** <https://www.ruby-forum.com/t/meta-programming-question-for-ruby-gurus/90037>\
**Category:** Rails\
**Created:** [March 25, 2007, 10:51am UTC](https://www.ruby-forum.com/t/meta-programming-question-for-ruby-gurus/90037 "2007-03-25T10:51:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dorelal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@dorelal](https://www.ruby-forum.com/u/dorelal)\
**Post date:** [March 25, 2007, 10:51am UTC](https://www.ruby-forum.com/t/meta-programming-question-for-ruby-gurus/90037/1 "2007-03-25T10:51:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dorelal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@dorelal](https://www.ruby-forum.com/u/dorelal)\
**Post date:** [March 25, 2007, 11:35am UTC](https://www.ruby-forum.com/t/meta-programming-question-for-ruby-gurus/90037/2 "2007-03-25T11:35:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dorelal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@dorelal](https://www.ruby-forum.com/u/dorelal)\
**Post date:** [March 25, 2007, 5:07pm UTC](https://www.ruby-forum.com/t/meta-programming-question-for-ruby-gurus/90037/3 "2007-03-25T17:07:29Z")

</div>

Hi,

On 3/25/07, Neeraj K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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 G.

---

<div class="post-metadata">

**Author:** ![dorelal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@dorelal](https://www.ruby-forum.com/u/dorelal)\
**Post date:** [March 25, 2007, 6:09pm UTC](https://www.ruby-forum.com/t/meta-programming-question-for-ruby-gurus/90037/4 "2007-03-25T18:09:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dorelal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@dorelal](https://www.ruby-forum.com/u/dorelal)\
**Post date:** [March 25, 2007, 5:17pm UTC](https://www.ruby-forum.com/t/meta-programming-question-for-ruby-gurus/90037/5 "2007-03-25T17:17:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dorelal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@dorelal](https://www.ruby-forum.com/u/dorelal)\
**Post date:** [March 25, 2007, 7:36pm UTC](https://www.ruby-forum.com/t/meta-programming-question-for-ruby-gurus/90037/6 "2007-03-25T19:36:52Z")

</div>

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
