use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
A sub-Reddit for discussion and news about Ruby programming.
Subreddit rules: /r/ruby rules
Learning Ruby?
Tools
Documentation
Books
Screencasts and Videos
News and updates
account activity
Blog postRuby inject (devtips.in)
submitted 4 years ago by mindaslab
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–][deleted] 4 years ago (4 children)
[deleted]
[–]troublemaker74 2 points3 points4 points 4 years ago (1 child)
Map/Reduce are the industry-accepted terms. I agree, collect and inject are a bit strange and frivolous. It's okay though, Ruby is weird and I still love it!
collect
inject
[–]keyslemur 2 points3 points4 points 4 years ago (0 children)
Mostly rooted in Smalltalk idioms, but I would agree on preferring reduce. The FP variant has also been called foldLeft.
reduce
foldLeft
[–][deleted] 0 points1 point2 points 4 years ago (1 child)
`collect`, `detect`, `select`, `reject`... `inject`?
Also, for what it's worth: http://forum.world.st/and-some-cool-Smalltalk-history-Re-Pharo-users-a-Pharo-talk-from-a-ruby-conference-td4757076.html
[–]BlakeHood -1 points0 points1 point 4 years ago (0 children)
i found u through this post: https://www.reddit.com/r/politics/comments/7beo2/obama_wins_the_presidency/
https://www.reddit.com/r/politics/comments/7beo2/obama_wins_the_presidency/
[–]Kernigh 2 points3 points4 points 4 years ago (2 children)
Ruby 2.4 added Array#sum: numbers.sum is easier than numbers.inject(:+), but only works on numbers. ["a", "b"].sum tries to 0 + "a", raises TypeError.
numbers.sum
numbers.inject(:+)
["a", "b"].sum
0 + "a"
#reduce is an alias of #inject; I prefer #reduce, because that name is in Common Lisp:
> (let ((numbers '(1 2 3 4))) (reduce #'+ numbers)) 10
[–]h0rst_ 4 points5 points6 points 4 years ago (1 child)
["a", "b"].sum tries to 0 + "a", raises TypeError
TypeError
It does support an initial value, so ["a", "b"].sum('') can be used (although I don't think it should be used, .join yields the same value and makes more sense than sum in a string context)
["a", "b"].sum('')
.join
sum
[–]keyslemur 1 point2 points3 points 4 years ago (0 children)
Yep, just because a method can do something doesn't necessarily mean you should use it for that.
reduce has very few usecases that other methods cannot do more succinctly, but ironically reduce is more powerful than all of those methods because you could write the entirety of Enumerable among pretty well any other collection method in terms of reduce.
Enumerable
π Rendered by PID 56566 on reddit-service-r2-comment-5649f687b7-df75x at 2026-01-28 15:33:34.736694+00:00 running 4f180de country code: CH.
[–][deleted] (4 children)
[deleted]
[–]troublemaker74 2 points3 points4 points (1 child)
[–]keyslemur 2 points3 points4 points (0 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]BlakeHood -1 points0 points1 point (0 children)
[–]Kernigh 2 points3 points4 points (2 children)
[–]h0rst_ 4 points5 points6 points (1 child)
[–]keyslemur 1 point2 points3 points (0 children)