I have the following code. However I get a error. How is this supposed to be written.
puts 'What is your favourite number?'
number = gets.chomp
number = number.to_i + 1
puts "you would like " + number + 'much better'
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It always helps if you include the error. There are two ways to fix that error:
puts "you would like #{number} much better"puts "you would like " + number.to_s + 'much better'The former,
#{...}syntax, evaluates the content of the braces as Ruby, and then appliesto_sto the result, before injecting it into the string. My two examples are literally equivalent.As to why it fails?
+doesn’t do type coercion in Ruby, which actually has very little implicit conversion going on, unlike other languages in similar spaces.