Created
November 14, 2009 05:34
-
-
Save closer/234396 to your computer and use it in GitHub Desktop.
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
| class Array | |
| def chain m | |
| r = shift | |
| each{|n| r = r.send(m, n)} | |
| r | |
| end | |
| end | |
| class Range | |
| def chain m | |
| to_a.chain m | |
| end | |
| end |
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
| #= Practice::factorial | |
| # 1 *2 *3 *.. *n | |
| require "chain" | |
| module Practice | |
| def factorial1 n | |
| n == 1 ? 1 : n * factorial1(n - 1) | |
| end | |
| def factorial2 n | |
| (1..n).chain :* | |
| end | |
| end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment