Updated April 3, 2023
Introduction to Ruby Ranges
Ranges in the Ruby are a way of handling some sequences , these sequences are anything which we use in our day to day life, for example, a to z, A to Z or from 1 to 100 all these are ranges. In Ruby, ranges can be used for three broader categories like for sequence, for conditions and also we can use the ranges for the interval, in general if you wanted to use the number from 1 to 100 then instead of writing each number we can simply put the range from 1 to 100(1..100) using double dots (..)or in case if we want to ignore the highest number which means we want to have range from 1 to 99 we can use 1…100 which is triple dots(…).
Syntax:
We can have three different syntax for the ranges, the three different syntaxes are given below:
- Syntax for the use of ranges as the sequences:
Here we can use it in case if we want some sequence from on start range to another end value. See the below syntax we have assigned some sequence from start to the end. You can assign anything from numeric ranges to string ranges for example, a..z or 1..100.
sequences = (start ..end).to_a
example (a..d).to_a (output will be a,b,c,d)
- Syntax for the use of ranges as the conditions:
In this use the case and you will be able to get the range of the number. In the below example we have one value it can be anything either numeric or string and it will be going into the case block of ruby where it will be check with a certain defined syntax we are using it for any comparison or for certain specific conditions, for example if you have one number 40 and you wanted to know the range of the number then you can ranges. Once the value match the particular ranges we can print the message or perform a certain operation as we have information about the ranges.
value = any numeric or string value
result = case value
when start1..end1 then "messages according to the conditions"
when start2..end2 then "messages according to the conditions"
when start3..end3 then "messages according to the conditions"
end
- Syntax for the use of the ranges as the Intervals:
for this we can use the operator ===, in the below syntax we can see the uses of the === for getting the value falling under the given range or not. For example a..g ===f and the output will be true as f is falling under the range a to g.
(start..end) === compare any value
(example , a..g===f ,output will be true)
How to Represent Ruby Ranges Using Various Methods?
Below we learn how to represent Ruby Ranges using Various Methods with the help of examples:
Example #1 – to_a Method
This method used to convert the ranges into an array. With the help of this method we can get the array as many times we need to create a large array with writing many attributes into that, with help of the method to_a we will get the array easily. See the below example along with the output of the screen.
Code:
puts "#{("abc".."abe").to_a}"
puts "#{(1..10).to_a}"
Output:
Example #2 – (min,max and include) Method
This method in the ranges returns the minimum value from the given range. In case if we have a large set of range in that case it will be very useful to fetch the minimum or the maximum value. we have also used the method include on the ranges which checks for the number or letter or any substring fall under the ranges. See the below example we have used min, max and include along with output screen.
Code:
alphabets = "a".."r"
puts "The letter n is in available in the given range #{alphabets.include?("n")}"
alphabet1 = alphabets.min
alphabet2 = alphabets.max
puts "The lowest value of the alphabet is #{alphabet1}"
puts "The largest value of the alphabet is #{alphabet2}"
Output:
Example #3 – Reject Method
Here we are using the reject method of the range, with the help of this method we can return the range of letters. In the below example we are simply rejecting the letter if it is less then “c”, in block of code it will start rejecting from c till the last range value , which is “e”.
Code:
alphabets ="a".."e"
rejected = alphabets.reject {|i| i < "c" }
puts "Rejected letters start from c are #{rejected}"
Output:
Example #4 – Each Method
Each method for the range is used to iterate over all the values inside the range. In the below example we are printing all the value of the range from a to e. Please see the below example with the screen of output.
Code:
alphabets ="a".."e"
alphabets.each do |alphabet|
puts "The list of alphabet is #{alphabet}"
end
Output:
Example #5 – Case Method
With the help of the case we check for the particular value ranges, in the below example we have a mark of a student and we wanted to understand the grade of the student. The case attribute “when” will check for the range each time and if match the particular range it will print the message for that range. Please follow the below example along with the screen of output.
Code:
mark =67
studentFinalGrade = case mark
when 0..28 then "The student got very less mark and failed in this paper"
when 29..40 then "The student got average marks and he got pass with D grade"
when 41..60 then "The student is ok and he got C grade"
when 61..81 then "The student is very good and he got B grade"
when 82..100 then "The student is very excellent and he got A grade"
else "The mark is not valid and no grade can be assigned"
end
puts studentFinalGrade
Output:
Conclusion
From these tutorials we learned about the ranges in Ruby, we understand the uses and the core concepts like its method which are the main plus points, as with the help of ranges method we can do comparison to conversion into array to the given set of ranges, which will make developer life more comfortable.
Recommended Article
We hope that this EDUCBA information on “Ruby Ranges” was beneficial to you. You can view EDUCBA’s recommended articles for more information.