Skip to content

Instantly share code, notes, and snippets.

@wojtekmach
Created August 10, 2011 14:58
Show Gist options
  • Save wojtekmach/1137015 to your computer and use it in GitHub Desktop.
Save wojtekmach/1137015 to your computer and use it in GitHub Desktop.
Ruby one-liners
# 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