-3

変数が整数かどうかをテストしようとしています。コードは次のとおりです。

if (params[:int1].is_a?(Integer) && params[:int2].is_a?(Integer))
  add(params[:int1], params[:int2])
else
  puts "Need two integers"
end

それがうまくいかない理由を知っているなら、あなたは私のすべての注目を集めています.

4

2 に答える 2

0

params[] stores only strings. You need to cast them to integers.

Try something like:

params[:int1].empty? ? raise EmptyIntegerException : my_int1 = params[:int1].to_i
于 2013-09-06T13:11:05.263 に答える