我在RailsCast中找到了这段代码:
def tag_names
@tag_names || tags.map(&:name).join(' ')
end
Run Code Online (Sandbox Code Playgroud)
什么是(&:name)中map(&:name)意味着什么?
这是我的代码,我不知道为什么这不会返回预期的结果: A Bunny Hops
text= "a bunny hops"
final = text.split.each{|i| i.capitalize}.join(' ')
puts final
Run Code Online (Sandbox Code Playgroud) 我正在尝试大写一个数组,其中一些字符串由多个单词组成,例如:
array = ["the dog", "the cat", "new york"]
Run Code Online (Sandbox Code Playgroud)
我尝试使用:
array.map(&:capitalize)
Run Code Online (Sandbox Code Playgroud)
但这只是将每个字符串的第一个字母大写。有什么建议?(我对编码很陌生^_^)
我想要的输出是:
["The Dog", "The Cat", "New York"]
Run Code Online (Sandbox Code Playgroud)