如果你想拆分以空格分隔的单词列表,你可以使用
def words(text)
return text.split.map{|word| word.downcase}
end
类似于Python的列表理解:
words("get out of here")
返回 ["get", "out", "of", "here"]
。如何将 block 应用于字符串中的每个字符?
最佳答案
使用String#chars
:
irb> "asdf".chars.map { |ch| ch.upcase }
=> ["A", "S", "D", "F"]
关于ruby - 字符串的 map() 的 Ruby 等价物是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22584937/