if / else条件があり、ifセクションとelseセクションが同じである場合は、使用する演算子を除きます。ある場合<
には、そして他の場合には>
。その演算子を条件付きで設定して、コードをドライアウトする方法はありますか?
if count_year < end_year
while count_year <= end_year
if count_year % 4 == 0
if count_year % 100 != 0
all_years << count_year unless (count_year % 400 == 0)
end
end
count_year += 1
end
puts all_years
elsif count_year > end_year
while count_year >= end_year
if count_year % 4 == 0
if count_year % 100 != 0
all_years << count_year unless (count_year % 400 == 0)
end
end
count_year -= 1
end
puts all_years.reverse
end
これは、指定された2年間のうるう年を印刷するためのプログラムの一部です。ループを2回繰り返さなくてもいい方法があるに違いないと思います。次のようなもの:count_year < end_year ? operator = "<" : operator = ">"
-次に、その変数を使用して、演算子をコードブロックなどに置き換えますか?何か案は?