# Pass option value to InstanceMethods define\_method

**URL:** <https://www.ruby-forum.com/t/pass-option-value-to-instancemethods-define-method/150368>\
**Category:** Rails\
**Created:** [October 7, 2008, 8:19pm UTC](https://www.ruby-forum.com/t/pass-option-value-to-instancemethods-define-method/150368 "2008-10-07T20:19:01Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![vidmantas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/8797f3/32.png) [@vidmantas](https://www.ruby-forum.com/u/vidmantas)\
**Post date:** [October 7, 2008, 8:19pm UTC](https://www.ruby-forum.com/t/pass-option-value-to-instancemethods-define-method/150368/1 "2008-10-07T20:19:01Z")

</div>

Hello,

I’m trying to develop a ActiveRecord plugin but run into the following  
problem: I pass arguments to

write\_inheritable\_attribute :some\_field, ‘value’  
class\_inheritable\_reader :some\_field

in ClassMethods module and I’m available to get them in InstanceMethods  
with self.class.some\_field. But when I try to do like this:

define\_method “#{self.class.some\_field}”  
…

or

class\_eval \<\<-eval  
def #{self.class.some\_field}  
…  
end  
eval

I get NoMethodError: undefined method `some\_field’ for Module:Class

Anybody could help with this?

---

<div class="post-metadata">

**Author:** ![vidmantas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/8797f3/32.png) [@vidmantas](https://www.ruby-forum.com/u/vidmantas)\
**Post date:** [October 7, 2008, 8:24pm UTC](https://www.ruby-forum.com/t/pass-option-value-to-instancemethods-define-method/150368/2 "2008-10-07T20:24:51Z")

</div>

On Oct 7, 7:19 pm, Vidmantas Kabošis \<rails-mailing-l…@andreas-  
[s.net](http://s.net)\> wrote:

> 

I may not be reading you correctly, but at that point do you not need  
to be using self.some\_field ? (self.class.some\_field would be trying  
to find that method on Class or something like that.

Fred

---

<div class="post-metadata">

**Author:** ![vidmantas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/8797f3/32.png) [@vidmantas](https://www.ruby-forum.com/u/vidmantas)\
**Post date:** [October 8, 2008, 9:36am UTC](https://www.ruby-forum.com/t/pass-option-value-to-instancemethods-define-method/150368/3 "2008-10-08T09:36:54Z")

</div>

Frederick C. wrote:

> using self.some\_field ?

I’ve tried this too, but then I get NoMethodError: undefined method  
`some\_field’ for MyModule::InstanceMethods:Module

---

<div class="post-metadata">

**Author:** ![vidmantas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/8797f3/32.png) [@vidmantas](https://www.ruby-forum.com/u/vidmantas)\
**Post date:** [October 8, 2008, 9:47am UTC](https://www.ruby-forum.com/t/pass-option-value-to-instancemethods-define-method/150368/4 "2008-10-08T09:47:50Z")

</div>

On Oct 8, 8:36 am, Vidmantas Kabošis \<rails-mailing-l…@andreas-  
[s.net](http://s.net)\> wrote:

> Frederick C. wrote:
> 
> > using self.some\_field ?
> 
> I’ve tried this too, but then I get NoMethodError: undefined method  
> `some\_field’ for MyModule::InstanceMethods:Module

I think you need to post more of your code. It’s not clear where you  
are calling what which is rather important here.

Fred

---

<div class="post-metadata">

**Author:** ![vidmantas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/8797f3/32.png) [@vidmantas](https://www.ruby-forum.com/u/vidmantas)\
**Post date:** [October 8, 2008, 12:19pm UTC](https://www.ruby-forum.com/t/pass-option-value-to-instancemethods-define-method/150368/5 "2008-10-08T12:19:18Z")

</div>

On 8 Oct 2008, at 11:11, Vidmantas Kabošis wrote:

> define\_method “#{self.some\_field}” do  
> # code  
> end  
> end  
> end

OK. when the define\_method runs, self is the module InstanceMethods.  
At that point it hasn’t even been included in anything so there is no  
way to get the value of some\_field.

I would just be doing this from the money\_is method (or if you want to  
keep the InstanceMethods module then from its included hook.

Fred

---

<div class="post-metadata">

**Author:** ![vidmantas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/8797f3/32.png) [@vidmantas](https://www.ruby-forum.com/u/vidmantas)\
**Post date:** [October 8, 2008, 12:11pm UTC](https://www.ruby-forum.com/t/pass-option-value-to-instancemethods-define-method/150368/6 "2008-10-08T12:11:36Z")

</div>

Frederick C. wrote:

> I think you need to post more of your code.

Sure:

module MyModule  
def self.included(base)  
base.extend ClassMethods  
end

module ClassMethods  
def money\_is(field\_name)  
write\_inheritable\_attribute :some\_field, ‘value’  
class\_inheritable\_reader :some\_field

```
  self.send(:include, MyModule::InstanceMethods)
end

```

end

module InstanceMethods  
define\_method “#{self.some\_field}” do  
# code  
end  
end  
end

> script/console  
> Loading development environment (Rails 2.1.0)  
> /home/vidmantas/projects/my\_project/vendor/plugins/my\_module/lib/my\_module.rb:16:NoMethodError:  
> undefined method `some\_field’ for MyModule::InstanceMethods:Module

---

<div class="post-metadata">

**Author:** ![vidmantas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/8797f3/32.png) [@vidmantas](https://www.ruby-forum.com/u/vidmantas)\
**Post date:** [October 8, 2008, 3:03pm UTC](https://www.ruby-forum.com/t/pass-option-value-to-instancemethods-define-method/150368/7 "2008-10-08T15:03:38Z")

</div>

Got it, thank you!

Frederick C. wrote:

> I would just be doing this from the money\_is method (or if you want to  
> keep the InstanceMethods module then from its included hook.
