为什么Ruby连接而不是添加2个值?

Zac*_*iro 0 ruby concatenation add

我写了一个小程序来测试Textmate 2(我对Ruby很新),并且由于某些原因它正在吐出4 + 9 = 49而不是13.

def add(x,y)
  c = x + y
  return c
end

puts "please enter a value "
a = gets.chomp
puts "please enter another value "
b = gets.chomp

printMe = add(a,b)

puts printMe
Run Code Online (Sandbox Code Playgroud)

Vas*_*ich 7

这是因为gets返回一个字符串:

def add(x,y)
  c = x + y
end

puts "please enter a value "
a = gets.to_i
puts "please enter another value "
b = gets.to_i

printMe = add(a,b)

puts printMe
Run Code Online (Sandbox Code Playgroud)