Unary operators behavior

前端 未结 2 1895
感情败类
感情败类 2021-01-13 08:04

Had some weird results when redefining the unary + operator in Ruby on the Fixnum class. Not exactly sure why things are happening the way they are

2条回答
  •  走了就别回头了
    2021-01-13 08:24

    I suspect you're seeing a side effect of the parser's behavior in how it interprets numeric literals.

    If we create our own class:

    class C
      def +@
        11
      end
    end
    

    and then look at some things:

    > c = C.new
    > +c
     => 11 
    > ++c
     => 11 
    

    That's exactly what we expect to happen. If we use your Fixnum unary + override and a Fixnum variable:

    > n = 23
    > +n
     => 15 
    > ++n
     => 15 
    

    then again we see what you're expecting. In both cases, we see the result of calling the +@ method on a non-literal.

    But when we look at +6 with your operator in place:

    > +6
     => 6 
    

    the +@ method is not called. Similarly if we override -@:

    class Fixnum
      def -@
        'pancakes'
      end
    end
    

    and see what it does:

    > -42
     => 42
    

    So what's going on here? Well, Ruby is seeing +6 and -42 not as 6.send(:+@) and 42.send(:-@) method calls but as single literals for positive six and negative forty-two.

    If you start adding parentheses, +(6) and -(42), then Ruby sees non-literal expressions and ends up calling the unary methods. Similarly when you double the unary operators.

提交回复
热议问题