From: George Ogata Date: 2007-01-31T20:48:45+09:00 Subject: Re: alias callback On 1/30/07, Trans wrote: > How does one reflect on alias definitions. Using #method_added only > gives you the name of the alias being defined --as if it were a new > method. And it's not possible to override 'alias' b/c it's not a real > method. > > Ideas? UnboundMethod#== returns true for aliases, so hopefully: class Module def alias?(name) name = name.to_s instance_method = instance_method(name) (instance_methods + private_instance_methods).any? do |n| n != name && instance_method(n) == instance_method end end end Bit of a test: require 'test/unit' class AliasTest < Test::Unit::TestCase def test_by_alias klass = Class.new do def foo end def bar end alias baz foo end assert klass.alias?(:foo) assert !klass.alias?(:bar) assert klass.alias?(:baz) end def test_by_alias_method klass = Class.new do def foo end def bar end alias_method :baz, :foo end assert klass.alias?(:foo) assert !klass.alias?(:bar) assert klass.alias?(:baz) end def test_across_inheritance klass = Class.new{alias foo object_id} assert klass.alias?(:foo) assert klass.alias?(:object_id) end def test_nonalias klass = Class.new{def foo; end} assert !klass.alias?(:foo) end def test_builtin assert Array.alias?(:size) assert Array.alias?(:length) assert !Array.alias?(:clear) end end