"abc def ".split(" ")
返回
["abc", "def"]
因此,我期待:
["a", "b", "c", " ", "d", "e", "f", " "].split(" ")
返回
[["a", "b", "c"], ["d", "e", "f"]]
但它回来了
[["a", "b", "c"], ["d", "e", "f"], []]
我通读了在 active_support/core_ext/array/grouping.rb 中进行拆分的源代码(我正在使用 ActiveSupport 4.0.0 和 ruby 2.0.0-p247)。您可以在此处找到 2 行文档:http: //api.rubyonrails.org/classes/Array.html#method-i-split,代码如下:
def split(value = nil, &block)
inject([[]]) do |results, element|
if block && block.call(element) || value == element
results << []
else
results.last << element
end
results
end
end
这就解释了它是如何进行拆分的。
现在,这是预期的行为还是 ActiveSupport 错误?