Polyglot CheatSheet - Range

Updated: 2018-06-18

Ruby

>> ages = 18..30
=> 18..30
>> ages.entries
=> [18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30]
>> ages.include?(25)
=> true
>> ages.include?(33)
=> false
  • ..: inclusive,
  • ...: exclusive
>> (1..5).to_a
=> [1, 2, 3, 4, 5]
>> (1...5).to_a
=> [1, 2, 3, 4]

use === to test if a value is included in the range

>> (1..5) === 5
=> true
>> (1...5) === 5
=> false

Or use Range.new

>> Range.new(1,5).to_a
=> [1, 2, 3, 4, 5]
>> Range.new(1,5,true).to_a
=> [1, 2, 3, 4]