Forum: Ruby on Rails number_to_currency() helper == non DRY == could be improved

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Mats P. (Guest)
on 2006-03-03 21:38
(Received via mailing list)
Hi all,

Apologies in advance if I got this one wrong, I'm still just a
newbie ;-)

Going through the Apple ADC RoR tutorial it suddenly occurred to me
that the 'number_to_currency()' helper method was not DRY streamlined.

The "number_to_currency()" methods options hash works great when I
need to dynamically display different currencies when I call
"number_to_currency()", as long a I have an idea of what the currency
should be each time ;-)

But, what if I want all instances of a currency to display as GBP, £,
SEK or any other currency ? Then I would have to create a CONSTANT
for that currency and then include that option in every instance of
the 'number_to_currency()' method.

number_to_currency(0.02)    versus     number_to_currency(0.02,

Not to bad some may say, but it's still not exactly DRY nor elegant
is it ?

Instead I would propose as similar scheme to the 'session_key'
override functionality that I use a lot.

##  add a custom session ID for this site (when developing with many
apps on local system)
ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS[:session_key] =

Whereby we could define the DEFAULT_CURRENCY_TYPE in the
environment.rb or something similar.

However, an app could also need to be localized, ie I might want the
default currency to be £ and someone else might want the � (euro) in
their session of the same app.

So, I see this issue, but is not (yet) knowledgeable enough to figure
out a proper fix for it and submit it as a ticket, hence this post
here, for someone else to pick up and hopefully 'run' with ;-)  As I
said above, apologies if this is the wrong approach or a stupid idea.

Kind regards,


"TextMate, coding with an incredible sense of joy and ease"
- -
Carl-Johan K. (Guest)
on 2006-03-04 11:20
(Received via mailing list)
Why don't you just define your own helper action that calls
number_to_currency with the proper options? Something like:

def number_to_pounds (number)
    number_to_currency(number, {:unit => "£", :separator => ",",
:delimiter => ""})

/ CJ
This topic is locked and can not be replied to.