9

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 into String TypeError

Expected Result - I should see printed statement -

this is number: 5

1
  • The recommended way to include variables is with string interpolation: "this is number: #{num}" Commented Jun 10, 2019 at 17:40

2 Answers 2

20

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.