# Number\_to\_currency() helper == non DRY == could be improved

**URL:** <https://www.ruby-forum.com/t/number-to-currency-helper-non-dry-could-be-improved/76377>\
**Category:** Rails\
**Created:** [March 3, 2006, 8:38pm UTC](https://www.ruby-forum.com/t/number-to-currency-helper-non-dry-could-be-improved/76377 "2006-03-03T20:38:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mats\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e79b87/32.png) [@Mats\_P](https://www.ruby-forum.com/u/Mats_P)\
**Post date:** [March 3, 2006, 8:38pm UTC](https://www.ruby-forum.com/t/number-to-currency-helper-non-dry-could-be-improved/76377/1 "2006-03-03T20:38:54Z")

</div>

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.

Scenario::  
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,  
{:unit =\> MY\_CURRENCY\_CONSTANT} )

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] =  
‘00\_shows\_at\_top\_of\_sorted\_cookies\_list’

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,

Mats

* * *

“TextMate, coding with an incredible sense of joy and ease”

- [www.macromates.com](http://www.macromates.com) -

---

<div class="post-metadata">

**Author:** ![Mats\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e79b87/32.png) [@Mats\_P](https://www.ruby-forum.com/u/Mats_P)\
**Post date:** [March 4, 2006, 10:20am UTC](https://www.ruby-forum.com/t/number-to-currency-helper-non-dry-could-be-improved/76377/2 "2006-03-04T10:20:29Z")

</div>

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 =\> “”})  
end

/ CJ
