Created
August 10, 2011 14:58
-
-
Save wojtekmach/1137015 to your computer and use it in GitHub Desktop.
Ruby one-liners
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
# factorial | |
(1..5).inject(&:*) # => 120 | |
# reverse | |
(1..5).inject([]) { |val, v| [v] + val } # => [5, 4, 3, 2, 1] | |
# fibonacci numbers | |
(1...5 - 1).inject([0, 1]) { |val, v| val << val[-2] + val[-1] } # => [0, 1, 1, 2, 3] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment