Range object learning notes and rubyrange learning notes in Ruby

Source: Internet
Author: User

Range object learning notes and rubyrange learning notes in Ruby

Range is a class of Range objects. You can use the Range operator ".. "or"... ","... "The generated range object includes the start and end points,"... "The generated range object does not include the start and end points. The range is composed of a sequence of ordered and regular element objects. Any ordered and regular group of objects, can be defined using a Range object, such as numbers, letters, strings, and even time

1. Definition of Range object

Copy codeThe Code is as follows:
R1 = 1 .. 5 # define the range object r1, including elements 1, 2, 3, 4, 5
R2 = Range. new () # equivalent to 1. 5
R3 = 1... 5 # define the range object r3, including elements 2, 3, 4
R4 = Range. new (1, 5, true) # equivalent to 1... 5

2. Wonderful use of Range objects

Copy codeThe Code is as follows:
R = r and * 100
Rf = format ('%. 2f', r)
Case r
When 90 .. 100
Puts "score: # {rf} score: Excellent"
When 70 .. 90
Puts "score: # {rf} score: good"
When 50... 70
Puts "score: # {rf} score: qualified"
Else
Puts "score: # {rf} score: unqualified"
End

Generally, it is a very troublesome process to traverse time, but with Range, this will be very simple. The following sample code:

Copy codeThe Code is as follows:
# Traverse 2013-01-01 to 2013-02-28 every day, the object is Date
Begin_date = Date. parse '2017-01-01'
End_date = Date. parse '2017-02-28'
R1 = begin_date... end_date
R1.each {| date | puts date}
# Traverse all abc-xyz strings
R2 = 'abc'... 'xyz'
R2.each {| str | puts str}
# Determine whether an element is in a certain range
R3 = 'A'... 'Z'
Puts r3 = 'A' # false
Puts r3.include? 'K' # true
Puts r3.min #
Puts r3.max # z
Puts r3.first (3) # a, B, c
Puts r3.last (4) # w, x, y, z


In ruby, what is the function of connecting objects with &? What is the returned result?

& = And

What is the difference between the Range object and the Cell object in EXCEL? Which object is the set that contains all the ranges?

Range object
Represents a cell, a row, a column, a selected area (this area can contain one or more consecutive cells), or a three-dimensional area.
Worksheet. Cells attributes
Returns a Range object that represents all cells in the worksheet (not just the currently used cells ).

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.