How to alias a class method

D

Dmitry V. Sabanin

Hi!

I've run into problem aliasing a class method, i.e.
I have:
class Abc
self.get_abc arg
...
end
end
And later I need to this method, with saving of
original implementation.
Following code is not working:
alias :eek:rig_get_abc, :get_abc
 
T

ts

D> Following code is not working:
D> alias :eek:rig_get_abc, :get_abc

you need to be in the singleton class to make the alias

svg% cat b.rb
#!/usr/bin/ruby
class Abc
def self.get_abc arg
puts "abc"
end
class << self
alias orig_get_abc get_abc
end
end

Abc.orig_get_abc(12)
svg%

svg% b.rb
abc
svg%



Guy Decoux
 
D

Dmitry V. Sabanin

you need to be in the singleton class to make the alias

svg% cat b.rb
#!/usr/bin/ruby
class Abc
def self.get_abc arg
puts "abc"
end
class << self
alias orig_get_abc get_abc
end
end

Abc.orig_get_abc(12)
svg%

svg% b.rb
abc
svg%

Guy Decoux

Very nice, thank you! :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top