Problems with alias_method

Discussion in 'Ruby' started by Pratik, Mar 8, 2007.

  1. Pratik

    Pratik Guest

    Hi Guys,

    I'm trying to alias_method in a bit different way. Here's what I'm
    trying to do : http://pastie.caboo.se/45668

    Any help would be great.

    Thanks for your time,
    Pratik
    Pratik, Mar 8, 2007
    #1
    1. Advertising

  2. Pratik

    Jan Svitok Guest

    On 3/8/07, Pratik <> wrote:
    > Hi Guys,
    >
    > I'm trying to alias_method in a bit different way. Here's what I'm
    > trying to do : http://pastie.caboo.se/45668
    >
    > Any help would be great.
    >
    > Thanks for your time,
    > Pratik


    Hi,

    if I understand correctly, the problem you want to solve is to move
    repeat_every before the method definition. You are getting
    NoMethorError now, so you've put the line after the method def. This
    error is expected, as the repeat_every is called before the
    interpreter comes to def...

    So, what are you looking for is kind of lazy evaluation. You need to
    postpone your code until the method is defined. One way to do this is
    to use method_added callback. Just set a flag that you are wainting
    for a method definition and then, when the method is defined, do your
    stuff. Notice that you get the name of the method as an argument, so
    this can be easily extended to handle any method.

    module Hello
    def self.included(base)
    base.extend ClassMethods
    end

    module ClassMethods
    def method_added(method_name)
    @waiting ||= false
    return unless @waiting
    @waiting = false
    method = <<-METHOD

    define_method:)main_with_schedule) do |*args|
    while true
    puts "looping"
    main_without_schedule(*args)
    sleep #{@sleep_time}
    end
    end
    alias_method :main_without_schedule, :main
    alias_method :main, :main_with_schedule
    METHOD

    self.instance_eval(method)
    end

    def repeat_every(time = 5)
    @sleep_time = time
    @waiting = true
    end
    end
    end

    class Redworld
    include Hello

    # I want to have repeat_every before main() method. But it gives
    method not found error.
    repeat_every 2 #formerly commented out

    def main
    puts "Hello World"
    end
    # here was: repeat_every 5

    end
    Jan Svitok, Mar 8, 2007
    #2
    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. Ilias Lazaridis
    Replies:
    16
    Views:
    533
    Ilias Lazaridis
    Jun 10, 2006
  2. Angel Martin

    alias or alias_method problem

    Angel Martin, Apr 29, 2004, in forum: Ruby
    Replies:
    6
    Views:
    116
    Kristof Bastiaensen
    Apr 29, 2004
  3. Kelly Felkins
    Replies:
    1
    Views:
    108
    Robert Klemme
    Aug 9, 2005
  4. Trans
    Replies:
    0
    Views:
    81
    Trans
    Nov 23, 2005
  5. Ara.T.Howard

    alias_method 'stickiness'

    Ara.T.Howard, Mar 13, 2006, in forum: Ruby
    Replies:
    1
    Views:
    84
Loading...

Share This Page