Last active
April 3, 2018 11:43
-
-
Save mykiy/a882e3b1e2e0fd3c3ee50cce4cec5667 to your computer and use it in GitHub Desktop.
reduce block in ruby
This file contains hidden or 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
| #reduce | |
| def sum(*args) #sum method accepts many number of arguments that method passed | |
| args.reduce(0, :+) #reduce will reduce the array in single number, 0 is the initial value and + tells the reduce method to perform addition | |
| end | |
| sum(1,2,3,4) | |
| => | |
| 10 | |
| ----- | |
| multiplication | |
| def mul(*args) | |
| args.reduce(1, :*) | |
| end | |
| mul(2,3,4,5) | |
| => | |
| 120 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment