1
假設我有一個數組,填充兩個字母的單詞一直到15個字母的單詞。如何從數組中只得到三個字母的單詞?根據長度獲取數組的元素
a = ['aa', 'ab', 'ad', ... , 'zoogeographical']
b = []
a.each do |x|
if x.length = 3
b.push(x)
end
end
似乎會有一個更簡單的方法。
假設我有一個數組,填充兩個字母的單詞一直到15個字母的單詞。如何從數組中只得到三個字母的單詞?根據長度獲取數組的元素
a = ['aa', 'ab', 'ad', ... , 'zoogeographical']
b = []
a.each do |x|
if x.length = 3
b.push(x)
end
end
似乎會有一個更簡單的方法。
我覺得select
方法是你在找什麼:
a.select {|string| string.length == 3 }
謝謝!這個竅門! – nizbit 2013-03-07 22:21:19