[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How to alias a class method

Dmitry V. Sabanin

9/16/2003 3:37:00 PM

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 :orig_get_abc, :get_abc

--
sdmitry -=- Dmitry V. Sabanin
MuraveyLabs.


2 Answers

ts

9/16/2003 3:43:00 PM

0

>>>>> "D" == Dmitry V Sabanin <sdmitry@lrn.ru> writes:

D> Following code is not working:
D> alias :orig_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

Dmitry V. Sabanin

9/17/2003 2:31:00 AM

0

On Tuesday 16 September 2003 23:43, ts wrote:
> 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! :)
--
sdmitry -=- Dmitry V. Sabanin
MuraveyLabs.