Hi! I'm Nathan Hoad

Ruby: Defining literal ranges

In Ruby it's simple to define literal ranges by doing something like:

numbers = 0..10

# prints the numbers 0 through 10
numbers.each { |n| puts n }

Ruby also lets you use '...' in order to specify an exclusive range. This works similar to the example above but ignores the very last number:

numbers = 0...10

# prints the number 0 through 9
numbers.each { |n| puts n }

This is useful if you have a count variable and want to define a range without having to do a minus one on it:

range = 4..(count - 1)

# as opposed to

range = 4...count

In the end both range definitions have their uses - the main thing is just remembering which is which

If I just made your day a little better then thank me with a coffee or maybe a a pizza