From: Josh Cheek Date: 2011-12-09T11:43:38+09:00 Subject: Re: Confusing results from string multiplication --f46d04447f1158c1a604b39fc020 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable 2011/12/8 Matthias W=E4chter > On 08.12.2011 17:16, Rob Marshall wrote: > >> I thought, maybe this is a problem with irb, so I wrote a little >> program: >> >> #!/usr/bin/env ruby >> >> puts (11.to_s * 2) >> puts (11.to_s *2) >> puts (11.to_s*2) >> >> And when I run it, I see: >> >> 1111 >> 1011 >> 1111 >> > > but it works correctly for other numbers. try > > puts (10.to_s *2) > > ;) > > lol, evil evil evil! If I was trying to figure out what was going on, using these numbers, I'd be so exasperated. Poor guy got stuck with 11 as his number, how confusing >.< On Thu, Dec 8, 2011 at 11:09 AM, Rob Marshall wrote: > Sorry, seems very confusing to me...Not that I think something like this > happens all that often, but it would be a potentially hard bug to track > down. I could probably look at it all day and not know why the math > wasn't working. For example: > > Oh, it is. Just, as Robert said, it's "a strange corner case". > Does this still happen on 1.9.3? > > Yeah, it's not a bug, it's just that the particular numbers you were using really obfuscated the problem. If you'd had like `123456789.to_s *2`, it would have been a lot more obvious. On Thu, Dec 8, 2011 at 11:32 AM, Rob Marshall wrote: > Well, I do, at least to some degree, understand what's happening. I > still find it a bit confusing that, in this case, '11.to_s *2' means: > convert 11 to binary and then to a string, when: '11.to_s*2' means: > convert 11 to a string and then multiply the string times two. To be > consistent, they should both do the same thing. > > It will actually tell you if you run with warnings (I never do, b/c I use the splat operator w/o parens relatively frequently) $ ruby -w -e 'puts 11.to_s *2' -e:1: warning: `*' interpreted as argument prefix 1011 --f46d04447f1158c1a604b39fc020--