Ruby Range
In Ruby, we can get the range of numbers by using (1..20). Just like an array, a range also supports each and do end.
numbers = (1..20)
numbers.each do |number|
puts number
end
Checking if Value is in Range
range = (20..40)
puts range.include? 30 // true
A range can be converted into an array using the to_a method.
range = (1..3)
array = range.to_a
puts array // [1,2,3]
Range supports both double dots and triple dots.
puts (1..3).to_a // [1, 2, 3]
puts(1...3).to_a // [1,2]
puts (1..5).include?(5) // true
puts (1...5).include?(5) // false
puts (1..4).to_a == (1...5).to_a // true
puts (1..4) == (1...5) // false