josedonizetti / ruby-duration

Immutable type that represents some amount of time with accuracy in seconds.

Home Page:http://bit.ly/ruby-duration

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error with negative quantities

smcabrera opened this issue · 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.

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