YouTip LogoYouTip

Ruby Range

Ruby Range \\n\\n

Ruby Range

\\n\\n

-- Learning not just skills, but dreams!

\\n\\n \\n\\n \\n\\n \\n\\n \\n\\n \\n\\n

Ruby Tutorial

\\n

\\n Ruby Tutorial\\n Ruby Introduction\\n Ruby Environment\\n Ruby Installation - Linux\\n Ruby Installation - Windows\\n Ruby Chinese Encoding\\n Ruby Command Line Options\\n Ruby Environment Variables\\n Ruby Syntax\\n Ruby Data Types\\n Ruby Classes and Objects\\n Ruby Class Case Study\\n Ruby Variables\\n Ruby Operators\\n Ruby Comments\\n Ruby Conditional Statements\\n Ruby Loops\\n Ruby Methods\\n Ruby Blocks\\n Ruby Modules\\n Ruby Strings\\n Ruby Arrays\\n Ruby Hashes\\n Ruby Date & Time\\n Ruby Ranges\\n Ruby Iterators\\n Ruby File I/O\\n Ruby File Class and Methods\\n Ruby Dir Class and Methods\\n Ruby Exceptions\\n

\\n\\n

Ruby Advanced Tutorial

\\n

\\n Ruby Object Oriented\\n Ruby Regular Expressions\\n Ruby Database Access - DBI Tutorial\\n Ruby Mysql\\n Ruby CGI Programming\\n Ruby CGI Methods\\n Ruby CGI Cookie\\n Ruby CGI Session\\n Ruby Sending Email - SMTP\\n Ruby Socket Programming\\n Ruby XML, XSLT and XPath Tutorial\\n Ruby Web Service\\n Ruby Multithreading\\n Ruby JSON\\n Ruby RubyGems\\n

\\n\\n

Ruby Date & Time

\\n

Ruby Iterators

\\n\\n

Ruby Range

\\n\\n

Ranges are everywhere: from a to z, from 0 to 9, and so on. Ruby supports ranges and allows us to use ranges in different ways:

\\n\\n
    \\n
  • As a sequence
  • \\n
  • As a condition
  • \\n
  • As an interval
  • \\n
\\n\\n

As a Sequence

\\n\\n

The first and most common use of a range is to express a sequence. A sequence has a start, an end, and a way to produce successive values in the sequence.

\\n\\n

Ruby uses the ''..'' and ''...'' range operators to create these sequences. The two-dot form creates a range that includes the specified high value, while the three-dot form creates a range that excludes the specified high value.

\\n\\n
(1..5)      #==> 1, 2, 3, 4, 5\\n(1...5)     #==> 1, 2, 3, 4\\n('a'..'d')  #==> 'a', 'b', 'c', 'd'
\\n\\n

The sequence 1..100 is a Range object that contains references to two Fixnum objects. If needed, you can convert a range to a list using the to_a method. Try the following example:

\\n\\n

Example

\\n
#!/usr/bin/ruby\\n$, = ", "   # Array value separator\\nrange1 = (1..10).to_a\\nrange2 = ('bar'..'bat').to_a\\nputs "#{range1}"\\nputs "#{range2}"
\\n\\n

Try it now Β»

\\n\\n

This will produce the following result:

\\n
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\\n["bar", "bas", "bat"]
\\n\\n

Ranges implement methods that let you traverse them, and you can check their contents in various ways:

\\n\\n

Example

\\n
#!/usr/bin/ruby\\n# -*- coding: UTF-8 -*-\\n# Specify range\\ndigits = 0..9\\nputs digits.include?(5)\\nret = digits.min\\nputs "Minimum value is #{ret}"\\nret = digits.max\\nputs "Maximum value is #{ret}"\\nret = digits.reject {|i| i<5 }\\nputs "Those that do not meet the criteria #{ret}"\\ndigits.each do |digit|\\n    puts "In the loop #{digit}"\\nend
\\n\\n

Try it now Β»

\\n\\n

This will produce the following result:

\\n
true\\nMinimum value is 0\\nMaximum value is 9\\nThose that do not meet the criteria [5, 6, 7, 8, 9]\\nIn the loop 0\\nIn the loop 1\\nIn the loop 2\\nIn the loop 3\\nIn loop 4\\nIn loop 5\\nIn the loop 6\\nIn the loop 7\\nIn the loop 8\\nIn the loop 9
\\n\\n

As a Condition

\\n\\n

Ranges can also be used as conditional expressions. For example, the following code snippet prints lines from standard input where each set starts with a line containing the word start and ends with a line containing the word end:

\\n\\n
while gets\\n    print if /start/../end/\\nend
\\n\\n

Ranges can be used in case statements:

\\n\\n

Example

\\n
#!/usr/bin/ruby\\n# -*- coding: UTF-8 -*-\\nscore = 70\\nresult = case score\\n    when 0..40\\n        "Bad score"\\n    when 41..60\\n        "Almost passing"\\n    when 61..70\\n        "Passing score"\\n    when 71..100\\n        "Good score"\\n    else\\n        "Wrong score"\\nend\\nputs result
\\n\\n

Try it now Β»

\\n\\n

This will produce the following result:

\\n
Passing score
\\n\\n

As an Interval

\\n\\n

The final use of ranges is interval checking: checking whether a specified value falls within the specified range. This is done using the === equality operator.

\\n\\n

Example

\\n
#!/usr/bin/ruby\\n# -*- coding: UTF-8 -*-\\nif ((1..10) === 5)\\n    puts "5 in (1..10)"\\nend\\nif (('a'..'j') === 'c')\\n    puts "c in ('a'..'j')"\\nend\\nif (('a'..'j') === 'z')\\n    puts "z in ('a'..'j')"\\nend
\\n\\n

Try it now Β»

\\n\\n

This will produce the following result:

\\n
5 in (1..10)\\nc in ('a'..'j')
\\n\\n

Ruby Date & Time

\\n

Ruby Iterators

← Ruby IteratorsRuby Date Time β†’