David Vallner
1/12/2006 11:36:00 AM
On Thu, 12 Jan 2006 12:08:05 +0100, Robert Klemme <bob.news@gmx.net> wrote:
> David Vallner wrote:
>> Phrogz wrote:
>>
>>> Christer Nilsson wrote:
>>>
>>>
>>>> class SafeArray < DelegateClass(Array)
>>>> def [](index)
>>>> fetch index
>>>> end
>>>> end
>>>>
>>>>
>>>
>>> Or, even easier:
>>>
>>> class SafeArray < DelegateClass(Array)
>>> alias_method :[], :fetch
>>> end
>>>
>>>
>> Groan at the different argument order of the alias statement and
>> #alias_method.
>
>>> class Foo
>>> def bar() "bar" end
>>> alias :b :bar
>>> end
> => nil
>>> Foo.new.b
> => "bar"
>>> class Bar
>>> def foo() "foo" end
>>> alias_method :f, :foo
>>> end
> => Bar
>>> Bar.new.f
> => "foo"
>
> It seems to me order is the same: first the new name then the old name.
> Am I missing something here?
>
> Kind regards
>
> robert
>
>
My bad. However, it seems alias_method silently lets me use an undefined
method name as the old name, which got me confused:
irb(main):001:0> class Foo
irb(main):002:1> def bar
irb(main):003:2> puts "BAR"
irb(main):004:2> end
irb(main):005:1> alias :bar :bar1
irb(main):006:1> alias_method :bar, :bar2
irb(main):007:1> end
NameError: undefined method `bar1' for class `Foo'
from (irb):5
irb(main):008:0> Foo.new.bar
BAR
=> nil
irb(main):009:0> Foo.new.bar1
NoMethodError: undefined method `bar1' for #<Foo:0x10295848>
from (irb):9
irb(main):010:0> Foo.new.bar2
NoMethodError: undefined method `bar2' for #<Foo:0x102934f0>
from (irb):10
David Vallner