I want to print numbers in my rails application with commas. As per the answer here, I could wrap every single number I print with
number_with_delimiter(@number, :delimiter => ',')
However, I don't want to go into my views and apply this manually. I'd much rather override the way integers are printed.
In java/jsp, anything inside a <%= %>
tag gets a toString()
call to evaluate what is printed on the page, so I figured that overriding the to_s
method for the Integer
class would do:
class Integer
def to_s
number_with_delimiter(self, :delimiter => ',')
end
end
Unfortunately, this doesn't work in that the numbers printed using the <%=%>
tag don't appear with commas. (No errors are raised.)
How do I get this right? Does the <%=%>
block not automagically call a to_s
method on the given object? How does it evaluate what to print?