Created
June 10, 2014 02:42
-
-
Save bsa7/cbc9d2fc883fcbd2ad32 to your computer and use it in GitHub Desktop.
Ruby inject
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
n=10 | |
(1..n).inject(:*) || 1 | |
## 1 ######### 2 #### 3 | |
#1. this is a enum object | |
#2. Do an operation (*) with accumulated result and next element - 1..3: res = 1 * 2 * 3 | |
#3. this is a foolprof - if an enum object is nil, result alway be the 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment