On Thu, Dec 8, 2011 at 11:09 AM, Rob Marshall <robmarshall / iglide.net> wrote:
> Hi Robert,
>
> Well, for someone who is new to Ruby, how would I, or anyone for that
> matter, know that doing: '11.to_s *2' converts the number 11 to binary
> when '11.to_s * 2' and 11.to_s*2 both do string multiplication as
> expected. Especially since whitespace isn't supposed to matter.

Wherever you heard whitespace doesn't matter, forget it; it's not true.

This same issue happens in some cases with unary operators too. Observe:

puts -1    # -1
puts 1-1   # 0
puts 1 -1  # 0
puts 1- 1  # 0
puts 1 - 1 # 0

x = 1
puts x -1  # 0
puts x- 1  # 0
puts x - 1 # 0

def y
  2
end

puts y -1  # ArgumentError: wrong number of arguments (1 for 0)
puts y- 1  # 1
puts y - 1 # 1

i.e. whitespace is ignored in all cases *except* when a method name is
to the immediate left of a minus sign leaning directly against another
object; in those cases, the minus sign is interpreted as a unary
operator. (The same goes for unary plus, which I'm guessing is
provided for consistency's sake.)