Call overwritten method of included module? - RUBY

This is a discussion on Call overwritten method of included module? - RUBY ; Hi all module MyModule def my_method return "my module rocks!" end end class MyClass include(MyModule) def my_method return "my class rocks!" end end Is there a way to call MyModule::my_method from within MyClass? "super" sadly doesn't work... Thanks a lot ...

+ Reply to Thread
Results 1 to 5 of 5

Call overwritten method of included module?

  1. Default Call overwritten method of included module?

    Hi all

    module MyModule
    def my_method
    return "my module rocks!"
    end
    end

    class MyClass
    include(MyModule)

    def my_method
    return "my class rocks!"
    end
    end

    Is there a way to call MyModule::my_method from within MyClass? "super"
    sadly doesn't work...

    Thanks a lot
    Josh
    --
    Posted via http://www.ruby-forum.com/.


  2. Default Re: Call overwritten method of included module?

    On Nov 22, 2007, at 6:21 PM, Joshua Muheim wrote:

    > Hi all
    >
    > module MyModule
    > def my_method
    > return "my module rocks!"
    > end
    > end
    >
    > class MyClass
    > include(MyModule)
    >
    > def my_method
    > return "my class rocks!"
    > end
    > end
    >
    > Is there a way to call MyModule::my_method from within MyClass?
    > "super"
    > sadly doesn't work...


    Perhaps this will do:

    <code>
    module MyModule
    def my_method
    "my module rocks!"
    end
    end

    class MyClass
    include MyModule
    alias module_method my_method
    def my_method
    "#{module_method}\nmy class rocks!"
    end
    end

    puts MyClass.new.my_method
    </code>

    <result>
    my module rocks!
    my class rocks!
    </result>

    Regards, Morton




  3. Default Re: Call overwritten method of included module?

    > module MyModule
    > def my_method
    > return "my module rocks!"
    > end
    > end
    >=20
    > class MyClass
    > include(MyModule)
    >=20
    > def my_method
    > return "my class rocks!"
    > end
    > end
    >=20
    > Is there a way to call MyModule::my_method from within=20
    > MyClass? "super"
    > sadly doesn't work...


    You'd need to alias the old method:

    class MyClass
    include(MyModule)
    alias ld_my_method :my_method
    def my_method
    old_my_method
    end
    end

    (If you're using rails/activesupport, you'd probably use
    alias_method_chain to simplify things)

    Dan.


  4. Default Re: Call overwritten method of included module?

    > (If you're using rails/activesupport, you'd probably use
    > alias_method_chain to simplify things)
    >
    > Dan.


    Thanks! Especially for the alias_method_chain hint! :-)
    --
    Posted via http://www.ruby-forum.com/.


  5. Default Re: Call overwritten method of included module?

    On Nov 23, 2007 12:21 AM, Joshua Muheim <forum@josh.ch> wrote:
    > Hi all
    >
    > module MyModule
    > def my_method
    > return "my module rocks!"
    > end
    > end
    >
    > class MyClass
    > include(MyModule)
    >
    > def my_method
    > return "my class rocks!"
    > end
    > end
    >
    > Is there a way to call MyModule::my_method from within MyClass? "super"
    > sadly doesn't work...

    super shall work and indeed does
    module MyModule
    def my_method
    return "my module rocks!"
    end
    end

    class MyClass
    include(MyModule)

    def my_method

    return [ super, "my class rocks!" ].join("\n")
    end
    end

    puts MyClass.new.my_method

    HTH
    Robert
    >
    > Thanks a lot
    > Josh
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >




    --
    what do I think about Ruby?
    http://ruby-smalltalk.blogspot.com/


+ Reply to Thread

Similar Threads

  1. FAQ 7.25 Why can't a method included in this same file be found?
    By Application Development in forum Perl
    Replies: 0
    Last Post: 10-23-2007, 02:03 AM
  2. How to detect overwritten virtual method in base class?
    By Application Development in forum c++
    Replies: 3
    Last Post: 08-23-2007, 08:23 PM
  3. FAQ 7.24 Why can't a method included in this same file be found?
    By Application Development in forum Perl
    Replies: 0
    Last Post: 08-04-2007, 02:03 AM
  4. FAQ 7.24 Why can't a method included in this same file be found?
    By Application Development in forum Perl
    Replies: 0
    Last Post: 05-15-2007, 08:03 PM
  5. Can't call method <function> on an undefined value at <module>
    By Application Development in forum Perl
    Replies: 2
    Last Post: 06-27-2005, 04:33 PM