# Meta programming question for Ruby gurus

**URL:** <https://discuss.rubyonrails.org/t/meta-programming-question-for-ruby-gurus/10749>\
**Category:** rubyonrails-talk\
**Created:** [March 25, 2007, 8:51am UTC](https://discuss.rubyonrails.org/t/meta-programming-question-for-ruby-gurus/10749 "2007-03-25T08:51:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Neeraj\_Kumar](https://avatars.discourse-cdn.com/v4/letter/n/977dab/32.png) [@Neeraj\_Kumar](https://discuss.rubyonrails.org/u/Neeraj_Kumar)\
**Post date:** [March 25, 2007, 8:51am UTC](https://discuss.rubyonrails.org/t/meta-programming-question-for-ruby-gurus/10749/1 "2007-03-25T08:51:39Z")

</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) &nbsp;&nbsp;&nbsp;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:** ![Nasir\_Khan](https://avatars.discourse-cdn.com/v4/letter/n/5daacb/32.png) [@Nasir\_Khan](https://discuss.rubyonrails.org/u/Nasir_Khan)\
**Post date:** [March 25, 2007, 9:35am UTC](https://discuss.rubyonrails.org/t/meta-programming-question-for-ruby-gurus/10749/2 "2007-03-25T09:35:11Z")

</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:** ![Michael\_Guterl](https://avatars.discourse-cdn.com/v4/letter/m/db5fbb/32.png) [@Michael\_Guterl](https://discuss.rubyonrails.org/u/Michael_Guterl)\
**Post date:** [March 25, 2007, 3:06pm UTC](https://discuss.rubyonrails.org/t/meta-programming-question-for-ruby-gurus/10749/3 "2007-03-25T15:06:29Z")

</div>

Hi,

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

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

---

<div class="post-metadata">

**Author:** ![Neeraj\_Kumar](https://avatars.discourse-cdn.com/v4/letter/n/977dab/32.png) [@Neeraj\_Kumar](https://discuss.rubyonrails.org/u/Neeraj_Kumar)\
**Post date:** [March 25, 2007, 3:16pm UTC](https://discuss.rubyonrails.org/t/meta-programming-question-for-ruby-gurus/10749/4 "2007-03-25T15:16:07Z")

</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 &nbsp;&nbsp;&nbsp;def bar(input) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#not sure what goes on here &nbsp;&nbsp;&nbsp;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:** ![Michael\_Latta](https://avatars.discourse-cdn.com/v4/letter/m/d26b3c/32.png) [@Michael\_Latta](https://discuss.rubyonrails.org/u/Michael_Latta)\
**Post date:** [March 25, 2007, 4:08pm UTC](https://discuss.rubyonrails.org/t/meta-programming-question-for-ruby-gurus/10749/5 "2007-03-25T16:08:23Z")

</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:** ![Nasir\_Khan](https://avatars.discourse-cdn.com/v4/letter/n/5daacb/32.png) [@Nasir\_Khan](https://discuss.rubyonrails.org/u/Nasir_Khan)\
**Post date:** [March 25, 2007, 5:36pm UTC](https://discuss.rubyonrails.org/t/meta-programming-question-for-ruby-gurus/10749/6 "2007-03-25T17:36:13Z")

</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 == 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
