how to wrap method of singleton class

Discussion in 'Ruby' started by Ittay Dror, Nov 21, 2008.

  1. Ittay Dror

    Ittay Dror Guest

    Hi,


    Given a class:

    class Foo

    class << self

    def foo

    puts 'foo'

    end

    end

    end


    How do I write a module, so that when included in Foo it will wrap the
    method foo so that Foo.foo will print:

    bar

    foo


    ?


    Thank you

    Ittay





    --
    --
    Ittay Dror <>
    Ittay Dror, Nov 21, 2008
    #1
    1. Advertising

  2. Ittay Dror

    James Coglan Guest

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

    > How do I write a module, so that when included in Foo it will wrap the
    > method foo so that Foo.foo will print:




    You can't (at least not without doing some sneaky metaprogramming -- this
    code is still a work in progress: http://gist.github.com/25104). A class's
    own methods take precedence over any modules mixed into it. What you can do
    is reopen the singleton class and store a reference to the old method:

    class << Foo # Foo singleton class
    old_foo = Foo.method:)foo)
    define_method:)foo) do |*args|
    old_result = old_foo.call(*args)
    old_result.upcase
    end
    end

    Foo.foo #=> "FOO"

    You must use define_method for this instead of def. def does not use a block
    so you won't get a closure, which you need to retain visibility of the
    old_foo variable.
    James Coglan, Nov 21, 2008
    #2
    1. Advertising

  3. Ittay Dror

    Ittay Dror Guest

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



    James Coglan wrote:
    >> How do I write a module, so that when included in Foo it will wrap the
    >> method foo so that Foo.foo will print:
    >>

    >
    >
    >
    > You can't (at least not without doing some sneaky metaprogramming -- this
    > code is still a work in progress: http://gist.github.com/25104). A class's
    > own methods take precedence over any modules mixed into it. What you can do
    > is reopen the singleton class and store a reference to the old method:
    >
    > class << Foo # Foo singleton class
    > old_foo = Foo.method:)foo)
    > define_method:)foo) do |*args|
    > old_result = old_foo.call(*args)
    > old_result.upcase
    > end
    > end
    >
    >

    doing it this way, i can do:
    class << Foo
    alias_method :eek:riginal, :foo
    def foo
    puts 'bar'
    original
    end
    end

    but i want a module i can mix in. (for regular methods, the module can
    use the included callback to alias the methods, just not sure how to do
    that for a singleton class)
    > Foo.foo #=> "FOO"
    >
    > You must use define_method for this instead of def. def does not use a block
    > so you won't get a closure, which you need to retain visibility of the
    > old_foo variable.
    >
    >


    --
    Ittay Dror <>
    Tikal <http://www.tikalk.com>
    Tikal Project <http://tikal.sourceforge.net>


    --
    --
    Ittay Dror <>
    Ittay Dror, Nov 21, 2008
    #3
    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. Aaron Fude

    To wrap or not to wrap?

    Aaron Fude, May 8, 2008, in forum: Java
    Replies:
    12
    Views:
    693
    Chronic Philharmonic
    May 10, 2008
  2. Paul McMahon
    Replies:
    3
    Views:
    204
    David A. Black
    Jun 9, 2008
  3. Charles Oliver Nutter

    Singleton methods without the singleton class

    Charles Oliver Nutter, Mar 15, 2010, in forum: Ruby
    Replies:
    4
    Views:
    199
    Charles Oliver Nutter
    Mar 22, 2010
  4. Art Werschulz

    Text::Wrap::wrap difference

    Art Werschulz, Sep 22, 2003, in forum: Perl Misc
    Replies:
    0
    Views:
    226
    Art Werschulz
    Sep 22, 2003
  5. Art Werschulz

    Text::Wrap::wrap difference

    Art Werschulz, Sep 24, 2003, in forum: Perl Misc
    Replies:
    1
    Views:
    242
    Anno Siegel
    Sep 25, 2003
Loading...

Share This Page