Explain the concept of scope in Ruby

Source: Internet
Author: User
Tags min

This article mainly introduces the concept of the scope of Ruby, the need for friends can refer to the

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 2 3 (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:

?

1 2 3 4 5 6 7 8 #!/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 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:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17-18 #!/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 = digits.reject {|i| i < 5} puts "rejected values are #{ret}" Digits.each do |digit| Puts ' in Loop #{digit} ' end

This will produce the following results:

?

1 2 3 4 5 6 7 8 9 10 11 12 13-14 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:

?

1 2 3 While gets print if/start/... /end/end

Scope can use Case statements:

?

1 2 3 4 5 6 7 8 9 10 11 12-13 #!/usr/bin/ruby score = score when 0..40: "Fail" when 41..60: ' Pass ' when 61..70: ' Pass with Merit ' W Hen 71..100: "Pass with distinction" else "Invalid Score" End puts result

This will produce the following results:

?

1 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 = =.

?

1 2 3 4 5 6 7 8 9 10 11 12-13 #!/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:

?

1 2 5 lies in (1..10) c lies in (' a '.. ') J ')
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.