inheritence of aliases methods - suprise!

Discussion in 'Ruby' started by ara.t.howard@noaa.gov, Feb 6, 2006.

  1. Guest

    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

    --
    happiness is not something ready-made. it comes from your own actions.
    - h.h. the 14th dali lama
    , Feb 6, 2006
    #1
    1. Advertising

  2. Eric Hodel Guest

    On Feb 6, 2006, at 1:06 PM, 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 - - http://segment7.net
    This implementation is HODEL-HASH-9600 compliant

    http://trackmap.robotcoop.com
    Eric Hodel, Feb 6, 2006
    #2
    1. Advertising

  3. On Feb 6, 2006, at 4:06 PM, wrote:

    >
    > 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
    >
    > --
    > 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
    %
    Logan Capaldo, Feb 6, 2006
    #3
  4. Guest

    On Tue, 7 Feb 2006, Logan Capaldo wrote:

    > 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
    > %


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

    regards.

    -a

    --
    happiness is not something ready-made. it comes from your own actions.
    - h.h. the 14th dali lama
    , Feb 6, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roedy Green

    enum aliases

    Roedy Green, Jun 26, 2005, in forum: Java
    Replies:
    5
    Views:
    3,840
    Roedy Green
    Jun 30, 2005
  2. Kevin
    Replies:
    5
    Views:
    2,158
    Jack Klein
    Feb 6, 2004
  3. Capstar
    Replies:
    2
    Views:
    275
    Daniel T.
    Aug 6, 2004
  4. tony summerfelt

    principle of most suprise

    tony summerfelt, Jan 30, 2004, in forum: Ruby
    Replies:
    8
    Views:
    111
  5. Kenneth McDonald
    Replies:
    5
    Views:
    298
    Kenneth McDonald
    Sep 26, 2008
Loading...

Share This Page