Skip to content

Commit

Permalink
Add Rational#as_percentage_of method
Browse files Browse the repository at this point in the history
  • Loading branch information
timcraft committed Oct 22, 2024
1 parent 48a6605 commit b373c7b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/percentage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ def to_percentage
def percent_of(n)
n * Percentage(self)
end

def as_percentage_of(n)
Percentage.new(self / n)
end
end

class BigDecimal
Expand Down
9 changes: 9 additions & 0 deletions spec/percentage_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,15 @@
expect(Rational(90).percent_of(Rational(150, 2))).to eq(Rational(135, 2))
end
end

describe '#as_percentage_of' do
it 'returns a percentage object with the value of the decimal divided by the argument' do
percentage = Rational(50).as_percentage_of(Rational(100))

expect(percentage).to be_a(Percentage)
expect(percentage.value).to eq(Rational(1, 2))
end
end
end

RSpec.describe BigDecimal do
Expand Down

0 comments on commit b373c7b

Please sign in to comment.