Apr 5

Ranges

Category: Basics,Sample Code

Ranges can be better defined as a numeric array. Ranges can be defined as such:

numrange = 1..20 (numrange holds all the numbers between 1 and 20, effectively turning it into an array)
numrange2 = 1…20 (is another way of defining ranges, holding all numbers between 1 and 20 except the last number 20)

As with previous arrays and hashes, several operations can be done on them like:

puts numrange.min (gives you the lowest number in the specified range)
puts numrange.maxn (gives you the highest number in the specified range)

To check if a specified number is part of the range

numrange.include? (14)

To show the contents of the specified range:

puts numrange.to_a

Comments are off for this post

Comments are closed.