@@ -54,7 +54,7 @@ The development version (hosted on Github) can be installed with:
54
54
55
55
## Usage
56
56
57
- ``` ruby
57
+ ``` ruby
58
58
require ' money'
59
59
60
60
# explicitly define locales
@@ -102,15 +102,15 @@ Currencies are consistently represented as instances of `Money::Currency`.
102
102
The most part of ` Money ` APIs allows you to supply either a ` String ` or a
103
103
` Money::Currency ` .
104
104
105
- ``` ruby
105
+ ``` ruby
106
106
Money .from_cents(1000 , " USD" ) == Money .from_cents(1000 , Money ::Currency .new (" USD" ))
107
107
Money .from_cents(1000 , " EUR" ).currency == Money ::Currency .new (" EUR" )
108
108
```
109
109
110
110
A ` Money::Currency ` instance holds all the information about the currency,
111
111
including the currency symbol, name and much more.
112
112
113
- ``` ruby
113
+ ``` ruby
114
114
currency = Money .from_cents(1000 , " USD" ).currency
115
115
currency.iso_code # => "USD"
116
116
currency.name # => "United States Dollar"
@@ -120,7 +120,7 @@ currency.cents_based? #=> true
120
120
To define a new ` Money::Currency ` use ` Money::Currency.register ` as shown
121
121
below.
122
122
123
- ``` ruby
123
+ ``` ruby
124
124
curr = {
125
125
priority: 1 ,
126
126
iso_code: " USD" ,
@@ -164,7 +164,7 @@ selector like the one available
164
164
custom methods to return the list of major currencies and all currencies as
165
165
follows:
166
166
167
- ``` ruby
167
+ ``` ruby
168
168
# Returns an array of currency id where
169
169
# priority < 10
170
170
def major_currencies (hash )
@@ -194,7 +194,7 @@ all_currencies(Money::Currency.table)
194
194
195
195
A default currency is not set by default. If a default currency is not set, it will raise an error when you try to initialize a ` Money ` object without explicitly passing a currency or parse a string that does not contain a currency. You can set a default currency for your application by using:
196
196
197
- ``` ruby
197
+ ``` ruby
198
198
Money .default_currency = Money ::Currency .new (" CAD" )
199
199
```
200
200
@@ -207,7 +207,7 @@ separator (which separates the major unit from the minor unit). See e.g.
207
207
[ ISO 4217] ( https://www.iso.org/iso-4217-currency-codes.html ) for more
208
208
information. You can find the exponent (as an ` Integer ` ) by
209
209
210
- ``` ruby
210
+ ``` ruby
211
211
Money ::Currency .new (" USD" ).exponent # => 2
212
212
Money ::Currency .new (" JPY" ).exponent # => 0
213
213
Money ::Currency .new (" MGA" ).exponent # => 1
@@ -217,7 +217,7 @@ Money::Currency.new("MGA").exponent # => 1
217
217
218
218
To find a given currency by ISO 4217 numeric code (three digits) you can do
219
219
220
- ``` ruby
220
+ ``` ruby
221
221
Money ::Currency .find_by_iso_numeric(978 ) # => Money::Currency.new(:eur)
222
222
```
223
223
@@ -227,7 +227,7 @@ Exchanging money is performed through an exchange bank object. The default
227
227
exchange bank object requires one to manually specify the exchange rate. Here's
228
228
an example of how it works:
229
229
230
- ``` ruby
230
+ ``` ruby
231
231
Money .add_rate(" USD" , " CAD" , 1.24515 )
232
232
Money .add_rate(" CAD" , " USD" , 0.803115 )
233
233
@@ -237,7 +237,7 @@ Money.ca_dollar(100).exchange_to("USD") # => Money.from_cents(80, "USD")
237
237
238
238
Comparison and arithmetic operations work as expected:
239
239
240
- ``` ruby
240
+ ``` ruby
241
241
Money .from_cents(1000 , " USD" ) <=> Money .from_cents(900 , " USD" ) # => 1; 9.00 USD is smaller
242
242
Money .from_cents(1000 , " EUR" ) + Money .from_cents(10 , " EUR" ) == Money .from_cents(1010 , " EUR" )
243
243
345
345
The following example implements a ` Redis ` store to save exchange rates to a redis database.
346
346
347
347
` ` ` ruby
348
-
349
348
class RedisRateStore
350
349
INDEX_KEY_SEPARATOR = ' _TO_' .freeze
351
350
@@ -409,7 +408,7 @@ Money.from_cents(1000, 'USD').exchange_to('CAD') #=> #<Money fractional:900 curr
409
408
There is nothing stopping you from creating store objects which scrapes
410
409
[ XE] ( https://www.xe.com ) for the current rates or just returns ` rand(2) ` :
411
410
412
- ``` ruby
411
+ ``` ruby
413
412
Money .default_bank = Money ::Bank ::VariableExchange .new (StoreWhichScrapesXeDotCom .new )
414
413
```
415
414
@@ -423,7 +422,7 @@ Money.default_bank = MyCustomBank.new(Money::RatesStore::Memory.new)
423
422
If you wish to disable automatic currency conversion to prevent arithmetic when
424
423
currencies don't match:
425
424
426
- ``` ruby
425
+ ``` ruby
427
426
Money .disallow_currency_conversion!
428
427
```
429
428
@@ -603,7 +602,7 @@ If you don't have some locale and don't want to get a runtime error such as:
603
602
I18n::InvalidLocale : :en is not a valid locale
604
603
605
604
Set the following :
606
- ` ` ` ruby
605
+ ` ` ` ruby
607
606
I18n.enforce_available_locales = false
608
607
` ` `
609
608
0 commit comments