How to overide "include" method?

Discussion in 'Ruby' started by Alexandre Mutel, Nov 18, 2009.

  1. I tried several way, but didn't found any to override the behavior of
    the "include" method.
    I would like to override it's behavior to accept a string as an argument
    and perform a special include based on that.. so I tried something like
    this:

    class Module
    def include(module1, *smth) # :doc:
    puts "In my include #{self}"
    if ( module1.is_a? String)
    instance_eval(File.read(module1), module1)
    else
    super module1,*smth
    end
    end
    end

    without any success, this method is nether called.

    Any ideas?
    --
    Posted via http://www.ruby-forum.com/.
    Alexandre Mutel, Nov 18, 2009
    #1
    1. Advertising

  2. Alexandre Mutel

    Phrogz Guest

    On Nov 18, 8:42 am, Alexandre Mutel <> wrote:
    > I tried several way, but didn't found any to override the behavior of
    > the "include" method.


    # Class methods of classes inherit from instance methods of
    # the Class class before they inherit from instance methods of Module.
    # http://phrogz.net/RubyLibs/RubyMethodLookupFlow.png
    class Class
    def include(*args)
    p "hi!"
    super
    end
    end

    module Bar; end

    class Foo
    include Bar #=> "hi!"
    end

    p Foo.ancestors
    #=> [Foo, Bar, Object, Kernel]
    Phrogz, Nov 18, 2009
    #2
    1. Advertising

  3. Gavin Kistner wrote:
    > # Class methods of classes inherit from instance methods of
    > # the Class class before they inherit from instance methods of Module.
    > # http://phrogz.net/RubyLibs/RubyMethodLookupFlow.png


    Thanks! Your example is working well until i use it inside a class
    definition.

    But when i'm trying to use it from the irb prompt, but the overloaded
    include it's not called...
    Is there a way to found from where the method is define? ( with
    method:)include).something?), so i know where to define the overload...

    For example, if i put the class overloading in a separate file
    class_includer.rb
    class Class
    def include(*args)
    p "hi!"
    super
    end
    end

    And from the irb :
    require "class_includer"
    include "test"

    The method above is not called. It means that it's implemented elsewhere
    in this case (outside a class definition, in the top level).

    I tried to overload directly the include method in the irb, but it's not
    working as well, very strange... well, i have probably to understand
    more how scope on self is working!

    So from the top level, where i can overload this "include" method?
    --
    Posted via http://www.ruby-forum.com/.
    Alexandre Mutel, Nov 18, 2009
    #3
  4. Alexandre Mutel

    Phrogz Guest

    On Nov 18, 10:47 am, Alexandre Mutel <> wrote:
    > Gavin Kistner wrote:
    > But when i'm trying to use it from the irb prompt, but the overloaded
    > include it's not called...
    > Is there a way to found from where the method is define? ( with
    > method:)include).something?), so i know where to define the overload...


    Apparently not, since method:)include).source_location returns nil.

    But you can see that the include at the top level is custom for main,
    not the same include from modules:

    irb(main):001:0> class Foo; end
    => nil

    irb(main):002:0> [ method:)include), method:)include).owner ]
    => [#<Method: main.include>, #<Class:#<Object:0x000001008b8e58>>]

    irb(main):003:0> [ Foo.method:)include), Foo.method:)include).owner ]
    => [#<Method: Class(Module)#include>, Module]

    > So from the top level, where i can overload this "include" method?


    Good question. I'm not sure where main fits into the method lookup
    flow. (But if someone tells me, I'll add it to the diagram :)
    Phrogz, Nov 19, 2009
    #4
  5. Gavin Kistner wrote:
    >> So from the top level, where i can overload this "include" method?

    >
    > Good question. I'm not sure where main fits into the method lookup
    > flow. (But if someone tells me, I'll add it to the diagram :)


    Seems that overriding like this is working :

    $x = method:)include)
    def self.include(mod)
    puts "Hi"
    $x.call mod
    end

    --
    Posted via http://www.ruby-forum.com/.
    Alexandre Mutel, Nov 19, 2009
    #5
  6. In a cleaner way, it seems to work on the irb level:

    class << self
    alias :_old_include_method :include
    def include(mod)
    puts "Hi"
    _old_include_method mod
    end
    end

    --
    Posted via http://www.ruby-forum.com/.
    Alexandre Mutel, Nov 19, 2009
    #6
    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. Steve
    Replies:
    1
    Views:
    529
    Hywel
    Sep 24, 2004
  2. Nikos
    Replies:
    5
    Views:
    2,291
  3. David Morgenthaler

    How to overide "for line in file:"

    David Morgenthaler, Feb 22, 2004, in forum: Python
    Replies:
    11
    Views:
    535
    Duncan Booth
    Feb 23, 2004
  4. Eddie
    Replies:
    2
    Views:
    258
    Eddie
    Sep 16, 2004
  5. overide?

    , Oct 12, 2005, in forum: C++
    Replies:
    8
    Views:
    451
Loading...

Share This Page