[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: (String * Fixnum) and (Fixnum * String

Mohit Sindhwani

8/21/2007 7:01:00 AM

Dan wrote:
> Just a small quick question:
>
> String * Fixnum works:
>
> "test " * 4
> => test test test test
>
>
> while Fixnum * String doesn't:
>
> 4 * "test "
> TypeError: String can't be coerced into
> Fixnum
> from (irb):19:in
> `*'
> from (irb):19
>
> I understand that ruby being strongly typed means you can only do "sensible"
> things with the types you are working with. However, aren't the examples
> above pretty much equivalent? or is it a precedence thing? Trying to answer
> my own question is it because the "*" operator for a Fixnum only accepts
> another Fixnum/Bignum, while the "*" operator for a String only allows a
> Fixnum, because it sure doesn't let you do String * String (and why would
> you really).
>
> Someone please help me grasp this :) Thanks
>
>

As I read it in Chris Pine's excellent 'How to Program', when you do
string * fixnum, you're telling the string to multiply itself 'fixnum'
times - the string knows how to do that.

When you do fixnum * string, you're trying to ask the a fixnum to
multiply itself string times - that makes no sense. Thus, it's not allowed.

Cheers,
Mohit.
8/21/2007 | 3:00 PM.

P.S. I'm new to the group - so, I'll use this email to piggyback and say
HI to everyone!