# Manufacturing methods

**URL:** <https://www.ruby-forum.com/t/manufacturing-methods/182659>\
**Category:** Ruby\
**Created:** [February 1, 2010, 8:08am UTC](https://www.ruby-forum.com/t/manufacturing-methods/182659 "2010-02-01T08:08:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mjgp2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/eb8c5e/32.png) [@mjgp2](https://www.ruby-forum.com/u/mjgp2)\
**Post date:** [February 1, 2010, 8:08am UTC](https://www.ruby-forum.com/t/manufacturing-methods/182659/1 "2010-02-01T08:08:31Z")

</div>

Hi there.

Apologies if this is covered somewhere… all the googling I’ve done on  
the subject has wound up with info on class-factories and and using  
variables in methods. Not exactly what I’m looking for.

The question I’ve got is related to manufacturing methods. In my  
particular case, I’m using a module that relies heavily on callback  
methods. I need to define a couple dozen callbacks which I’d like to  
all behave in basically the same way. In order to keep my code simple,  
I thought I’d try manufacturing them all from the same basic code… but  
haven’t found a syntax that works.

Though this syntax clearly doesn’t work, an example of the sort of thing  
I’m looking for might look like this, assuming I want to create the  
methods “on\_foo”, “on\_bar”, and “on\_baz”:

%w( foo bar baz ).each do |callback|  
def on\_#{callback}  
# callback work here  
end  
end

Is what I’m attempting to do even possible?

Thanks,  
Matt

---

<div class="post-metadata">

**Author:** ![mjgp2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/eb8c5e/32.png) [@mjgp2](https://www.ruby-forum.com/u/mjgp2)\
**Post date:** [February 1, 2010, 8:54am UTC](https://www.ruby-forum.com/t/manufacturing-methods/182659/2 "2010-02-01T08:54:10Z")

</div>

On Mon, Feb 1, 2010 at 1:05 AM, Matthew P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> manufacturing them all from the same basic code… but haven’t found a syntax  
> end
> 
> Is what I’m attempting to do even possible?
> 
> Thanks,  
> Matt

If you are in a class, you should can do:

class Test  
%w( foo bar baz ).each do |callback|  
define\_method “on\_#{callback}” do |param|  
puts “This is method #{callback}”  
puts “I have received the parameter: #{param}”  
puts  
end  
end  
end

test = Test.new  
(test.methods - Object.methods).each\_with\_index do |method,index|  
test.send method , index  
end

Here is a list of useful methods like this  
[http://weare.buildingsky.net/2009/08/25/rubys-metaprogramming-toolbox](http://weare.buildingsky.net/2009/08/25/rubys-metaprogramming-toolbox)

They are a bit cumbersome to use, though. For example, I don’t know how  
to  
set it up to receive a block, and you can’t use them certain places.

---

<div class="post-metadata">

**Author:** ![mjgp2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/eb8c5e/32.png) [@mjgp2](https://www.ruby-forum.com/u/mjgp2)\
**Post date:** [February 1, 2010, 10:00am UTC](https://www.ruby-forum.com/t/manufacturing-methods/182659/3 "2010-02-01T10:00:20Z")

</div>

2010/2/1 Josh C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > I need to define a couple dozen callbacks which I’d like to all behave in  
> > # callback work here
> 
> end
> 
> test = Test.new  
> (test.methods - Object.methods).each\_with\_index do |method,index|  
> test.send method , index  
> end

In this case there is a much simpler solution which does not need  
metaprogramming:

class X  
def on\_event\_impl(\*a)  
printf “args=%p\n”, a  
end

alias foo on\_event\_impl  
alias bar on\_event\_impl  
alias baz on\_event\_impl  
end

If you want to use a bit of metaprogramming in order to not repeat you  
can do:

class Y  
def on\_event\_impl(\*a)  
printf “args=%p\n”, a  
end

%w{foo bar baz}.each {|m| alias\_method m, :on\_event\_impl}  
end

Yet another option is to use #method\_missing

class Z  
def method\_missing(s, \*a, &b)  
if /\Aon\_/ =~ s.to\_s  
printf “args=%p\n”, a  
else  
super  
end  
end  
end

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![mjgp2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/eb8c5e/32.png) [@mjgp2](https://www.ruby-forum.com/u/mjgp2)\
**Post date:** [February 2, 2010, 1:13am UTC](https://www.ruby-forum.com/t/manufacturing-methods/182659/4 "2010-02-02T01:13:39Z")

</div>

Thanks very much Robert and Josh for your suggestions. It looks like  
all of these work for what I need, so I’ll just need to pick one.

Thanks!  
Matt
