[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Aliasing class methods

Bryan Richardson

1/18/2008 11:52:00 AM

Hello all,

I'm having trouble aliasing class methods. I've tried the following,
but got errors saying the method being aliased doesn't exist for class
"Class".

Class Foo
class << self
alias bar bla
end

def self.bla
puts "Hello"
end
end

Foo.bar

Any suggestions? Thanks! -- BTR

1 Answer

Stefano Crocco

1/18/2008 12:16:00 PM

0

Alle Friday 18 January 2008, Bryan Richardson ha scritto:
> Hello all,
>
> I'm having trouble aliasing class methods. I've tried the following,
> but got errors saying the method being aliased doesn't exist for class
> "Class".
>
> Class Foo
> class << self
> alias bar bla
> end
>
> def self.bla
> puts "Hello"
> end
> end
>
> Foo.bar
>
> Any suggestions? Thanks! -- BTR

The alias statement is executed as soon as it's found, i.e before Foo.bla is
defined. Since you can't alias something which doesn't exist, you get an
error. Try switching the order:

Class Foo
class << self
alias bar bla
end

def self.bla
puts "Hello"
end
end

Foo.bar

Stefano