ruby-duration icon indicating copy to clipboard operation
ruby-duration copied to clipboard

Error with negative quantities

Open smcabrera opened this issue 9 years ago • 1 comments

Unless I'm missing something really obvious there seems to be a problem with negative numbers. For example if I instantiate a couple of Duration objects...

a = Duration.new(10)
=> #<Duration:0x007f21aa066c88 @seconds=10, @negative=false, @total=10, @weeks=0, @days=0, @hours=0, @minutes=0>
b = Duration.new(-5)
=> #<Duration:0x007f21a9e97150 @seconds=5, @negative=true, @total=5, @weeks=0, @days=0, @hours=0, @minutes=0>

They correctly report whether or not they are negative:

a.negative?
=> false
b.negative?
=> true

But then if I try to add a couple durations together the negative duration doesn't seem to behave as negative:

c = a + b
=> #<Duration:0x007f21a902f180 @seconds=15, @negative=false, @total=15, @weeks=0, @days=0, @hours=0, @minutes=0>

I get a positive 15.

smcabrera avatar Mar 04 '15 04:03 smcabrera

I've submitted a pull request for a first run at a fix for this problem. Please let me know what you think.

smcabrera avatar Mar 04 '15 18:03 smcabrera