Dynamically created functions?

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

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)

regards
ralf

On Fri, Sep 18, 2009 at 12:10 PM, Lin Wj [email protected] 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.

Thanks for the help !

greatly appreciated