我正在使用Ruby 2.0作爲Rails項目,並且在控制檯中獲取數組的元素長度時遇到了一些問題。Ruby - Array.length返回字符數而不是元素數
第一個例子
2.0.0-p353 :001 > search = "test"
=> "test"
2.0.0-p353 :002 > search.split
=> ["test"]
2.0.0-p353 :003 > search.length
=> 4
第二個例子
2.0.0-p353 :001 > search = "testOne, TestTwo"
=> "testOne, TestTwo"
2.0.0-p353 :002 > search.split(/[\s,]+/)
=> ["testOne", "TestTwo"]
2.0.0-p353 :003 > search.length
=> 16
如何返回元素計數代替字符的算?
'search.split.length => 1' – engineersmnky