I am attempting to run the following program, which will give out a simple greeting taking user input. However, whenever I run the code I get the following message:
syntax error, unexpected tSTRING_BEG, expecting do or '{' or '('
I've tried replacing the single quote marks with doubles, and I've also tried placing '()' around the variable names.
puts 'First name: '
first_name = gets.chomp
puts 'Middle name: '
middle_name = gets.chomp
puts 'Surname: '
surname = gets.chomp
puts "Greets to you," + first_name + middle_name + surname "! Welcome to Valhalla!"
#eux already gave a correct answer to the syntax error in your example.
I just want to show another way to generate the expected output. It's a common Ruby idiom to use string interpolation instead of string concatenation. With string interpolation, you can generate your output like this
puts "Greets to you, #{first_name} #{middle_name} #{surname}! Welcome to Valhalla!"
String interpolation has many advantages – as described in this answer and its comments. In the context of your question the pros are:
It is shorter
It is much clearer where to put whitespace
And It is overall easier to read and understand
You missed a + before ! Welcome to Valhalla! at the last line:
puts "Greets to you," + first_name + middle_name + surname + "! Welcome to Valhalla!"
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
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.
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.
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.
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.