When I type this:
puts 'repeat' * 3
I get:
>> repeat repeat repeat
But it’s not working if I do this:
puts 3 * 'repeat'
Why?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In Ruby, when you call
a * b, you’re actually calling a method called*ona. Try this, for example:Thus
<String> * <Fixnum>works fine, because the*method onStringunderstands how to handle integers. It responds by concatenating a number of copies of itself together.But when you do
3 * "repeat", it’s invoking*onFixnumwith aStringargument. That doesn’t work, becauseFixnum‘s*method expects to see another numeric type.