文字列の配列を数値化したいです。
print "数字を2つ入力してください"
num = gets.split(" ")
num.map(&:to_i)
x = num[0]
y = num[1]
if x < 2 && 2 < y then
puts "OK"
else
puts "NO"
end
とした場合に、est2.rb:6:in <': comparison of String with 2 failed (ArgumentError) from test2.rb:6:in
<main>というエラーとなり、map(&:to_i)が効いておらず、Stringのままです。
x = num[0]
y = num[1]
を
x = num[0].to_i
y = num[1].to_i
とすると、うまく通ることは確認しておりますが、配列内の文字列を一括で数値化する方法があれば、教えて下さい。
よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2015/11/18 13:18