Is it possible to find out if an identifier is a method alias?

def method; end

=> nil

alias :method_alias :method

=> nil

defined? method

=> "method"

defined? method_alias

=> "method"

The above makes perfect sense, but is there a way to determine if an
identifier is an alias?

I don't have any practical use for such functionality. I'm purely
curious if there is a way to do it.

Thanks,
Ammar

class Foo
   def alpha; end
   def beta; end
   alias gamma alpha
end

Foo.instance_method(:alpha) == Foo.instance_method(:beta) # => false
Foo.instance_method(:alpha) == Foo.instance_method(:gamma) # => true

Regards,

Dan

···

On 10/14/10 4:48 PM, Ammar Ali wrote:

def method; end

=> nil

alias :method_alias :method

=> nil

defined? method

=> "method"

defined? method_alias

=> "method"

The above makes perfect sense, but is there a way to determine if an
identifier is an alias?

I don't have any practical use for such functionality. I'm purely
curious if there is a way to do it.

Thanks Daniel. That's helpful.

Regards,
Ammar

···

On Fri, Oct 15, 2010 at 2:17 AM, Daniel Berger <djberg96@gmail.com> wrote:

class Foo
def alpha; end
def beta; end
alias gamma alpha
end

Foo.instance_method(:alpha) == Foo.instance_method(:beta) # => false
Foo.instance_method(:alpha) == Foo.instance_method(:gamma) # => true

But which of the two is the alias?

robert

···

On Fri, Oct 15, 2010 at 1:03 PM, Ammar Ali <ammarabuali@gmail.com> wrote:

On Fri, Oct 15, 2010 at 2:17 AM, Daniel Berger <djberg96@gmail.com> wrote:

class Foo
def alpha; end
def beta; end
alias gamma alpha
end

Foo.instance_method(:alpha) == Foo.instance_method(:beta) # => false
Foo.instance_method(:alpha) == Foo.instance_method(:gamma) # => true

Thanks Daniel. That's helpful.

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Remains unknown, but I'm a little closer than I was.

Thanks,
Ammar

···

On Fri, Oct 15, 2010 at 3:31 PM, Robert Klemme <shortcutter@googlemail.com> wrote:

On Fri, Oct 15, 2010 at 1:03 PM, Ammar Ali <ammarabuali@gmail.com> wrote:

On Fri, Oct 15, 2010 at 2:17 AM, Daniel Berger <djberg96@gmail.com> wrote:

class Foo
def alpha; end
def beta; end
alias gamma alpha
end

Foo.instance_method(:alpha) == Foo.instance_method(:beta) # => false
Foo.instance_method(:alpha) == Foo.instance_method(:gamma) # => true

Thanks Daniel. That's helpful.

But which of the two is the alias?

I don't know of a way to tell. I asked about this a while back:

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/196974

Regards,

Dan

···

On 10/15/10 6:31 AM, Robert Klemme wrote:

On Fri, Oct 15, 2010 at 1:03 PM, Ammar Ali<ammarabuali@gmail.com> wrote:

On Fri, Oct 15, 2010 at 2:17 AM, Daniel Berger<djberg96@gmail.com> wrote:

class Foo
  def alpha; end
  def beta; end
  alias gamma alpha
end

Foo.instance_method(:alpha) == Foo.instance_method(:beta) # => false
Foo.instance_method(:alpha) == Foo.instance_method(:gamma) # => true

Thanks Daniel. That's helpful.

But which of the two is the alias?

Hi --

class Foo
def alpha; end
def beta; end
alias gamma alpha
end

Foo.instance_method(:alpha) == Foo.instance_method(:beta) # => false
Foo.instance_method(:alpha) == Foo.instance_method(:gamma) # => true

Thanks Daniel. That's helpful.

But which of the two is the alias?

I could be wrong, and haven't dug into the code, but I've always had the
impression that (by design) there's no difference between different ways
to refer to a method. It's not that the alias is a kind of nickname for
the "real" name; it *is* (one of) the real name(s).

In 1.9.2, anyway, #instance_methods seems to list methods in the order
they were created/aliased, though I don't know whether this is
guaranteed:

ruby-1.9.2-p0 > class C; def x; end; alias a x; def b; end; def d; end;
alias c d; def f; end; def e; end; end
=> nil ruby-1.9.2-p0 > C.instance_methods
=> [:x, :a, :b, :d, :c, :f, :e, ... ]

David

···

On Fri, 15 Oct 2010, Robert Klemme wrote:

On Fri, Oct 15, 2010 at 1:03 PM, Ammar Ali <ammarabuali@gmail.com> wrote:

On Fri, Oct 15, 2010 at 2:17 AM, Daniel Berger <djberg96@gmail.com> wrote:

--
David A. Black, Senior Developer, Cyrus Innovation Inc.

   The Ruby training with Black/Brown/McAnally
   Compleat Stay tuned for next event!
   Rubyist http://www.compleatrubyist.com

I could be wrong, and haven't dug into the code, but I've always had the
impression that (by design) there's no difference between different ways
to refer to a method. It's not that the alias is a kind of nickname for
the "real" name; it *is* (one of) the real name(s).

You inspired me to take a look and you are right on. alias just
creates a new entry for the same method into the target's method
table. There is nothing in the entry that distinguishes it as an
alias.

In 1.9.2, anyway, #instance_methods seems to list methods in the order
they were created/aliased, though I don't know whether this is
guaranteed:

The methods are always copied in the order they were defined (using
rb_ary_push). I guess that's guaranteed enough for a hack. Based on
the equality Daniel pointed out and the ordering you did, I took a jab
at it:

  631102’s gists · GitHub

Thanks,
Ammar

···

On Sat, Oct 16, 2010 at 4:19 PM, David A. Black <dblack@rubypal.com> wrote:

<snip> In 1.9.2, anyway, #instance_methods seems to list methods in the order

they were created/aliased, though I don't know whether this is
guaranteed:

ruby-1.9.2-p0 > class C; def x; end; alias a x; def b; end; def d; end;
alias c d; def f; end; def e; end; end
=> nil ruby-1.9.2-p0 > C.instance_methods
=> [:x, :a, :b, :d, :c, :f, :e, ... ]

David

Maybe it is best to say that somehow it really does not matter, I
would be curious to see where it matters. The following, completely
useless code, somehow demonstrates this:

Class::new do
  def a; end
  alias_method :x, :a
  remove_method :a
  alias_method :a, :x
  p instance_methods( false )
end

and the question I would like to ask is, what is :a in this case? An
alias to itself after having been removed I guess ;). But I prefer
David's notation above which is the perfect explanation of what is
really happening here.(1)
Hopefully that kind of manipulation can be safely ignored when talking
about "definition order".

(1) Maybe with the exception that remove_method "really" means, remove
the entry with this name pointing to the method, but by all means let
us keep the name :).

Cheers
Robert

···

On Sat, Oct 16, 2010 at 3:19 PM, David A. Black <dblack@rubypal.com> wrote:

--
David A. Black, Senior Developer, Cyrus Innovation Inc.

The Ruby training with Black/Brown/McAnally
Compleat Stay tuned for next event!
Rubyist http://www.compleatrubyist.com

--
There are worse things than having people misunderstand your work. A
worse danger is that you will yourself misunderstand your work.
-- Paul Graham

If I understand how this works correctly, then remove_method is only
removing the :a entry for the method from the object's table, but
since there is another "reference" to the method definition (the b
alias, which is just another entry to same method), the body of the
method is not removed. Adding :a again is simply adding a new entry
for the same method. If both :a and :b are removed, then one tries to
add an alias a NameError would be raised.

Regards,
Ammar

···

On Mon, Oct 18, 2010 at 5:56 PM, Robert Dober <robert.dober@gmail.com> wrote:

Maybe it is best to say that somehow it really does not matter, I
would be curious to see where it matters. The following, completely
useless code, somehow demonstrates this:

Class::new do
def a; end
alias_method :x, :a
remove_method :a
alias_method :a, :x
p instance_methods( false )
end

and the question I would like to ask is, what is :a in this case? An
alias to itself after having been removed I guess ;).

That is my understanding too.
Cheers
Robert

···

On Mon, Oct 18, 2010 at 5:20 PM, Ammar Ali <ammarabuali@gmail.com> wrote:

--
There are worse things than having people misunderstand your work. A
worse danger is that you will yourself misunderstand your work.
-- Paul Graham