Is inject the same thing as reduce in ruby?
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!
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.