Simple Ruby Program...Help Needed - ruby

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 + "?"

Related

Beginner on Ruby making a calculator

this is my first entry to StackOverflow and I'm a newbie coder.
So I'm making a simple addition calc and I added commas in the last 2 lines to print out integers ...
What am I missing? The error says
C:/Ruby193/rubystuff/ex1.rb:13: syntax error, unexpected ',' print
("The result of the addition is " +,result)
I thought this was the right thing to do ... i must have missed something simple.
print ("Please enter your name: ")
name = gets
puts ("Hello, " + name)
print ("Enter a number to add: ")
num1 = gets
print ("Enter a second number to add: ")
num2 = gets
result = Integer(num1) + Integer(num2)
print result
print ("The result of the addition is ",result)
print ("So the result of adding " + num1.chomp + " plus " + num2.chomp + " equals: ",result)
Ruby has string interpolation and I think most would argue that's the most idiomatic way of doing things. RubyMonk does a great job explaining it here
by changing the 'print' call to the puts method you can do:
puts "The result of the additions is #{result}"
There are two ways to pass arguments to a method:
in parentheses directly after the method name
without parentheses with whitespace after the method name
You have white space after the method, ergo you are using option #2 and are passing a single argument ("The result of the addition is ",result) to the method, but ("The result of the addition is ",result) is not legal syntax.

Basic Ruby gets.chomp

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.

'+' can't convert Fixnum into String (TypeError)

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.

Adding a number to a phrase

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.

Very Basic Ruby puts and gets

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.

Resources