As I retrieve data from a database table an array is populated. Some of the fields are defined as decimal & money fields and within the array they are represented as BigDecimal.
I use these array values to populate a CSV file, but the problem is that all BigDecimal values are by default represented in Scientific format (which is the default behaviour of the BigDecimal to_s method). I can show the values by using to_s('F'), but how can I override the default?
Ruby makes this easy. Behold:
This technique is called monkey patching. As you might guess from the name, it's something you should use cautiously. This use seems reasonable to me, though.
This is built on @Farrel's answer, but without polluting the method namespace with a useless
old_xyz
method. Also, why not use default arguments directly?In Ruby 1.8, this gets slightly uglier:
Or, if you don't like the warning you get with the above code: