I saw that they were documented together here. Are they the same thing? Why does Ruby have so many aliases (such as map/collect for arrays)? Thanks a lot.
3 Answers
Yes, and it's also called fold
in many other programming languages and in Mathematics. Ruby aliases a lot in order to be intuitive to programmers with different backgrounds. If you want to use #length
on an Array
, you can. If you want to use #size
, that's fine too!

- 2,441
- 1
- 16
- 20
-
4If you want to create your own aliases, you may be interested in [alias_method](http://ruby-doc.org/core-2.0.0/Module.html#method-i-alias_method). – Nick McCurdy Oct 25 '13 at 02:16
-
25#length and #count are not equivalent in Ruby. #length and #size are. – Quolonel Questions May 06 '14 at 16:33
-
103.length gives NoMethodError; 3.size returns 4. Obviously 3 isn't an array, but beware of duck typing! (3_000_000_000_000_000.size returns 8 and 3_000_000_000_000_000_000_000_000_000_000_000_000_000_000.size returns 20; it seems to be the amount of memory it takes up.) – ChrisPhoenix Oct 15 '14 at 01:39
-
19hm, I think the current Ruby docs http://ruby-doc.org/core-2.2.3/Enumerable.html#method-i-reduce might be better if it just says the same as `inject` so that users don't have to read the two description and try to figure out if they are the same... doesn't this go with the DRY principle? – nonopolarity Dec 30 '15 at 19:39
-
3@太極者無極而生, good point, I was confused by this just as you prediced – neontapir Jan 13 '16 at 21:07
-
@Quolonel Questions, #length and #size are aliases, but #count (without any arguments) is, essentially, the same. – traday Mar 09 '16 at 01:05
-
3+1 on the documentation had both inject and reduce together with no explanation, would be clearer to say that they are equivalent. I googled because I was confused and ended up here. – Homan Mar 25 '16 at 00:13
-
Err, why? This seems ridiculous – Jay Jun 08 '16 at 22:29
-
Is this equivalent to Haskell's foldl? – stephanmg May 24 '19 at 13:38
More recent versions of the documentation of Enumerable#reduce
specify it explicitly:
The
inject
andreduce
methods are aliases. There is no performance benefit to either.

- 68,258
- 9
- 99
- 134
-
1i like to separate them based on how it's used, obviously just for semantics. if its a proc (`&:+`), reduce, if it's a block, inject – TheRealMrCrowley Jan 08 '18 at 20:12
Are they the same thing?
Yes, aliases run the exact same code in the end.
Why does Ruby have so many aliases (such as map/collect for arrays)?
It boils down to the language's approach
Different languages have different approaches, I tried to visualize it here:
Ruby does it in favor of developer productivity. Basically, by having aliases you give programmers from different programming languages and human languages backgrounds to write code more intuitively.
However, they can also help your code's clarity because some things may have different semantic possibilities like the method midnight()
can also be expressed as start_of_day
or end_of_day
. Those can be more clear depending on the context.
By the way, some programmers use inject
and reduce
to differentiate between different semantic situations too.

- 1,020
- 13
- 28