aliasing an external method from within a method definition?

Discussion in 'Ruby' started by Francis Hwang, Mar 7, 2005.

  1. So maybe some of you language wizards out there can help me with this:
    Is there a way to define a method that aliases some external method
    without assigning that method to a global in the first place?

    The example is the code below: For mockfs/override.rb, I'm undefining
    File, but want to keep File.join.

    $join_method = File.method :join
    Object.send( :remove_const, :File )
    module File
    def self.join( *args )
    $join_method.call *args
    end

    def self.method_missing( symbol, *args )
    MockFS.file.send( symbol, *args )
    end
    end

    This works, but then I'm leaving a global variable just sitting around.
    Is there a better way to do this?

    Francis Hwang
    http://fhwang.net/
    Francis Hwang, Mar 7, 2005
    #1
    1. Advertising

  2. Francis Hwang wrote:
    > So maybe some of you language wizards out there can help me with this:
    > Is there a way to define a method that aliases some external method
    > without assigning that method to a global in the first place?
    >
    > The example is the code below: For mockfs/override.rb, I'm undefining
    > File, but want to keep File.join.
    >
    > $join_method = File.method :join
    > Object.send( :remove_const, :File )
    > module File
    > def self.join( *args )
    > $join_method.call *args
    > end
    >
    > def self.method_missing( symbol, *args )
    > MockFS.file.send( symbol, *args )
    > end
    > end
    >
    > This works, but then I'm leaving a global variable just sitting around.
    > Is there a better way to do this?


    I'll be the firstest with the worstest:

    x = 1
    M = Module.new do
    (class << self; self; end).instance_eval do
    define_method :foo do
    x
    end
    end
    end

    p M.foo # ==> 1

    Mutatis mutandis.
    Joel VanderWerf, Mar 7, 2005
    #2
    1. Advertising

  3. Francis Hwang

    Luke Graham Guest

    On Mon, 7 Mar 2005 16:03:40 +0900, Francis Hwang <> wrote:
    > So maybe some of you language wizards out there can help me with this:
    > Is there a way to define a method that aliases some external method
    > without assigning that method to a global in the first place?
    >
    > The example is the code below: For mockfs/override.rb, I'm undefining
    > File, but want to keep File.join.
    >
    > $join_method = File.method :join
    > Object.send( :remove_const, :File )
    > module File
    > def self.join( *args )
    > $join_method.call *args
    > end
    >
    > def self.method_missing( symbol, *args )
    > MockFS.file.send( symbol, *args )
    > end
    > end
    >
    > This works, but then I'm leaving a global variable just sitting around.
    > Is there a better way to do this?


    Im a long way from a wizard, but how about this...

    OldFile = File.clone
    Object.send :remove_const, :File
    module File
    OldFile = OldFile.clone
    def self.method_missing(symbol, *args)
    File.send symbol, *args
    end
    end


    --
    spooq
    Luke Graham, Mar 7, 2005
    #3
  4. Francis Hwang

    Luke Graham Guest

    On Mon, 7 Mar 2005 17:31:24 +1000, Luke Graham <> wrote:
    > On Mon, 7 Mar 2005 16:03:40 +0900, Francis Hwang <> wrote:
    > > So maybe some of you language wizards out there can help me with this:
    > > Is there a way to define a method that aliases some external method
    > > without assigning that method to a global in the first place?
    > >
    > > The example is the code below: For mockfs/override.rb, I'm undefining
    > > File, but want to keep File.join.
    > >
    > > $join_method = File.method :join
    > > Object.send( :remove_const, :File )
    > > module File
    > > def self.join( *args )
    > > $join_method.call *args
    > > end
    > >
    > > def self.method_missing( symbol, *args )
    > > MockFS.file.send( symbol, *args )
    > > end
    > > end
    > >
    > > This works, but then I'm leaving a global variable just sitting around.
    > > Is there a better way to do this?

    >
    > Im a long way from a wizard, but how about this...
    >
    > OldFile = File.clone
    > Object.send :remove_const, :File
    > module File
    > OldFile = OldFile.clone
    > def self.method_missing(symbol, *args)
    > File.send symbol, *args
    > end
    > end


    Missed the final..

    Object.send :remove_const, :OldFile

    Better name the OldFile in the module something different too :p

    --
    spooq
    Luke Graham, Mar 7, 2005
    #4
  5. Francis Hwang

    Pit Capitain Guest

    Francis Hwang schrieb:
    > Is there a way to define a method that aliases some external method
    > without assigning that method to a global in the first place?
    > ...
    > This works, but then I'm leaving a global variable just sitting around.
    > Is there a better way to do this?


    If you don't mind a local variable then this might help:

    m = Module.new

    class << m
    define_method:)join, &File.method:)join))
    def method_missing(*args)
    args
    end
    end

    Object.send:)remove_const, :File)
    File = m

    p File.join("a", "b") # => "a/b"
    p File.open("x", "y", "z") # => [:eek:pen, "x", "y", "z"]

    Regards,
    Pit
    Pit Capitain, Mar 8, 2005
    #5
    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. Jianli Shen
    Replies:
    1
    Views:
    587
    Victor Bazarov
    Mar 13, 2005
  2. Ark
    Replies:
    1
    Views:
    415
    Chris Torek
    Aug 7, 2004
  3. Jon Slaughter
    Replies:
    4
    Views:
    449
    Jon Slaughter
    Oct 26, 2005
  4. T.G.
    Replies:
    1
    Views:
    134
    John Wilger
    Nov 4, 2005
  5. Mellow Crow
    Replies:
    6
    Views:
    418
    Richard Cornford
    Nov 4, 2005
Loading...

Share This Page