Skip to content

Instantly share code, notes, and snippets.

@jeremyruppel
Last active December 11, 2015 17:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeremyruppel/4631950 to your computer and use it in GitHub Desktop.
Save jeremyruppel/4631950 to your computer and use it in GitHub Desktop.
[ :a, :b, :c ].inject( [ [] ] ) { |arr, n| arr.push( arr.last.dup << n ) }.reverse
# => [[:a, :b, :c], [:a, :b], [:a], []]
class Array
def countdown
inject( [ [] ] ) { |arr, n| arr.push( arr.last.dup << n ) }.reverse
end
end
query = {
:country_code => user.country_code,
:region_code => user.region_code,
:zip_code => user.zip_code
}
[ :country_code, :region_code, :zip_code ].countdown.find do |constraints|
number = TwilioNumber.shared.available.where( query.slice( constraints ) ).first_or_create
number.valid?
end
# >> [:country_code, :region_code, :zip_code]
# >> [:country_code, :region_code]
# >> [:country_code]
# >> []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment