Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > inheritence of aliases methods - suprise!

Reply
Thread Tools

inheritence of aliases methods - suprise!

 
 
ara.t.howard@noaa.gov
Guest
Posts: n/a
 
      02-06-2006

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


 
Reply With Quote
 
 
 
 
Eric Hodel
Guest
Posts: n/a
 
      02-06-2006
On Feb 6, 2006, at 1:06 PM, http://www.velocityreviews.com/forums/(E-Mail Removed) 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 - (E-Mail Removed) - http://segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com




 
Reply With Quote
 
 
 
 
Logan Capaldo
Guest
Posts: n/a
 
      02-06-2006

On Feb 6, 2006, at 4:06 PM, (E-Mail Removed) 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
%




 
Reply With Quote
 
ara.t.howard@noaa.gov
Guest
Posts: n/a
 
      02-06-2006
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a way to find the class methods of a class, just like'methods' finds the instance methods? Kenneth McDonald Ruby 5 09-26-2008 03:09 PM
enum aliases Roedy Green Java 5 06-30-2005 03:09 PM
thunderbird 0.9 aliases timbo Firefox 2 12-07-2004 06:51 AM
abstract methods and inheritence Capstar C++ 2 08-06-2004 12:24 PM
SQL aliases in Visual C++ 6.0 Kevin C++ 5 02-06-2004 03:06 AM



Advertisments