How to alias a Module method? 
Author Message
 How to alias a Module method?

Hi:

I have a module with a module method written as:

module MyModule

  def self.old
    # code here
  end#sef.old

end#module MyModule

I would like to alias this method.
However, this does NOT work:

module MyModule

  def self.old
    # code here
  end#sef.old
  aliase :self.new :self.old

end#module MyModule

How is this done?

Thanks
--
Jim Freeze
Today is a fine day for Ruby programming.
www.freeze.org



Sun, 04 Jul 2004 23:32:17 GMT  
 How to alias a Module method?

Oops!

Quote:
> module MyModule

>   def self.old
>     # code here
>   end#sef.old
>   aliase :self.new :self.old

---------^
Typo in this email only
Should read:

Quote:
>   alias :self.new :self.old

--
Jim Freeze
Today is a fine day for Ruby programming.
www.freeze.org


Sun, 04 Jul 2004 23:37:45 GMT  
 How to alias a Module method?
Hi --

Quote:

> Hi:

> I have a module with a module method written as:

> module MyModule

>   def self.old
>     # code here
>   end#sef.old

> end#module MyModule

> I would like to alias this method.
> However, this does NOT work:

> module MyModule

>   def self.old
>     # code here
>   end#sef.old
>   aliase :self.new :self.old

> end#module MyModule

You have to do the aliasing in the context of the class of MyModule:

  module MyModule
    def self.old
      puts "hi"
    end
    class << self
    alias :oldold :old
    end
  end
  MyModule.old     # hi
  MyModule.oldold  # hi

You could even do:

  module MyModule
    class << self
    def old
      puts "hi"
    end
    alias :oldold :old
    end
  end

(I'm sort of assuming that you don't really want to alias the method
to 'new' -- ?)

David

--
David Alan Black


Web:  http://pirate.shu.edu/~blackdav



Sun, 04 Jul 2004 23:42:58 GMT  
 How to alias a Module method?

J> I would like to alias this method.

 Same response than for attr_*

J> module MyModule

J>   def self.old
J>     # code here
J>   end#sef.old

     class << self
        alias new old
     end
  end

Guy Decoux



Sun, 04 Jul 2004 23:42:58 GMT  
 How to alias a Module method?

Quote:

> module MyModule

>   def self.old
>     # code here
>   end#sef.old
>   aliase :self.new :self.old

> end#module MyModule

Heh - two answers in a row with the same technique!

You could do

  irbsh(main):008:0> module A
  irbsh(main):009:1>   def self.old
  irbsh(main):010:2>     puts 123
  irbsh(main):011:2>   end
  irbsh(main):015:1>   class <<self
  irbsh(main):016:2>     alias :sss :old
  irbsh(main):017:2>   end
  irbsh(main):018:1> end
  nil
  irbsh(main):019:0> A.old
  123
  nil
  irbsh(main):020:0> A.sss
  1

Dave



Sun, 04 Jul 2004 23:42:58 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. RCR: Stack, Queue alias methods in Array

2. alias/method redfinition

3. Module#include and module/class methods

4. Aliases for class methods

5. Aliases for class methods

6. How do I alias a class method?

7. How do I alias a class method?

8. alias method

9. To Alias or not to Alias

10. Why not alias an alias?

11. alias component(Not alias language construct)

12. getting to methods from modules

 

 
Powered by phpBB® Forum Software