Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > aliasing an external method from within a method definition?

Reply
Thread Tools

aliasing an external method from within a method definition?

 
 
Francis Hwang
Guest
Posts: n/a
 
      03-07-2005
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/



 
Reply With Quote
 
 
 
 
Joel VanderWerf
Guest
Posts: n/a
 
      03-07-2005
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.


 
Reply With Quote
 
 
 
 
Luke Graham
Guest
Posts: n/a
 
      03-07-2005
On Mon, 7 Mar 2005 16:03:40 +0900, Francis Hwang <(E-Mail Removed)> 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


 
Reply With Quote
 
Luke Graham
Guest
Posts: n/a
 
      03-07-2005
On Mon, 7 Mar 2005 17:31:24 +1000, Luke Graham <(E-Mail Removed)> wrote:
> On Mon, 7 Mar 2005 16:03:40 +0900, Francis Hwang <(E-Mail Removed)> 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

--
spooq


 
Reply With Quote
 
Pit Capitain
Guest
Posts: n/a
 
      03-08-2005
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") # => [pen, "x", "y", "z"]

Regards,
Pit


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Create references to external scipt files from within an external script file Mellow Crow Javascript 6 11-04-2005 01:16 PM
Re: "Aliasing" an object's __str__ to a different method Jeffrey E. Forcier Python 2 07-24-2005 02:25 PM
"Aliasing" an object's __str__ to a different method Jeffrey E. Forcier Python 7 07-24-2005 12:45 AM
Defining a method vs aliasing it leon breedt Ruby 7 06-26-2005 09:08 PM
Aliasing a singleton method of a Class object Yuri Leikind Ruby 7 09-12-2004 09:50 AM



Advertisments