Ruby不会分裂字符串

时间:2018-04-04 19:53:08

标签: ruby

我试图将用户输入年龄除以2.我的代码如下:

puts "what is your name?"
name = gets.chomp

puts "when were you born please enter your birthdate"
birthdate = gets.chomp

puts "how old are you "
age = gets.chomp

puts "hello"  + name + " wow that is a good day to be born" + "thats a great age"
puts "the half  of your age is" + age/2 + " that is good to know"

它不起作用。

1 个答案:

答案 0 :(得分:4)

您的age是一个字符串

age = gets.to_i

现在是一个数字。但是你不能连接一个字符串和一个数字。两个选项:

插值

puts "the half  of your age is #{age/2} that is good to know"

puts "the half  of your age is " + (age/2).to_s + " that is good to know"