Ruby Range
\\n\\n-- Learning not just skills, but dreams!
\\n\\n- \\n
- Home \\n
- HTML \\n
- JavaScript \\n
- CSS \\n
- Vue \\n
- React \\n
- Python3 \\n
- Java \\n
- C \\n
- C++ \\n
- C# \\n
- AI \\n
- Go \\n
- SQL \\n
- Linux \\n
- VS Code \\n
- Bootstrap \\n
- Git \\n
- Local Bookmarks \\n
- \\n
- Vue3 Tutorial \\n
- Vue2 Tutorial \\n
- \\n
- Bootstrap3 \\n
- Bootstrap4 \\n
- Bootstrap5 \\n
- \\n
- Machine Learning \\n
- PyTorch \\n
- TensorFlow \\n
- Sklearn \\n
- NLP \\n
- AI Agent \\n
- Ollama \\n
- Coding Plan \\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\\nRuby 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 \\n \\n\\nRuby Range
\\n\\nRanges 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
As a Sequence
\\n\\nThe 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\\nRuby 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\\nExample
\\n#!/usr/bin/ruby\\n$, = ", " # Array value separator\\nrange1 = (1..10).to_a\\nrange2 = ('bar'..'bat').to_a\\nputs "#{range1}"\\nputs "#{range2}"\\n\\n \\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\\nExample
\\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 \\n\\n This will produce the following result:
\\ntrue\\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\\nRanges 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\\nwhile gets\\n print if /start/../end/\\nend\\n\\n Ranges can be used in case statements:
\\n\\nExample
\\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 \\n\\n This will produce the following result:
\\nPassing score\\n\\n As an Interval
\\n\\nThe 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\\nExample
\\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 \\n\\n This will produce the following result:
\\n5 in (1..10)\\nc in ('a'..'j')\\n\\n \\n
YouTip