[#1026] Is this a bug? — Dave Thomas <Dave@...>

18 messages 2000/01/03

[#1084] Infinite loop — Dave Thomas <Dave@...>

17 messages 2000/01/11

[#1104] The value of while... — Dave Thomas <Dave@...>

24 messages 2000/01/11

[ruby-talk:01173] Re: Possible bug in ruby-man-1.4

From: Dave Thomas <Dave@...>
Date: 2000-01-24 22:01:21 UTC
List: ruby-talk #1173
Huayin Wang <wang@rjka.com> writes:

> does this have anything to do with the following?
> 
> irb(main):001:0> -2 ** 2
> -4
> irb(main):002:0>

Sure is!

You can play around with trace_func to see what's happening:

     set_trace_func proc{|event, file, line, id, binding,klass,*rest|
       print "#{event} at #{file}:#{line} - #{id.id2name} in #{klass}\n"
     }

     -2**2

produces:

     line at t.rb:5 -  in false
     c-call at t.rb:5 - ** in Fixnum
     c-return at t.rb:5 - ** in Fixnum
     c-call at t.rb:5 - -@ in Fixnum
     c-return at t.rb:5 - -@ in Fixnum

It calls the ** method in Fixnum, generating a new Fixnum, then calls
the -@ (unary minus) method in that object.

Until Matz's change, you can get around this with (-2)**2

Regards

Dave

In This Thread