While researching some finance related Ruby software, I came across the "Ruby Money Gem". It looked cool, but it isn't in the Debian repositories as of December 2009, so I didn't try it.
I ran into some issues with Ruby Float arithmetic, read a suggestion to deal with cents instead of dollars to use integers instead of floating point numbers, and I decided to do that as well. To do that, I'm extending the Ruby string object, like this:
class RdMoney < String
def no_d
return (self.gsub(/[^0-9\.]/,'').to_d * 100).to_i
end
end
The "no_d" function removes any non-integer characters except a dot, then converts it to a BigDecimal object (like a Float object, but without the float issues), multiplies it by 100, then converts it to an integer object.
Recently I reviewed the Ruby Money gem code and it appears that it uses a similar approach. I also found that there are many other ruby gems based off of the Ruby Money gem.
In the future, I might use one of those gems with Regdel, but for now, the simple "no_d" function should be all right.
