如何查看數組是否與範圍匹配?如何將範圍與數組進行比較?
[1..3] == [1,2,3] # => false
我也試過
[1..3].to_a == [1,2,3] # => false
,但我難倒。有沒有辦法將一個範圍強制到一個數組中,以便我可以將它與一個數據庫進行比較?
如何查看數組是否與範圍匹配?如何將範圍與數組進行比較?
[1..3] == [1,2,3] # => false
我也試過
[1..3].to_a == [1,2,3] # => false
,但我難倒。有沒有辦法將一個範圍強制到一個數組中,以便我可以將它與一個數據庫進行比較?
啊!原來我對語法感到困惑。
arr = [1..3] # Actually sets an array with a range as the first element
arr[0] # => 1..3
我需要的是這樣的:
(1..3).to_a == [1,2,3] # => true
'(1..3).to_a'也可以寫成'[* 1..3] => [1,2,3]'。 – 2014-10-07 06:08:36
2.1.1 :006 > [1..3].class
=> Array
2.1.1 :007 > (1..3).class
=> Range
2.1.1 :008 > (1..3).to_a == [1,2,3]
=> true
希望解決您的問題
問題尚不清楚。定義一個範圍與數組匹配的時間。 – sawa 2014-10-07 06:11:40