Created
October 17, 2010 18:27
-
-
Save ammar/631102 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Object | |
# This does not work on ruby 1.8.x because the methods are not returned | |
# in the order they were defined. Also, under 1.8, the method names are | |
# returned as strings, not symbols. | |
# Identifies method aliases in the class and returns an array with | |
# their symbols. This uses #methods, so it includes all methods | |
# accessible in the object's ancestors. | |
def alias_methods | |
aliases = [] | |
ms = methods | |
ms.each_with_index do |m, i| | |
(i+1).upto(ms.length-1) do |t| | |
next if aliases.include? m | |
aliases << ms[t] if method(m) == method(ms[t]) | |
end | |
end | |
aliases | |
end | |
# Returns an array of the aliases defined for the given method, | |
# returns an empty array if no aliases are defined. | |
def method_aliases(m) | |
aliases = [] | |
alias_methods.each do |a| | |
aliases << a if method(m) == method(a) | |
end | |
aliases | |
end | |
# Returns the original method for the given alias, return nil if the | |
# alias does not resolve to a method in the object. | |
def resolve_alias(a) | |
return unless is_alias?(a) | |
methods.each {|m| return(m) if method(m) == method(a)} | |
end | |
# Tests if the given method (symbol) is an alias to another | |
# method in the object. | |
def is_alias?(m) | |
alias_methods.include? m | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "test/unit" | |
require File.expand_path '../alias_methods', __FILE__ | |
class Foo | |
def m_1; end | |
def m_2; end | |
def m_3; end | |
alias :am_1 :m_1 | |
alias :am_2 :m_2 | |
alias :am_3 :m_2 | |
alias :am_4 :m_3 | |
alias :am_5 :m_3 | |
alias :am_6 :m_3 | |
end | |
class TestMethodAliases < Test::Unit::TestCase | |
def test_alias_methods | |
o_aliases = Object.new.alias_methods | |
f_aliases = Foo.new.alias_methods | |
assert_equal( 6, (f_aliases - o_aliases).length ) | |
assert_equal( true, f_aliases.include?(:am_2) ) | |
end | |
def test_method_aliases | |
aliases = Foo.new.method_aliases(:m_3) | |
assert_equal( 3, aliases.length ) | |
assert_equal( true, aliases.include?(:am_6) ) | |
end | |
def test_resolve_alias | |
assert_equal( :m_2, Foo.new.resolve_alias(:am_3) ) | |
end | |
def test_resolve_alias_undefined | |
assert_equal( nil, Foo.new.resolve_alias(:am_9) ) | |
end | |
def test_is_alias | |
assert_equal( true, Foo.new.is_alias?(:am_3) ) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment