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