[b]范围
Ranges[/b]
表示位于一个开始值和一个结束值之间的一些值。
开始和结束值之间放置二三个点,如果使用两个点,该范围包含边界
超过三个点,结尾点不包含在内
1..10 #包含10
1.0...10.0 #不包含10.0
include?方法测试一个值是否被包含在一个范围内
cold_war=1945..1989
cold_war.include?birthdate.year
<=>
r='a'..'c'
r.each{|l| print "[#{l}]"} # prints "[a][b][c]"
r.step(2){|l| print "[#{l}]"} # prints "[a][c]"
r.to_a #=> ['a','b','c']
测试一个Range的成员关系
..
定义 begin..end
begin<=x<=end
...
字义begin...end
begin<=x<end
Ruby1.8
r=0...100
r.member?50 # true
r.include?100 #false
r.include?99.9 # true
Ruby1.9
include?
member? 是同义词
triples="AAA".."ZZZ"
triples.include? "ABC" # true fast in 1.8 slow in 1.9
triples.include? "ABCD" #true in 1.8 ,false in 1.9
triples.cover? "ABCD" # true and fast in 1.9
triples.to_a.include? "ABCD" #false and slow in 1.8 and 1.9
2011-4-7 20:09 danny
Ranges[/b]
表示位于一个开始值和一个结束值之间的一些值。
开始和结束值之间放置二三个点,如果使用两个点,该范围包含边界
超过三个点,结尾点不包含在内
1..10 #包含10
1.0...10.0 #不包含10.0
include?方法测试一个值是否被包含在一个范围内
cold_war=1945..1989
cold_war.include?birthdate.year
<=>
r='a'..'c'
r.each{|l| print "[#{l}]"} # prints "[a][b][c]"
r.step(2){|l| print "[#{l}]"} # prints "[a][c]"
r.to_a #=> ['a','b','c']
测试一个Range的成员关系
..
定义 begin..end
begin<=x<=end
...
字义begin...end
begin<=x<end
Ruby1.8
r=0...100
r.member?50 # true
r.include?100 #false
r.include?99.9 # true
Ruby1.9
include?
member? 是同义词
triples="AAA".."ZZZ"
triples.include? "ABC" # true fast in 1.8 slow in 1.9
triples.include? "ABCD" #true in 1.8 ,false in 1.9
triples.cover? "ABCD" # true and fast in 1.9
triples.to_a.include? "ABCD" #false and slow in 1.8 and 1.9
2011-4-7 20:09 danny