# Plugin: create instance methods dynamically in ClassMethods?

**URL:** <https://www.ruby-forum.com/t/plugin-create-instance-methods-dynamically-in-classmethods/120803>\
**Category:** Rails\
**Created:** [November 20, 2007, 6:59pm UTC](https://www.ruby-forum.com/t/plugin-create-instance-methods-dynamically-in-classmethods/120803 "2007-11-20T18:59:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [November 20, 2007, 6:59pm UTC](https://www.ruby-forum.com/t/plugin-create-instance-methods-dynamically-in-classmethods/120803/1 "2007-11-20T18:59:49Z")

</div>

Hi all

I have the following plugin code:

module IncenseCrud  
def self.included(base\_class)  
base\_class.extend(ClassMethods)  
end

module ClassMethods  
def performs\_incense\_crud  
def index  
list  
return render(:action =\> ‘list’)  
end  
end  
end  
end

In init.rb I have the following:

require File.dirname( **FILE** ) + ‘/lib/incense\_crud’  
ActionController::Base.send(:include, IncenseCrud)

So far so good. In any controller I can put now

class AnyController \< ApplicationController  
performs\_incense\_crud  
end

and the performs\_incense\_crud method is invoked. Cool - the class method  
has been added to the controller through the plugin.  
But strangely the instance method index does not seem to have been  
added! It’s just disappeared when trying to reach

www.xxx.yyy/any\_controller/index

What’s wrong here? Can’t I add these methods dynamically this way?

Thanks  
Josh

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [November 20, 2007, 8:15pm UTC](https://www.ruby-forum.com/t/plugin-create-instance-methods-dynamically-in-classmethods/120803/2 "2007-11-20T20:15:47Z")

</div>

define\_method usually does the trick when trying to define methods  
dynamically 🙂

Fred

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [November 21, 2007, 2:04pm UTC](https://www.ruby-forum.com/t/plugin-create-instance-methods-dynamically-in-classmethods/120803/3 "2007-11-21T14:04:22Z")

</div>

Frederick C. wrote:

> define\_method usually does the trick when trying to define methods  
> dynamically 🙂
> 
> Fred

Thank you, this works well.

Now I’ve got another question…

In fact, my method performs\_incense\_crud accepts an option parameter:

def performs\_incense\_crud(options = {}) … end

Now I’d like to use this options parameter in another method called  
list:

module IncenseCrud  
def self.included(base\_class)  
base\_class.extend(ClassMethods)  
end

module ClassMethods  
def performs\_incense\_crud(options = {})  
define\_method(:index) do  
list  
return render(:action =\> ‘list’)  
end

```
  define_method(:list) do
    @pages, @objects = paginate :sessions, :per_page => 

```

(options[:items\_per\_page] || 10)  
end  
end  
end  
end

When using…

performs\_incense\_crud :items\_per\_page =\> 5

…it works great. But when omitting this option…

performs\_incense\_crud

…it returns the following error:

NameError in XxxController#index  
undefined local variable or method ` 10’ for  
#LinksController:0x32a896c

The same happens when using ? : …

:per\_page =\> (options[:items\_per\_page] ? options[:items\_per\_page] : 10)

instead of ||.

Can anybody tell why this doesn’t work here? Thanks a lot,  
Josh
