What is the difference between .. and ... in a ruby for loop.
for num in 1..5
puts num
end
vs
for num in 1...5
puts num
end
How are those two loops different.
What is the difference between .. and ... in a ruby for loop.
for num in 1..5
puts num
end
vs
for num in 1...5
puts num
end
How are those two loops different.
The three dots indicates that the end stops before the terminator, two dots indicates it includes the terminator.
SRC: http://strugglingwithruby.blogspot.pt/2008/11/loops.html
1...5
-> 1 to 4
1..5
-> 1 to 5
In ruby 1...5
gives you a range which doesn't include 5
whereas 1..5
gives you a range which does include 5
eg:
>> (1..5).to_a
[
[0] 1,
[1] 2,
[2] 3,
[3] 4,
[4] 5
]
>> (1...5).to_a
[
[0] 1,
[1] 2,
[2] 3,
[3] 4
]
The difference is between inclusive and no inclusive ranges.
i.e.:
(1..5).to_a
# => [1, 2, 3, 4, 5]
(1...5).to_a
# => [1, 2, 3, 4]
For this reason, your loops will have a different number of cycles.