manufacturing methods

Discussion in 'Ruby' started by Matthew Pounsett, Feb 1, 2010.

  1. 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
     
    Matthew Pounsett, Feb 1, 2010
    #1
    1. Advertising

  2. Matthew Pounsett

    Josh Cheek Guest

    [Note: parts of this message were removed to make it a legal post.]

    On Mon, Feb 1, 2010 at 1:05 AM, Matthew Pounsett <> wrote:

    > 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
    >
    >
    >
    >

    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

    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.
     
    Josh Cheek, Feb 1, 2010
    #2
    1. Advertising

  3. 2010/2/1 Josh Cheek <>:
    > On Mon, Feb 1, 2010 at 1:05 AM, Matthew Pounsett <> wro=

    te:
    >
    >> 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 variable=

    s in
    >> methods. =A0Not exactly what I'm looking for.
    >>
    >> The question I've got is related to manufacturing methods. =A0In my
    >> particular case, I'm using a module that relies heavily on callback meth=

    ods.
    >> =A0I need to define a couple dozen callbacks which I'd like to all behav=

    e in
    >> basically the same way. =A0In order to keep my code simple, I thought I'=

    d try
    >> manufacturing them all from the same basic code.. but haven't found a sy=

    ntax
    >> 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 meth=

    ods
    >> "on_foo", "on_bar", and "on_baz":
    >>
    >> %w( foo bar baz ).each do |callback|
    >> =A0 def on_#{callback}
    >> =A0 =A0 =A0# callback work here
    >> =A0 end
    >> end
    >>
    >> Is what I'm attempting to do even possible?
    >>
    >> Thanks,
    >> =A0Matt
    >>
    >>
    >>
    >>

    > If you are in a class, you should can do:
    >
    > class Test
    > =A0%w( foo bar baz ).each do |callback|
    > =A0 =A0define_method "on_#{callback}" do |param|
    > =A0 =A0 =A0puts "This is method #{callback}"
    > =A0 =A0 =A0puts "I have received the parameter: #{param}"
    > =A0 =A0 =A0puts
    > =A0 =A0end
    > =A0end
    > end
    >
    >
    > test =3D Test.new
    > (test.methods - Object.methods).each_with_index do |method,index|
    > =A0test.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=3D%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=3D%p\n", a
    end

    %w{foo bar baz}.each {|m| alias_method m, :eek:n_event_impl}
    end

    Yet another option is to use #method_missing

    class Z
    def method_missing(s, *a, &b)
    if /\Aon_/ =3D~ s.to_s
    printf "args=3D%p\n", a
    else
    super
    end
    end
    end

    Kind regards

    robert

    --=20
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
     
    Robert Klemme, Feb 1, 2010
    #3
  4. 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
     
    Matthew Pounsett, Feb 2, 2010
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Neo
    Replies:
    1
    Views:
    524
    Scott Allen
    Jan 7, 2005
  2. Buster Copley
    Replies:
    5
    Views:
    562
    Gianni Mariani
    Jul 7, 2003
  3. Tron Thomas
    Replies:
    10
    Views:
    1,085
    Tom Widmer
    Nov 10, 2004
  4. Oliver Wong
    Replies:
    14
    Views:
    1,628
    Chris Uppal
    Jun 13, 2006
  5. Kenneth McDonald
    Replies:
    5
    Views:
    324
    Kenneth McDonald
    Sep 26, 2008
Loading...

Share This Page