Skip to content

Instantly share code, notes, and snippets.

@maxjacobson
Last active December 29, 2015 00:09
Show Gist options
  • Save maxjacobson/7584201 to your computer and use it in GitHub Desktop.
Save maxjacobson/7584201 to your computer and use it in GitHub Desktop.
I was always pet peeved that Array#second didn't exist when Array#first and Array#last do wouldn't this be fun? haha
# gem install numbers_in_words
require 'numbers_in_words'
require 'numbers_in_words/duck_punch'
class Array
def method_missing(m)
self[m.to_s.gsub(/\_/, ' ').in_numbers - 1]
end
end
nums = (1..100).to_a
puts nums.first #=> 1
puts nums.second #=> 2
puts nums.third #=> 3
puts nums.fourth #=> 4
puts nums.thirty_five #=> 35
puts nums.thirty_fifth #=> 30 (bug)
# I just want to acknowledge that I'm using `#=>` incorrectly. That's supposed to indicate the return value, but I'm using it to indicate the output.
@maxjacobson
Copy link
Author

@cjlwired thanks for breaking it down btw, I never would have thought it was leaning on active support if you hadn't dug into the source <3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment