tags:

views:

73

answers:

2

I'm familiarizing myself with Ruby and it's the first time I see synonyms for methods in the standard library. For now I've found the ones in the Enumerable module:

collect <-> map
detect <-> find
include <-> member
etc.

Probably other modules contains synonymic methods too. Why are they there? Is it some kind of backward compatibility? Or is it a feature of the language, so the programmer can choose between different options depending on the context to make the program look like plain English?

+2  A: 

I think it is made to be compatible with other programming languages' word usage. Ruby is based on Smalltalk. In Smalltalk, collect means map. However, most programming languages in the world uses map, so Ruby added map to make other people feel familiar.

See http://en.wikipedia.org/wiki/Map_function for more details.

SHiNKiROU
+2  A: 

Synonyms build on the familiarity of users coming from different scripting or programming backgrounds with existing nomenclature (e.g. map, grep etc. are very familiar to people coming from a Perl background.)

It also sometimes comes in handy, indirectly, to have these synonyms, e.g. in Rails associations, which is also meant to behave like an enumerable, find is overloaded to perform an actual SQL query whereas the detect synonym remains available to do the actual enumerable find/detect.

Cheers, V.

vladr
I didn't get the find/detect part. Are you saying there's a difference between them in some scenarious?
Max
@Max: Yes, there will be a difference in scenarios where one of the methods has been overridden and the other hasn't. For example, if I do `def find(*x) nil end`, then `find` will always fail but `detect` will still work.
Chuck