Ruby - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Ruby Ranges

Ruby Ranges

shape Introduction

This chapter demonstrates about the Ruby Ranges ruby allows to user to use ranges in variety of ways ranges are occur in every where and following are the concepts covered in this chapter.
  • Ranges as conditions
  • Ranges as sequence
  • Ranges as Intervals

Ranges as conditions

shape Description

In Ruby Ranges can be known as a conditional expressions as shown in the below snippet. In the below snippet code fragment print set of lines from standard input in which the first line begins with word start and the last line ends with end as shown in below snippet. [ruby] while gets print if /start/../end/ end [/ruby] The code below demonstrates the ranges in case statement as shown below. [ruby] score = 70 result = case score when 0..40 then "Fail" when 41..60 then "Pass" when 61..70 then "Pass with Merit" when 71..100 then "Pass with Distinction" else "Invalid Score" end puts result [/ruby] Result By running the above code in command prompt, the output can be obtained as shown in the image below.

Ranges as sequence

shape Description

Ruby can express Ranges as a sequences those sequences have the starting point and ending point and which produce some successive values. The sequence can be created by using the ".." and  "..." range operators in which 2 dots forms an inclusive range and 3 dots form excluded range which specified the peaks value. The snippet below demonstrates range from starting point to ending point as shown below. [ruby] (1..5) #==> 1, 2, 3, 4, 5 (1...5) #==> 1, 2, 3, 4 ('a'..'d') #==> 'a', 'b', 'c', 'd' [/ruby] User can change the range to a list with to_a method. The code below demonstrates to convert to 1..1000 sequence to two Fixnum objects as shown below. [ruby] $, =", " # Array value separator range1 = (1..10).to_a range2 = ('bar'..'bat').to_a puts "#{range1}" puts "#{range2}" [/ruby] Result By running the above code in command prompt, the output can be obtained as shown in the image below. Ranges implements some methods which iterate over them and test the contents in a different ways as shown in below code. [ruby] # Assume a range digits = 0..9 puts digits.include?(5) ret = digits.min puts "Min value is #{ret}" ret = digits.max puts "Max value is #{ret}" ret = digits.reject {|i| i < 5 } puts "Rejected values are #{ret}" digits.each do |digit| puts "In Loop #{digit}" end [/ruby] Result By running the above code in command prompt, the output can be obtained as shown in the image below.

Ranges as Intervals

shape Description

Ranges as Intervals is the final use of the ranges if some values falls with in the interval shown by the range which can be done by using "===" operator. The code below demonstrates the Ranges as interval. [ruby] if ((1..10) === 5) puts "5 lies in (1..10)" end if (('a'..'j') === 'c') puts "c lies in ('a'..'j')" end if (('a'..'j') === 'z') puts "z lies in ('a'..'j')" end [/ruby] Result By running the above code in command prompt, the output can be obtained as shown in the image below.

Summary

shape Key Points

  • Sequence consist starting point and ending point.
  • Two dot creates inclusive range and Three dot creates exclusive range.
  • Ranges may also used as conditional expressions.