An object that handles a range of values. For example, when expressing 1 to 3, it is expressed as follows.
irb(main):001:0> 1..3
=> 1..3
irb(main):002:0> (1..3).class
=> Range
You can also convert it to an array using a method called to_a.
irb(main):003:0> (1..3).to_a
=> [1, 2, 3]
irb(main):004:0> (1..3).to_a.class
=> Array
By the way, if you set 3 dots, you can create a Range object that does not include the last value.
irb(main):006:0> (1...3).to_a
=> [1, 2]
You can also create a Range object for the alphabet.
irb(main):011:0> ('a'..'e').to_a
=> ["a", "b", "c", "d", "e"]
Like an array, each method can be used to expand and retrieve each value.
irb(main):016:0> (1..5).each { |i| puts "#{i}" }
1
2
3
4
5
=> 1..5
Recommended Posts