In Ruby 1.8.7, Array("hello\nhello")
gives you ["hello\n", "hello"]
. This does two things that I don't expect:
在Ruby 1.8.7中,Array(“hello \ nhello”)为您提供了[“hello \ n”,“hello”]。这样做有两件我不期望的事情:
-
It splits the string on newlines. I'd expect it simply to give me an array with the string I pass in as its single element without modifying the data I pass in.
它将字符串拆分为换行符。我希望它只是给我一个数组,其中我传入的字符串作为单个元素而不修改我传入的数据。
-
Even if you accept that it's reasonable to split a string when passing it to Array, why does it retain the newline character when
"foo\nbar".split
does not?即使你接受在将字符串传递给Array时拆分字符串是合理的,为什么当“foo \ nbar”.split没有时它会保留换行符?
Additionally:
另外:
>> Array.[] "foo\nbar"
=> ["foo\nbar"]
>> Array.[] *"foo\nbar"
=> ["foo\n", "bar"]
3 个解决方案
#1
1
It splits the string on newlines. I'd expect it simply to give me an array with the string I pass in as its single element without modifying the data I pass in.
它将字符串拆分为换行符。我希望它只是给我一个数组,其中我传入的字符串作为单个元素而不修改我传入的数据。
That's a convention as good as any other. For example, the list
constructor in Python does something entirely different:
这是一个与其他任何一样好的惯例。例如,Python中的列表构造函数完全不同:
>>> list("foo")
['f', 'o', 'o']
So long as it's consistent I don't see the problem.
只要它一致,我就不会发现问题。
Even if you accept that it's reasonable to split a string when passing it to Array, why does it retain the newline character when
"foo\nbar".split
does not?即使你接受在将字符串传递给Array时拆分字符串是合理的,为什么当“foo \ nbar”.split没有时它会保留换行符?
My wild guess here (supported by quick googling and TryRuby) is that the .split
method for strings does so to make it the "inverse" operation of the .join
method for arrays.
我在这里疯狂猜测(由快速谷歌搜索和TryRuby支持)是字符串的.split方法这样做,使其成为数组.join方法的“逆”操作。
>> "foospambar".split("spam").join("spam")
=> "foospambar"
By the way, I cannot replicate your behaviour on TryRuby:
顺便说一下,我无法在TryRuby上复制你的行为:
>> x = Array("foo\nbar")
=> ["foo\nbar"]
>> Array.[] *"foo\nbar"
=> ["foo\nbar"]
#2
0
If you replace the double-quotes with single-quotes it works as expected:
如果用单引号替换双引号,它按预期工作:
>> Array.[] "foo\nbar"
=> ["foo\nbar"]
>> Array.[] 'foo\nbar'
=> ["foo\\nbar"]
#3
0
You may try:
你可以尝试:
"foo\nbar".split(/w/)
"foo\nbar".split(/^/)
"foo\nbar".split(/$/)
and other regular expressions.
和其他正则表达式。
#1
1
It splits the string on newlines. I'd expect it simply to give me an array with the string I pass in as its single element without modifying the data I pass in.
它将字符串拆分为换行符。我希望它只是给我一个数组,其中我传入的字符串作为单个元素而不修改我传入的数据。
That's a convention as good as any other. For example, the list
constructor in Python does something entirely different:
这是一个与其他任何一样好的惯例。例如,Python中的列表构造函数完全不同:
>>> list("foo")
['f', 'o', 'o']
So long as it's consistent I don't see the problem.
只要它一致,我就不会发现问题。
Even if you accept that it's reasonable to split a string when passing it to Array, why does it retain the newline character when
"foo\nbar".split
does not?即使你接受在将字符串传递给Array时拆分字符串是合理的,为什么当“foo \ nbar”.split没有时它会保留换行符?
My wild guess here (supported by quick googling and TryRuby) is that the .split
method for strings does so to make it the "inverse" operation of the .join
method for arrays.
我在这里疯狂猜测(由快速谷歌搜索和TryRuby支持)是字符串的.split方法这样做,使其成为数组.join方法的“逆”操作。
>> "foospambar".split("spam").join("spam")
=> "foospambar"
By the way, I cannot replicate your behaviour on TryRuby:
顺便说一下,我无法在TryRuby上复制你的行为:
>> x = Array("foo\nbar")
=> ["foo\nbar"]
>> Array.[] *"foo\nbar"
=> ["foo\nbar"]
#2
0
If you replace the double-quotes with single-quotes it works as expected:
如果用单引号替换双引号,它按预期工作:
>> Array.[] "foo\nbar"
=> ["foo\nbar"]
>> Array.[] 'foo\nbar'
=> ["foo\\nbar"]
#3
0
You may try:
你可以尝试:
"foo\nbar".split(/w/)
"foo\nbar".split(/^/)
"foo\nbar".split(/$/)
and other regular expressions.
和其他正则表达式。