Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
def sumof s = 12 + 17 puts "The sum of 12 and 17 is: " + s end
当我调用 sumof时,出现错误
感谢您的帮助
确实有一种方法可以转换为字符串,但它可以以多种方式使用。该方法被调用to_s(到字符串)。
to_s
方式1(手动):
"Some string " + num.to_s
方式2(插值):
"Some string #{num}"
你会想做
"The sum is " + s.to_s或者"The sum is #{s}"
"The sum is " + s.to_s
"The sum is #{s}"
问题是在您的原始示例中没有隐式地转换为字符串。