Ruby范围作为序列

示例

范围最重要的用途是表达序列

语法:

(begin..end) => this construct will include end value

(begin...end) => this construct will exclude end value

要么

Range.new(begin,end,exclude_end) => exclude_end is by default false

最重要的end值必须大于begin,否则将不返回任何值。

例子:

(10..1).to_a            #=> []

(1...3)                 #=> [1, 2]

(-6..-1).to_a           #=> [-6, -5, -4, -3, -2, -1]

('a'..'e').to_a         #=> ["a", "b", "c", "d", "e"]

('a'...'e').to_a        #=> ["a", "b", "c", "d"]

Range.new(1,3).to_a     #=> [1, 2, 3] 

Range.new(1,3,true).to_a#=> [1, 2]

           

以上是 Ruby范围作为序列 的全部内容, 来源链接: utcz.com/z/340704.html

回到顶部