I’m sorry if I may have missed a documentation topic on this.
What does the double dot mean on this piece of code:
require 'benchmark'
iterations = 1000000
b = Benchmark.measure do
for i in 1..iterations do
x = i
end
end
c = Benchmark.measure do
iterations.times do |i|
x = 1
end
end
puts b
puts c
I am new to Ruby so please be easy on me 😛
..indicates aRangeobject going from the number on the left to the number on the right. This can be best shown by converting the object to anArrayobject, like this:See? This object provides a range of digits from 1 through to 10.
It also works for sequential letters:
The
Rangeclass includes theEnumerablemodule which gives us theeachmethod, allowing us to iterate over each element inside the range.Additonal trivia:
...is also used in Ruby, except it gets everything up to but not including the value on the right.