Map & Collect in Ruby

Map & Collect in the Enumerable mixin do the same thing, we can see this in the official Ruby documentation. #collect | #map


This points to both collect and map calling the enum_collect function in the underlying c code.

I wanted to dig one step further and see where and how map and collect get defined to call this enum_collect function.

The enum_collect function is defined in the enum.c class. In the very bottom of this class is where we can find how these two methods are bound to the enum_collect function.

    rb_define_method(rb_mEnumerable, "collect", enum_collect, 0);
    rb_define_method(rb_mEnumerable, "map", enum_collect, 0);

definition of collect | definition of map

So we can say with certainty map and collect can be used interchangeably with no differences in implementation or speed.

Now why these both exist and not just one is speculated on, but no real official answer.