Skip to main content

endless_and_beginless_ranges

🤯 Master Endless and Beginless Ranges​

Ruby 2.6 introduced endless (start..) and beginless (..end) ranges, which can be used for elegant threshold checks, slicing endless collections, and lazy enumerations. They’re especially powerful when combined with methods like lazy, take_while, or drop_while.

# Beginless range to filter early entries
data = [5, 10, 15, 20, 25]
early = data.drop_while { |n| n < 15 } # [15, 20, 25]

# Endless range to limit values
numbers = (1..) # infinite Enumerator
first_ten = numbers.take(10) # [1,2,3,…,10]

# Usage in slicing
array = %w[a b c d e f]
slice = array[..3] # ["a","b","c","d"]
slice2 = array[2..] # ["c","d","e","f"]