Alias for Module Method

Hi,

Is there a way to have an alias for module method? This code does not
work:

---------------------

module MyModule
def MyModule.func
puts ‘Hello’
end

alias funcAlias func
end

MyModule.funcAlias

---------------------

I am using Ruby 1.9.2.

Thanks,

Bill

Hey,

Yeah, in your code Ruby tries to alias an instance method called :func
defined in the module. Since it doesn’t exist, it doesn’t work. Alias
and alias_method only work on instance methods, but luckily singleton
methods like :func in your example are really instance methods defined
in the singleton class of MyModule.

module MyModule
def MyModule.func
puts ‘Hello’
end

class << self # or, class << MyModule
alias funcAlias func
end
end

MyModule.funcAlias

Take care,

-Luke

Wow, it really works! Thanks a lot for the singleton class.

Regards,

Bill

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs