Square Corner Blog
Published in

Square Corner Blog

Ruby’s New Infinite Range Syntax: (0..)

Heads up, we’ve moved! If you’d like to continue keeping up with the latest technical content from Square please visit us at our new home https://developer.squareup.com/blog

Introducing Ruby 2.6’s Endless Range

42..
#=> 42..nil # yes, this is infinite!
42..Float::INFINITY
#=> 42..Infinity
42.step
#<Enumerator: ...>
42.step.size
#=> Infinity

Usage Examples

[:a, :b, :c, :d, :e][2..]
#=> [:c, :d, :e]
'012345'[2..]
#=> "2345"
[:a, :b, :c, :d, :e][2..-1]
#=> [:c, :d, :e]
'012345'[2..-1]
#=> "2345"
case 2022
when(2030..)
:mysterious_future
when(2020..)
:twenties
when(2010..)
:nowish
else
:ancient_past
end
#=> :twenties
[:a, :b, :c].zip(42..)
#=> [[:a, 42], [:b, 43], [:c, 44]
# instead of:[:a, :b, :c].each.with_index(42).to_a
#=> [[:a, 42], [:b, 43], [:c, 44]]
(:a..).lazy.zip(42..).first(3)
#=> [[:a, 42], [:b, 43], [:c, 44]]
(42..).each { |n| # forever! }

Conclusion

--

--

Buying and selling sound like simple things - and they should be. Somewhere along the way, they got complicated. At Square, we're working hard to make commerce easy for everyone.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store