Forum: Ruby inheritence of aliases methods - suprise!

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Cb48ca5059faf7409a5ab3745a964696?d=identicon&s=25 unknown (Guest)
on 2006-02-06 22:07
(Received via mailing list)
this suprised me today:

     harp:~ > cat a.rb
     class A
       def assertion
         raise NotImplementedError
       end
       alias_method "assertion?", "assertion"
     end

     class B < A
       def assertion
         true
       end
     end

     B::new.assertion?


     harp:~ > ruby a.rb
     a.rb:3:in `assertion?': NotImplementedError (NotImplementedError)
             from a.rb:14


this only way i can seem to make this work is through some
self::inherited
hacks or to actually define assertion? in the base class.  is there no
clean
way to inherit aliases?

regards.

-a
58479f76374a3ba3c69b9804163f39f4?d=identicon&s=25 Eric Hodel (Guest)
on 2006-02-06 22:25
(Received via mailing list)
On Feb 6, 2006, at 1:06 PM, ara.t.howard@noaa.gov wrote:

> this suprised me today:

$ parse_tree_show -
     class A
       def assertion
         raise NotImplementedError
       end
       alias_method "assertion?", "assertion"
     end

     class B < A
       def assertion
         true
       end
     end
[[:class, :B, :A, [:defn, :assertion, [:scope, [:block, [:args],
[:true]]]]],
[:class,
   :A,
   :Object,
   [:defn,
    :assertion,
    [:scope,
     [:block,
      [:args],
      [:fcall, :raise, [:array, [:const, :NotImplementedError]]]]]],
   [:defn,
    :"assertion?",
    [:fbody,
     [:scope,
      [:block,
       [:args],
       [:fcall, :raise, [:array, [:const, :NotImplementedError]]]]]]]]]

> this only way i can seem to make this work is through some
> self::inherited
> hacks or to actually define assertion? in the base class.  is there
> no clean
> way to inherit aliases?

alias copies the method, it doesn't make a pointer to the method.

--
Eric Hodel - drbrain@segment7.net - http://segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 Logan Capaldo (Guest)
on 2006-02-06 22:25
(Received via mailing list)
On Feb 6, 2006, at 4:06 PM, ara.t.howard@noaa.gov wrote:

>
>     a.rb:3:in `assertion?': NotImplementedError (NotImplementedError)
>
> -a
>
> --
> happiness is not something ready-made.  it comes from your own
> actions.
> - h.h. the 14th dali lama
>

This makes sense, If you consider the common idiom of alias'ing a
method to wrap additional functionality around it. If alias(_method)
didn't work like that you couldn't use it like this. alias_method
doesn't work like assignment in ruby, its more like it creates a new
method with the same source as the original method.

As a poorly conceived alternative:

% cat a.rb
class Class
   def shallow_alias(new_name, current_name)
     self.module_eval <<-END
     def #{new_name}(*args, &block)
       #{current_name}(*args, &block)
     end
     END
   end
end

class A
   def assertion
     raise NotImplementedError
   end
   shallow_alias "assertion?", "assertion"
end

class B < A
   def assertion
     true
   end
end

B.new.assertion?

% ruby a.rb
%
Cb48ca5059faf7409a5ab3745a964696?d=identicon&s=25 unknown (Guest)
on 2006-02-06 22:59
(Received via mailing list)
On Tue, 7 Feb 2006, Logan Capaldo wrote:

>  def shallow_alias(new_name, current_name)
>    raise NotImplementedError
> B.new.assertion?
>
> % ruby a.rb
> %

yeah - this is pretty much what i did.  for some reason i had always
assume
that alias_method worked this way.

regards.

-a
This topic is locked and can not be replied to.