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.
Related
num1 = 2
num2 = 4
op = num1 + num2
puts "enter a number would like to multiply with 6"
num3 = gets.to_i * op
print ("its " + num3 + " man")
Why doesnt this work? If i have it as 'print num3' alone itll work. What is going on?
The answers to this question, Concatenating string with number in ruby, outline two ways you can do it via either:
string interpolation
or, using .to_s to change the number to a string when constructing the result string
Using .to_s you can do this:
print ("its " + num3.to_s + " man")
Although using string interpolation may be a better approach.
Described here:
https://ruby-doc.org/core-2.5.0/doc/syntax/literals_rdoc.html
You can't concat Fixnum and string.
Please do it like this.
print ("its #{num3} man")
If i have it as print num3 alone it'll work. What is going on?
print can indeed print arbitrary objects using their to_s methods, even multiple objects.
But in order to do so, you have to pass them as separate arguments:
num = 42
print('its ', num, ' man')
# its 42 man
However, it's more idiomatic to use string interpolation:
print("its #{num} man")
# its 42 man
or printf which takes a template string: (%d means decimal number)
printf('its %d man', num)
# its 42 man
Note that none of the above will print a newline. If you want one, you have to append \n yourself, or use puts which adds it automatically.
The print statement given below executes correctly when I remove the parentheses and produces an error syntax error, unexpected ',', expecting ')' when parentheses are kept. Any help would be appreciated since I am new to Ruby
print "Enter name: "
name=gets
puts name
puts "Enter first number"
num1=gets
puts "Enter Second number"
num2=gets
result=Integer(num1)+Integer(num2)
print ("The addition of "+num1.chomp+" and " + num2.chomp+ " is ",result)
There are several ways to achieve what you want:
get rid of the space between print and (
print("The addition of " + num1.chomp + " and " + num2.chomp + " is ", result)
use + to concatenate the strings; this will require you to use to_s to convert the numeric value result into a string:
print("The addition of " + num1.chomp + " and " + num2.chomp + " is " + result.to_s)
use string interpolation:
print("The addition of #{num1.chomp} and #{num2.chomp} is #{result}")
Do you have a Python background? This code looks like Python written in Ruby ;)
print 'Enter first number: '
num1 = gets.to_i
print 'Enter Second number: '
num2 = gets.to_i
puts format('The addition of %d and %d is %d', num1, num2, num1 + num2)
String#to_i is more often used than Integer(). Also, both Integer() and to_i ignore newlines, so you don't need to call chomp.
Kernel#format is a good and fast way to integrate variables in a string. It uses the same format as C/Python/Java/...
print "Enter name: "
name=gets.strip ##strip method removes unnecessary spaces from start & End
puts "Enter first number"
num1=gets.strip.to_i ##convert string number into Int
puts "Enter Second number"
num2=gets.strip.to_i
result=num1+num2
print "The addition: #{num1} + #{num2} = #{result}" ##String interpolation.
This is a simple Ruby code, but I am not getting how to understand and use it.
def string_length_interpolater(incoming_string)
"The string you just gave me has a length of #{incoming_string.length}"
end
Could somebody help me the process?
It returns a string with the phrase shown ("The string you...") plus the length of the string passed in, e.g.
string_length_interpolater('Hi')
=> "The string you just gave me has a length of 2"
string_length_interpolater('Hi There')
=> "The string you just gave me has a length of 8"
string_length_interpolater('123456789')
=> "The string you just gave me has a length of 9"
string_length_interpolater('Hello Ruby')
=> "The string you just gave me has a length of 10"
As you can see the method is just returning that text - "The string you just gave me has a length" with the length of the parameter that is passed in. The #{} within the DOUBLE quotes means work out the ruby value and then use that in the string.
It is also the same as
def string_length_interpolater(incoming_string)
"The string you just gave me has a length of " + incoming_string.length
end
When the output becomes comples - a string with 'ruby output at various points it becomes easier to use the doubles qoutes ('interpolation') method, for example this:
"From #{start} to #{end} the #{person} used a #{tool}"
is often easier to read/write/maintain than
"From " + start + " to " + end + " the " + person + " used a " + tool
Added: If you ALSO want to show the value of the string itself you could use:
def string_and_length(nm)
"Hello there #{nm}, did you know your name is #{nm.length} letters long?"
end
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.