# Dynamically created functions?

**URL:** <https://www.ruby-forum.com/t/dynamically-created-functions/175721>\
**Category:** Ruby\
**Created:** [September 18, 2009, 12:10pm UTC](https://www.ruby-forum.com/t/dynamically-created-functions/175721 "2009-09-18T12:10:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![wanyixue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/958977/32.png) [@wanyixue](https://www.ruby-forum.com/u/wanyixue)\
**Post date:** [September 18, 2009, 12:10pm UTC](https://www.ruby-forum.com/t/dynamically-created-functions/175721/1 "2009-09-18T12:10:09Z")

</div>

def self.developer\_options(\*args)  
fetch\_options({‘area’=\>‘developers’,‘index’ =\> args.first})  
end

def self.priority\_options(\*args)  
fetch\_options({‘area’=\>‘priority’,‘index’ =\> args.first})  
end

def self.status\_options(\*args)  
fetch\_options({‘area’=\>‘status’,‘index’ =\> args.first})  
end

is there anyway to dynamically create methods with a specific name ?

ie: create methods which are

def self.???\_options(\*args)  
fetch\_options({‘area’=\>’???’,‘index’ =\> args.first})  
end

---

<div class="post-metadata">

**Author:** ![wanyixue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/958977/32.png) [@wanyixue](https://www.ruby-forum.com/u/wanyixue)\
**Post date:** [September 18, 2009, 12:28pm UTC](https://www.ruby-forum.com/t/dynamically-created-functions/175721/2 "2009-09-18T12:28:52Z")

</div>

Lin Wj wrote:

> end
> 
> is there anyway to dynamically create methods with a specific name ?
> 
> ie: create methods which are
> 
> def self.???\_options(\*args)  
> fetch\_options({‘area’=\>‘???’,‘index’ =\> args.first})  
> end

see [Class: Module (Ruby 1.8.7)](http://www.ruby-doc.org/core-1.8.7/classes/Module.html#M000396)

regards  
ralf

---

<div class="post-metadata">

**Author:** ![wanyixue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/958977/32.png) [@wanyixue](https://www.ruby-forum.com/u/wanyixue)\
**Post date:** [September 18, 2009, 12:41pm UTC](https://www.ruby-forum.com/t/dynamically-created-functions/175721/3 "2009-09-18T12:41:45Z")

</div>

On Fri, Sep 18, 2009 at 12:10 PM, Lin Wj [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end
> 
> is there anyway to dynamically create methods with a specific name ?
> 
> ie: create methods which are
> 
> def self.???\_options(\*args)  
> fetch\_options({‘area’=\>‘???’,‘index’ =\> args.first})  
> end

As Ralf said, you can use define\_method. If you want to create them  
up-front (untested):

%w{developer priority status}.each do |area|  
define\_method “#{area}\_options” do |\*args|  
fetch\_options({‘area’ =\> area, ‘index’ =\> args.first})  
end  
end

Of course this needs to be done in the scope where self is the  
singleton class of the object (see a recent post about this).

If you want to define them lazily, you can use method\_missing  
(untested):

def method\_missing (meth, _args, &blk)  
m = meth.match /(._)\_options$/  
super unless m  
define\_method meth do |\*arguments|  
fetch\_options({‘area’ =\> m[1], ‘index’ =\> arguments.first})  
end  
end

Hope this helps,

Jesus.

---

<div class="post-metadata">

**Author:** ![wanyixue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/958977/32.png) [@wanyixue](https://www.ruby-forum.com/u/wanyixue)\
**Post date:** [September 22, 2009, 6:20am UTC](https://www.ruby-forum.com/t/dynamically-created-functions/175721/4 "2009-09-22T06:20:37Z")

</div>

Thanks for the help !

greatly appreciated
