I'm trying to reverse words in an array of string variables, but split doesn't seem to be working.
我试图在字符串变量的数组中反转单词,但是split似乎不起作用。
Testing in IRB I get "NoMethodError: private method `split' called for Array", which I'm assuming has something to do with my program quietly doing nothing.
在IRB中进行测试时,我得到了“NoMethodError: private method ' split'调用了Array”,我假设这与我的程序没有任何关系。
For example, I have:
例如,我有:
nameList = ["Joe Blow", "Mary Sue", "Alice Mallory"].
I expect to return:
我期待返回:
["Blow Joe", "Sue Mary", "Mallory Alice"].
So I iterate through the array, splitting, reversing and joining. This is where nothing happens:
所以我遍历数组,拆分,反转和合并。这就是什么都没有发生的地方:
nameList.each { |x|
x.to_s.split(' ').reverse!.join(' ')
puts x #testing here
}
This outputs:
这个输出:
Joe Blow
Mary Sue
Alice Mallory
I must be missing something extremely simple, as this can't be too difficult.
我一定错过了一些非常简单的东西,因为这不会太难。
3 个解决方案
#1
7
You're splitting, reversing and discarding the result. Check this out.
你在分裂,颠倒和丢弃结果。看看这个。
nameList = ["Joe Blow", "Mary Sue", "Alice Mallory"]
nameList.each { |x|
puts x.to_s.split(' ').reverse.join(' ')
puts x
puts '' # for easier reading
}
# >> Blow Joe
# >> Joe Blow
# >>
# >> Sue Mary
# >> Mary Sue
# >>
# >> Mallory Alice
# >> Alice Mallory
# >>
If you want to apply some transformation to every element of array, get new value and construct a new array of these values, it is idiomatic to use Array#map
function.
如果您想对数组的每个元素应用一些转换,获取新值并构造这些值的新数组,那么使用array #map函数是惯用的。
nameList = ["Joe Blow", "Mary Sue", "Alice Mallory"]
newList = nameList.map { |x|
x.to_s.split(' ').reverse.join(' ')
}
Also, here you shouldn't use bang version of reverse (reverse!
). It has destructive semantics. reverse
creates a new reversed array, while reverse!
updates source array in place. In this case source array is a temp variable, so it makes no difference result-wise. But I consider it confusing and distracting.
此外,这里不应该使用反向的bang版本(反向!)它有破坏性的语义。反向创建一个新的反向数组,而反向!更新源数组的位置。在这种情况下,源数组是一个临时变量,因此它对结果没有影响。但我认为它令人困惑和分心。
#2
1
A compact version:
一个紧凑的版本:
nameList.map!{ |x| x.split.reverse.join(' ') }
#=> ["Blow Joe", "Sue Mary", "Mallory Alice"]
#3
0
The Problem
Everything in Ruby has a return value. In this case, you aren't using the return value of your method chain; you're just printing the original value.
Ruby中的所有东西都有返回值。在这种情况下,您没有使用方法链的返回值;你只是打印原始值。
nameList.each { |x|
# Returns a result, but you aren't doing anything with it.
x.to_s.split(' ').reverse!.join(' ')
# Here you print your original value.
puts x #testing here
}
The Solution
The simple way to do this is to use #collect, which returns an array.
简单的方法是使用#collect,它返回一个数组。
p nameList.collect { |name| name.split.reverse.join(' ') }
["Blow Joe", "Sue Mary", "Mallory Alice"]
=> ["Blow Joe", "Sue Mary", "Mallory Alice"]
This prints a modified array, and also returns an array as a result for further processing. Alternatively, if you really want to change your array, you can assign the result like so:
这将打印一个修改后的数组,并返回一个数组作为进一步处理的结果。或者,如果您真的想要更改数组,您可以这样分配结果:
nameList = nameList.collect { |name| name.split.reverse.join(' ') }
=> ["Blow Joe", "Sue Mary", "Mallory Alice"]
nameList
=> ["Blow Joe", "Sue Mary", "Mallory Alice"]
There are certainly other ways to do this, but it's important to keep it readable. In this case, it makes sense to maintain the semantics of assigning an array to your variable so that the intent is clear.
当然,还有其他方法可以做到这一点,但是保持可读性是很重要的。在这种情况下,维护为变量分配数组的语义是有意义的,这样目的就很清楚了。
#1
7
You're splitting, reversing and discarding the result. Check this out.
你在分裂,颠倒和丢弃结果。看看这个。
nameList = ["Joe Blow", "Mary Sue", "Alice Mallory"]
nameList.each { |x|
puts x.to_s.split(' ').reverse.join(' ')
puts x
puts '' # for easier reading
}
# >> Blow Joe
# >> Joe Blow
# >>
# >> Sue Mary
# >> Mary Sue
# >>
# >> Mallory Alice
# >> Alice Mallory
# >>
If you want to apply some transformation to every element of array, get new value and construct a new array of these values, it is idiomatic to use Array#map
function.
如果您想对数组的每个元素应用一些转换,获取新值并构造这些值的新数组,那么使用array #map函数是惯用的。
nameList = ["Joe Blow", "Mary Sue", "Alice Mallory"]
newList = nameList.map { |x|
x.to_s.split(' ').reverse.join(' ')
}
Also, here you shouldn't use bang version of reverse (reverse!
). It has destructive semantics. reverse
creates a new reversed array, while reverse!
updates source array in place. In this case source array is a temp variable, so it makes no difference result-wise. But I consider it confusing and distracting.
此外,这里不应该使用反向的bang版本(反向!)它有破坏性的语义。反向创建一个新的反向数组,而反向!更新源数组的位置。在这种情况下,源数组是一个临时变量,因此它对结果没有影响。但我认为它令人困惑和分心。
#2
1
A compact version:
一个紧凑的版本:
nameList.map!{ |x| x.split.reverse.join(' ') }
#=> ["Blow Joe", "Sue Mary", "Mallory Alice"]
#3
0
The Problem
Everything in Ruby has a return value. In this case, you aren't using the return value of your method chain; you're just printing the original value.
Ruby中的所有东西都有返回值。在这种情况下,您没有使用方法链的返回值;你只是打印原始值。
nameList.each { |x|
# Returns a result, but you aren't doing anything with it.
x.to_s.split(' ').reverse!.join(' ')
# Here you print your original value.
puts x #testing here
}
The Solution
The simple way to do this is to use #collect, which returns an array.
简单的方法是使用#collect,它返回一个数组。
p nameList.collect { |name| name.split.reverse.join(' ') }
["Blow Joe", "Sue Mary", "Mallory Alice"]
=> ["Blow Joe", "Sue Mary", "Mallory Alice"]
This prints a modified array, and also returns an array as a result for further processing. Alternatively, if you really want to change your array, you can assign the result like so:
这将打印一个修改后的数组,并返回一个数组作为进一步处理的结果。或者,如果您真的想要更改数组,您可以这样分配结果:
nameList = nameList.collect { |name| name.split.reverse.join(' ') }
=> ["Blow Joe", "Sue Mary", "Mallory Alice"]
nameList
=> ["Blow Joe", "Sue Mary", "Mallory Alice"]
There are certainly other ways to do this, but it's important to keep it readable. In this case, it makes sense to maintain the semantics of assigning an array to your variable so that the intent is clear.
当然,还有其他方法可以做到这一点,但是保持可读性是很重要的。在这种情况下,维护为变量分配数组的语义是有意义的,这样目的就很清楚了。