Getting runtime code error: In '+': no implicit conversion of Integer into String TypeError
num = 5;
puts ("this is number: " + num);
Actual Result:
runtime code error: In '+': no implicit conversion of
Integer
intoString
TypeError
Expected Result - I should see printed statement -
this is number: 5
Don’t put spaces between method names and opening parentheses in the first place.
The cause of the error one cannot add numbers to strings, ruby prevents implicit coercion. One might use string interpolation:
puts "this is number: #{num}"
or convert the number to string explicitly:
puts("this is number: " + num.to_s)
Sidenote: semicolon at the end of the line is redundant and should be avoided.
you need to convert the variable num
into string
puts("this is number: "+ num.to_s)
refer the official docs https://www.rubyguides.com/2018/09/ruby-conversion-methods/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With