"Inches/yard: #{12*3}" "#{"Tora! "*3}"The second example is confusing, unless you remember that everything is an object in
Ruby (yes, even string literals! They are of class String.). Since the string literal
creates a String object, you can act on it just like any other object. In this case,
multiplying a string by 3 simply does what you would think: makes three copies of the
string.
I read the above para in one of the ruby book. The first line says The second example is confusing, unless you remember that everything is an object in Ruby. What is there in the second example that i should remember ,everything is an object in ruby ? Isn’t it just a feature that multiplying by 3 will print tora three times ?
I don’t exactly understand what does the author want me to understand from the above paragraph
Well, yes you can consider it a feature maybe, but what the author is perhaps trying to explain (although not very clearly at least in this one paragraph), is that what actually is happening is this:
"Tora !"is an object of classString(“everything is an object”)“you can call any method on it just like any other object”.
In this case you are calling the method
*(multiply).So what ACTUALLY is happening is that the
"Tora !"Stringgets called in a fashion like this:You see? The operator
*is just a method on theStringobject.In many simpler languages operators are actually “baked into” the language itself, and do not operate on the targets as method calls.
If you’re not used to other languages you might not find it all that remarkable, since in Ruby it’s just a normal everyday thing. You just never need to type
1.+(2), Ruby does it for you automatically when you type1 + 2.So this is what the author wants you to remember – all operators and operations are just essentially method calls on other objects.