Numbers vs. Strings / Expressions vs. Values
When I type the following:
print "2+2 is equal to" +2+2
An error appears: I cannot convert the number to a string, but when I type:
print "2+2 is equal to", 2+2
it takes it and displays:
2+2 is equal to4
What's the difference between the two? This is not logical for me. Can someone explain this?
source to share
print "2+2 is equal to" + 2 + 2
Here you are trying to add a number to a string. This operation is meaningless. It's like adding an apple to a cat. It will fail, but if it does, it will print
print the result.
print "2+2 is equal to", 2 + 2
This is where you tell the command print
to print that line, as well as the result of summing those two numbers. he knows how to print strings and how to print numbers. Strings and numbers should not be mixed in this case, they are processed separately. This is why this operation succeeds.
You can also perform the first operation. To do this, you must be explicit that you want this number to be a string, so that both additional operands are strings and can actually be added together.
print "2+2 is equal to" + (2 + 2).to_s
or
print "2+2 is equal to #{2 + 2}" # this is called string interpolation
Some languages try to be friendly, and if you add a number to a string, it will jot down the number for you. The results can be ... unexpected.
Javascript:
"2 + 2 equals to " + 2 + 2
# => "2 + 2 equals to 22"
"2 + 2 equals to " + (2 + 2)
# => "2 + 2 equals to 4"
It's good that ruby doesn't do such tricks :)
source to share
Everyone pointed out how it works print
, so I thought I shed some light on +
.
These two operators look the same, don't they?
-
'2'+'2'
-
2+2
There are actually two very different operations going on:
- String # + - This concatenates the argument with the original string. The argument must be a string.
- Fixnum # + - adds an argument to the original number. The argument must be a number.
So if String # + only works with objects string
, how can we print different types of objects?
Some classes are very similar to strings and can be thought of as strings in most contexts (for example, Exception
up to Ruby 1.9) because they implement to_str
(implicit conversion).
We can also implement to_s
in our own objects to allow it to return a representation of the object string
(explicit conversion).
You can read more about this at http://codeloveandboards.com/blog/2014/03/18/explicit-vs-implicit-conversion-methods/
source to share
print "2+2 is equal to" +2+2
equivalent to:
print("2+2 is equal to" +2+2)
You are trying to add an integer 2
to a string "2+2 is equal to"
.
print "2+2 is equal to", 2+2
equivalent to:
print("2+2 is equal to", 2+2)
It print
takes two arguments, one is a string, the other is an expression 2+2
.
source to share
print "2+2 is equal to" + 2+2
fails because it tries to add an integer to the string before the result is sent to print
. An operation that doesn't make sense. Pay attention to:
print "2+2 is equal to", 2+2
- another operation. Here you are sending two arguments to print
. String and integer. Internally print
calls to_s
for both values.
From the documentation :
print(obj, ...)
→nil
Prints each object in turn on
$stdout
. [...] Objects that are not strings will be converted by calling their methodto_s
.
Another way to do this is with string interpolation, which also automatically calls to_s
:
print "2+2 is equal to #{2+2}"
source to share