The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Synonyms vs Aliases

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Daniel Berger

Posts: 1383
Nickname: djberg96
Registered: Sep, 2004

Daniel Berger is a Ruby Programmer who also dabbles in C and Perl
Synonyms vs Aliases Posted: Nov 9, 2007 6:36 AM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Daniel Berger.
Original Post: Synonyms vs Aliases
Feed Title: Testing 1,2,3...
Feed URL: http://djberg96.livejournal.com/data/rss
Feed Description: A blog on Ruby and other stuff.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Daniel Berger
Latest Posts From Testing 1,2,3...

Advertisement
I originally posted this on ruby-core but thought I'd blog it, too. I've posted before about the problem of determining which methods were "real" and which were aliases. One problem, as I mention in that link, is that that Ruby mostly uses synonyms instead of aliases in the source code.

For example, Array#map and Array#collect are synonyms but not true aliases, because of the way they're declared in array.c:
rb_define_method(rb_cArray, "collect", rb_ary_collect, 0);
rb_define_method(rb_cArray, "map", rb_ary_collect, 0);

The Array#map method just refers to the same function as Array#collect, but it isn't an actual alias. If it were, it would have been declared like so:
rb_define_alias(rb_cArray, "map", "collect");

At this point you're probably thinking, "What's the big deal?". The big deal, in addition to making it impossible to calculate a given class' aliases at runtime, is that it screws up Method#==.

Consider the following snippet:
m1 = Array.instance_method(:size)
m2 = Array.instance_method(:length)
m1 == m2 # => true

Here m1 and m2 are equal because Array#size and Array#length are true aliases (one of five in the core Ruby classes). Now watch what happens if we compare synonyms:
m3 = Array.instance_method(:map)
m4 = Array.instance_method(:collect)
m3 == m4 # => false

They aren't equal from Ruby's point of view because they aren't true aliases.

Read: Synonyms vs Aliases

Topic: Last Night's Dream Previous Topic   Next Topic Topic: Minimize parentheses in Textmate

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use