Ruby For Loop Number Ranges

Oct
5

I was starting to learn Ruby, and came across number ranges in a for loop. I discovered that there is an easy way to include or exclude the top boundary depending upon the number of periods that you use. Note that you can also use variables as either end of the range.

top_limit = 3
for i in 1..top_limit # inclusive of the top boundary
  puts i # 1 2 3
end

for j in 1...top_limit # doesn't include the top boundary
  puts j # 1 2
end
Tags: #Ruby Author: Seth Westphal
comments powered by Disqus