Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add Money#abs

  • Loading branch information...
commit 360421d2caa783a206870b1a2b6f963d0c3755dd 1 parent dafe368
@semmons99 semmons99 authored
Showing with 11 additions and 0 deletions.
  1. +5 −0 lib/money/money.rb
  2. +6 −0 test/money_spec.rb
View
5 lib/money/money.rb
@@ -218,6 +218,11 @@ def remainder(val)
a.modulo(b) - (b.is_a?(Money) ? b : Money.new(b, a.currency))
end
+ # Return absolute value of self as a new Money object
+ def abs
+ Money.new(self.cents.abs, self.currency)
+ end
+
# Test if the money amount is zero
def zero?
cents == 0
View
6 test/money_spec.rb
@@ -430,6 +430,12 @@ def to_money
end
end
+ specify "#abs correctly returns the absolute value as a new Money object" do
+ n = Money.new(-1, :USD)
+ n.abs.should == Money.new( 1, :USD)
+ n.should == Money.new(-1, :USD)
+ end
+
specify "Money.empty creates a new Money object of 0 cents" do
Money.empty.should == Money.new(0)
end
Please sign in to comment.
Something went wrong with that request. Please try again.