I can print a raw number with this code:
puts 'Please enter your favorite number'
favNumber = gets.chomp
betterNumber = favNumber.to_i
puts betterNumber + 1
but I need to set a message including the number. I changed the last two lines to this, but it's wrong.
betterNumber = favNumber.to_i + 1
puts 'Your favorite number sucks, a better number is '+ betterNumber + '!'
Help me.
betterNumber is of class Fixnum and your string is of course of class String. You can't add a String and a Fixnum, you need to cast your Fixnum into a String using to_s.
"Your favorite number sucks, a better number is " + betterNumber.to_s + "!"
Also, using interpolation calls to_s on any objects being interpolated. So this works, too (and is more common):
"Your favorite number sucks, a better number is #{betterNumber}!"
Also, in Ruby we usually use snake_case variables as opposed to camelCase variables. So I recommend using better_number
You need to convert betterNumber to a string when printing it, like this: betterNumber.to_s.
Related
Trying to solve one of the problems in Chris Pine's book. Asks users to input their first and last names, then it should display the total number of characters of both.
Here's one of many solutions I've come up with:
puts "First name?"
first_name = gets.chomp
puts "Last name?"
last_name = gets.chomp
total = first_name.length.to_i + last_name.length.to_i
puts 'Did you know you have ' + total + ' characters in your name ' + first_name + last_name + '?'
Ruby is pretty strict about the difference between a String and an Integer, it won't convert for you automatically. You have to ask, but you can ask politely:
puts "Did you know you have #{total} characters in your name #{first_name} #{last_name}?"
You can also do the math this way:
puts "Did you know you have #{first_name.length + last_name.length} characters in your name?"
The #{...} interpolation only works inside "double-quoted" strings, but it does convert to a string whatever the result of that little block is. Single quoted ones avoid interpolation, which is sometimes handy.
If you do want to concatenate strings you have to convert manually:
"this" + 2.to_s
Length returns an integer (no need to convert to_i) so change your code to this:
total = first_name.length + last_name.length
I'm new to programming and I'm trying to answer this basic question.
Write a program which asks for a person's favorite number. Have your program add one to the number, then suggest the result as a bigger and better favorite number.
This is what I have to far, but it won't convert to a number.
puts "What is your favorite number?"
number = gets.chomp
number = number.to_i + 1
puts "I suggest " + number + " as a bigger and better number"
Look more closely at the error you get:
What is your favorite number?
42
number.rb:4:in `+': can't convert Fixnum into String (TypeError)
from number.rb:4:in `<main>'
Line 4 is:
puts "I suggest " + number + " as a bigger and better number"
The problem is that Ruby won't implicitly convert number into a string (e.g. "foo" + 42 is not valid in Ruby). There are a couple of solutions:
Call to_s on number to convert it to a string before concatenating:
puts "I suggest " + number.to_s + " as a bigger and better number"
Use Ruby string interpolation:
puts "I suggest #{number} as a bigger and better number"
Option 2 is more idiomatic, I suggest using that.
As in many other problems in ruby there are a lot of ways to do it....without the three solutions writed above there is two more:
puts "What is your favorite number?"
number = gets.chomp.to_i
puts "I suggest %d as a bigger and better number" % [number + 1]
and one wich is almost the same:
puts "What is your favorite number?"
number = gets.chomp.to_i
num = number + 1
puts "I suggest %d as a bigger and better number" % [num]
You can do it this way:
print 'What is your favorite number? '
number = gets.chomp
puts "I suggest #{number.to_i + 1} as a bigger and better number"
There is not to much to explain about the code, but there are few things to take into account:
If you are rendering plain text use 'text' instead of "text". "In the double-quoted case, Ruby does more work. First, it looks for substitutions (sequences
that start with a backslash character) and replaces them with some binary value" - Programming ruby 1.9.3
Always try to reduce the number of lines of code.
This things are really insignificant here, but when you are coding a big program, web page etc., it really makes a difference.
I've hit upon a 'can't convert Fixnum into String (TypeError)' error and whilst it seems simple enough I'm unsure about how to get around it. I thought my logic was sound - convert the entered string variable to an integer and then carry out the basic operation - but apparently I'm missing some key bit of information.
puts 'What is your favourite number?'
favenum = gets.chomp
better = favenum.to_i + 1
puts 'Yeah '+favenum+' is nice enough but '+better+' is bigger and better by far! Think on.'
Have tried searching for an answer but examples of the same error out there are way beyond my rudimentary ruby skills at present.
Ruby (unlike some other languages) does not cast objects to strings when they are operands in String#+ method. Either cast to string manually:
puts 'Yeah ' + favenum.to_s + ' is nice enough but ' + better.to_s + ' is bigger and better by far!'
or use string interpolation (note the double quotes):
puts "Yeah #{favenum} is nice enough but #{better} is bigger and better by far!"
Try using string interpolation, like this:
puts "Yeah #{favenum} is nice enough but #{better} is bigger and better by far! Think on."
Based on the tutorial you're following
puts 'Please enter your favourite number: '
number = gets.chomp
imp = number.to_i + 1
puts 'I\'d say '.to_s + imp.to_s + ' is a much better number.'
Produces the "correct" result at a beginner level.
Just trying to write a simple Ruby program here. I'm trying to get the program to ask for the user's favorite number and then suggest a new favorite number that's one greater. What's wrong with the code below?
puts "hey, whats your favorite number?"
favnumber = gets.chomp
newfavnumber = favnumber.to_i + 1
puts "how about " + newfavnumber "?"
puts "how about " + newfavnumber "?"
First of all, you probably wanted a + before the "?" there. The way this is written now, it parses as puts("how about " + newfavnumber("?")), i.e. you're calling a function called newfavnumber, which is obviously not what you want.
However if you change it to puts "how about " + newfavnumber + "?", which you presumably intended, it still won't work: newfavnumber is a number and "how about " is a string. In ruby you can't add numbers to strings. To fix this you can call to_s on newfavnumber to convert it to a string.
A better way to write this would be using string interpolation: puts "how about #{newfavnumber}?". This way you don't need to call to_s because you can use any type inside #{}.
You're missing a + after newfavnumber and a conversion to string.
puts "how about " + newfavnumber.to_s + "?"
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'
It always helps if you include the error. There are two ways to fix that error:
Interpolate the value: puts "you would like #{number} much better"
Turn it from a number to a string: puts "you would like " + number.to_s + 'much better'
The former, #{...} syntax, evaluates the content of the braces as Ruby, and then applies to_s to 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.