Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我在 Ruby 中有一个数组,比如["dog", "cat", bat"]. 如何获得一个数组,其中每个元素都传递给特定方法?例如,如果该方法反转输入字符串,那么我希望得到["god", "tac", "tab"]. 我知道迭代元素很容易,将它们传递给方法,然后将结果放入数组中。但是有没有更短的方法?
["dog", "cat", bat"]
["god", "tac", "tab"]
编辑:我也不想修改原始数组。
["dog", "cat", "bat"].map { |word| word.reverse }
或者
["dog", "cat", "bat"].map &:reverse