红宝石 |范围类方法
先决条件:红宝石 |范围
Ruby 提供了一个Range 类。 Ruby 范围描述了一组具有开始和结束的值。范围的值可以是数字、字符、字符串或对象。它是使用start_point..end_point 、 start_point...endpoint 字面量或::new构造的。它为代码提供了灵活性并减少了代码的大小。您还可以使用Range.new创建范围。包含.. (两个点)的范围意味着从起始值到结束值(包括),如果一个范围包含…… (三个点)意味着它不包括结束值。
例子:
(1..6).to_a # Output= [1, 2, 3, 4, 5, 6]
(1...6).to_a # Output= [1, 2, 3, 4, 5]
注意:在 Ruby 中,可以使用对象创建范围,只要对象可以使用它们的 <=>运算符进行比较。为了按顺序返回下一个对象,它提供了对succ方法的支持。
类方法
new :此方法用于从给定的开始和结束值创建一个范围。在该方法中,如果第三个参数为 false 或排除,则范围包括 end-object。否则,它将省略。
Range.new(start, end, exclusive=false)
例子:
Ruby
# Ruby program to illustrate
# new Method
a = 12
b = 15
# Output will be 12..15
Range.new(a, b, false)
Ruby
# Ruby program to illustrate
# the use of == method
# Using Range.new class method
a = Range.new(2, 5, false)
b = Range.new(2, 5, false)
# Using == instance method
a == b
Ruby
# Ruby program to illustrate the use
# of === method by case statement
# taking case statement
case 25.67
when 1...55 then puts "Lower"
when 55...85 then puts "Medium"
when 85...100 then puts "Upper"
end
Ruby
# Ruby program to illustrate
# the use of begin method
# Creating range using the new method
b = Range.new(3, 9, false)
# using begin instance method
b.begin
Ruby
# Ruby program to illustrate
# the use of each method
# using each method
(40..45).each do |i|
print i, '....'
end
Ruby
# Ruby program to illustrate
# the use of end method
# using end method
a = Range.new(3, 9, false)
a.end
Ruby
# Ruby program to illustrate
# the use of eql? method
# Constructing ranges
a = Range.new(2, 5, false)
b = Range.new(2, 5, false)
# using eql? method
a.eql?(b)
Ruby
# Ruby program to illustrate the
# use of exclude_end? method
# constructing range
a = Range.new(3, 9, false)
# using exclude_end? method
a.exclude_end?
Ruby
# Ruby program to illustrate
# the use of first method
# constructing range
a = Range.new(3, 9, false)
# using the first method
a.first
Ruby
# Ruby program to illustrate
# the use of last method
# constructing range
a = Range.new(3, 9, false)
# using last method
a.last
Ruby
# Ruby program to illustrate
# the use of member? method
# taking a range
a = 1..10
# using member? method
a.member?(5)
Ruby
# Ruby program to illustrate
# the use of include? method
# using include? method
("A".."G").include?("Z")
输出:
12..15
实例方法
- == :如果obj的起始、结束和第三个参数的值与rng相同,则此方法返回 true。否则,它将返回 false。此方法的返回类型是布尔值。
rng==obj-->true or false
- 例子:
红宝石
# Ruby program to illustrate
# the use of == method
# Using Range.new class method
a = Range.new(2, 5, false)
b = Range.new(2, 5, false)
# Using == instance method
a == b
- 输出:
true
- === :在此方法中,如果rng省略其结尾,则返回rng.start <= value < rng.end并且如果rng包含在内,则返回rng.start <= value<= rng.end 。方便的是,=== 是运算符,case 语句使用它。
rng===value --> true or false
- 例子:
红宝石
# Ruby program to illustrate the use
# of === method by case statement
# taking case statement
case 25.67
when 1...55 then puts "Lower"
when 55...85 then puts "Medium"
when 85...100 then puts "Upper"
end
- 输出:
Lower
- begin :此方法返回rng的第一个对象。
rng.begin --> obj
- 例子:
红宝石
# Ruby program to illustrate
# the use of begin method
# Creating range using the new method
b = Range.new(3, 9, false)
# using begin instance method
b.begin
- 输出:
3
- each :此方法用于迭代rng的元素,方法是将 each 依次传递给块。
rng.each{|j| block} --> rng
- 例子:
红宝石
# Ruby program to illustrate
# the use of each method
# using each method
(40..45).each do |i|
print i, '....'
end
- 输出:
40....41....42....43....44....45....
- end :此方法返回rng的结束对象。
rng.end --> obj
- 例子:
红宝石
# Ruby program to illustrate
# the use of end method
# using end method
a = Range.new(3, 9, false)
a.end
- 输出:
9
- 情商? :此方法检查obj和rng在开始、结束和排他标志方面是否相等。如果obj包含与rng相同的 start、end 和 Exclusive flage 值,则返回 true,否则返回 false。
rng.eql?(obj) --> true or false
- 例子:
红宝石
# Ruby program to illustrate
# the use of eql? method
# Constructing ranges
a = Range.new(2, 5, false)
b = Range.new(2, 5, false)
# using eql? method
a.eql?(b)
- 输出:
true
- 排除结束? : 如果省略rng的结尾,此方法返回 true,否则返回 false。
rng.exclude_end? --> true or false
- 例子:
红宝石
# Ruby program to illustrate the
# use of exclude_end? method
# constructing range
a = Range.new(3, 9, false)
# using exclude_end? method
a.exclude_end?
- 输出:
false
- first :此方法返回rng的起始对象。
rng.first --> obj
- 例子:
红宝石
# Ruby program to illustrate
# the use of first method
# constructing range
a = Range.new(3, 9, false)
# using the first method
a.first
- 输出:
3
- last :此方法返回rng的最后一个对象。
rng.last --> obj
- 例子:
红宝石
# Ruby program to illustrate
# the use of last method
# constructing range
a = Range.new(3, 9, false)
# using last method
a.last
- 输出:
9
- 成员? :此方法检查给定值是否为rng的成员。如果给定值是rng的成员,则返回 true,否则返回 false。
rng.member?(value) --> true or false
- 例子:
红宝石
# Ruby program to illustrate
# the use of member? method
# taking a range
a = 1..10
# using member? method
a.member?(5)
- 输出:
true
- 包括? :如果obj是rng的元素,此方法返回 true,否则返回 false。
rng.include?(value) --> true or false
- 例子:
红宝石
# Ruby program to illustrate
# the use of include? method
# using include? method
("A".."G").include?("Z")
- 输出:
false
- step :此方法通过将每个第 n个对象传递给块来迭代rng 。如果范围有数字,则加一会生成连续的元素。否则,步骤调用succ方法来迭代范围元素。
rng.step(n=1){|obj|block} --> rng
参考: https://ruby-doc.org/core-1.9.3/Range.html