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.
假设我有一个数组,其中包含两个字母单词,一直到十五个字母单词。如何从数组中只得到三个字母的单词?
a = ['aa', 'ab', 'ad', ... , 'zoogeographical'] b = [] a.each do |x| if x.length = 3 b.push(x) end end
似乎会有更简单的方法。
我认为该select方法是您正在寻找的方法:
select
a.select {|string| string.length == 3 }