This gem will easily help you to perform currency conversion and arithmetics with different currencies.
Add this line to your application's Gemfile:
gem 'monefy'
And then execute:
$ bundle
Monefy.conversion_rates('EUR', {
'USD' => 1.11,
'Bitcoin' => 0.0047
}) # => {"USD"=>1.11, "Bitcoin"=>0.0047, "EUR"=>1}
fifty_eur = Monefy.new(50, 'EUR')
# => #<Monefy:0x... @amount=50.0, @currency="EUR">
fifty_eur.convert_to('USD')
# => #<Monefy:0x... @amount=55.5, @currency="USD">
fifty_eur.convert_to('Bitcoin')
# => #<Monefy:0x... @amount=0.24, @currency="Bitcoin">
fifty_eur + Monefy.new(20, 'USD')
# => #<Monefy:0x... @amount=68.02, @currency="EUR">
fifty_eur - Monefy.new(0.03, 'Bitcoin')
# => #<Monefy:0x... @amount=43.62, @currency="EUR">
fifty_eur / 2
# => #<Monefy:0x... @amount=25.0, @currency="EUR">
fifty_eur * 3
# => #<Monefy:0x... @amount=150.0, @currency="EUR">
fifty_eur > Monefy.new(54.55, 'USD') # => true
Monefy.new(180, 'USD') < Monefy.new(0.73, 'Bitcoin') # => false
Monefy.new(54.55, 'USD') == Monefy.new(49.14, 'EUR') # => true
Monefy.new(35, 'USD') != Monefy.new(49.14, 'EUR') # => true
Monefy.new(50, 'USD')
# => StandardError: No conversion rates set
# Solution
# Set your conversion rates on your Initialize file
Monefy.conversion_rates('EUR', {
'USD' => 1.11,
'Bitcoin' => 0.0047
}) # => {"USD"=>1.11, "Bitcoin"=>0.0047, "EUR"=>1}
Monefy.new(50, 'INVALID-CURRENCY')
# => StandardError: "Invalid currency"
Monefy.new(50, 'EUR').convert_to('INVALID-CURRENCY')
# => StandardError: "Invalid currency"
Monefy.new(50, 'EUR') == 50
# => StandardError: "Not a Monefy instance"
Monefy.new(50, 'EUR') > "40.0 EUR"
# => StandardError: "Not a Monefy instance"
Monefy.new(50, 'EUR') + 50
# => StandardError: "Not a Monefy instance"
Monefy.new(50, 'EUR') - "40.0 EUR"
# => StandardError: "Not a Monefy instance"
Monefy.new(50, 'EUR') / Monefy.new(15, 'EUR')
# => StandardError: "Not a numeric"
Monefy.new(50, 'EUR') * "40.0 EUR"
# => StandardError: "Not a numeric"
After checking out the repo, run bin/setup
to install dependencies. Then, run rake test
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
.
Bug reports and pull requests are welcome on GitHub at https://github.com/wwwbruno/monefy.
The gem is available as open source under the terms of the MIT License.