Explain the concept of the scope of Ruby _ruby topics

Source: Internet
Author: User

The range is everywhere: January to December, 0-9 days, 50 to 67 lines, and so forth. Ruby supports scope and allows us to use a range of ways:

    • As a sequence range
    • As a condition range
    • As an interval range

As a sequence range:

First, it may be the most natural use of the range to express the sequence. The sequence has a starting point, an endpoint and a sequential value in the sequence of methods to produce.

Ruby creates '.. ' And the ' ... ' range of operators use these sequences. The form of these two points establishes an inclusive range, while the form of three points creates a range that does not include the specified high value.

(1..5)    #==> 1, 2, 3, 4, 5
(1...5)    #==> 1, 2, 3, 4
(' a ' ...) d ')  #==> ' A ', ' B ', ' C ', ' d '

Sequence 1. 100 is a reference to a Range object that contains two Fixnum objects. If you want, you can use the To_a method for a series of lists. Try the following example:

#!/usr/bin/ruby

$, = ","  # Array Value separator
range1 = (1..10). To_a
range2 = (' Bar ' ... ') Bat '). To_a

puts "#{range1}"
puts "#{range2}"

This will produce the following results:

1, 2, 3, 4, 5, 6, 7, 8, 9,
Bar, Bas, bat

Scope implementations that can iterate between them and test their content in a variety of ways:

#!/usr/bin/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 = digit S.reject {|i| i < 5}
puts "rejected values are #{ret}"

Digits.each do |digit|
  Puts ' in Loop #{digit} ' end


This will produce the following results:

True
Min value is 0
Max value are 9
rejected values are 5, 6, 7, 8, 9 in
Loop 0 in
Loop 1
in Loop 2 in
Loop 3 in
Loop 4 in loop 5 in
loop 6 in
Loop 7 in
Loop 8 in
Loop 9

As a range of conditions:

Scopes can also be used as conditional expressions. For example, the following code fragment prints a set of lines from standard input, and the first line in each group contains the last line of words beginning and ending:

While gets
  print if/start/... /end/End


Scope can use Case statements:

#!/usr/bin/ruby

score =

score when
  0..40: "Fail" when 41..60: ' Pass ' when
  61..70: "P Ass with Merit ' when
  71..100: ' Pass with distinction '
  Else ' Invalid Score '
end

puts result

This will produce the following results:

Pass with Merit

Interval range:

The last universal range used is for the interval test: Whether a value falls within the interval interval, which is an equality operation using = =.

#!/usr/bin/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


This will produce the following results:

5 lies in (1..10)
C lies in (' a '.. ') J ')

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.