真是个新问题,不好意思。我有一个这样的字符串,由几个单词组成,我想把它变成一个数组,其中每个单词都是数组中的子数组。
my_string = "Made up of several words"
my_array = []
my_string.split(/s/) do |word|
my_array << word
end
给我
["Made", "up", "of", "several", "words"]
但是我想得到:
[["Made"], ["up"], ["of"], ["several"], ["words"]]
有谁知道我该怎么做吗?我使用do end语法,因为我想要一个代码块,接下来我可以添加一些逻辑,围绕我对来自字符串的某些单词所做的事情。谢谢。
下面呢:
my_string = "Made up of several words"
my_string.scan(/(w+)/)
# => [["Made"], ["up"], ["of"], ["several"], ["words"]]
这样行吗?
my_string = "Made up of several words"
my_array = my_string.split(/s+/).map do |word|
[word]
end
# => [["Made"], ["up"], ["of"], ["several"], ["words"]]