inheritence of aliases methods - suprise!

A

ara.t.howard

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
 
E

Eric Hodel

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.
 
L

Logan Capaldo

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

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

ara.t.howard

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top