Python には優れた機能があります。
print([j**2 for j in [2, 3, 4, 5]]) # => [4, 9, 16, 25]
Ruby ではさらに簡単です。
puts [2, 3, 4, 5].map{|j| j**2}
しかし、ネストされたループに関するものであれば、Python の方が便利に見えます。
Python では、次のことができます。
digits = [1, 2, 3]
chars = ['a', 'b', 'c']
print([str(d)+ch for d in digits for ch in chars if d >= 2 if ch == 'a'])
# => ['2a', '3a']
Ruby で同等のものは次のとおりです。
digits = [1, 2, 3]
chars = ['a', 'b', 'c']
list = []
digits.each do |d|
chars.each do |ch|
list.push d.to_s << ch if d >= 2 && ch == 'a'
end
end
puts list
Rubyには似たようなものがありますか?