Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
def number_with_delimiter(number, delimiter=",", separator=".")
begin
parts = number.to_s.split('.')
parts[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{delimiter}")
parts.join separator
rescue
number
end
end

rdp commented Jul 5, 2011

why is there a rescue in there... ?

Owner

jpemberthy commented Jul 5, 2011 edited

calling number_with_delimeter("") will raise:

NoMethodError: undefined method `gsub!' for nil:NilClass
from (irb):3:in `number_with_delimiter'
from (irb):25
from /Users/jpemberthy/.rvm/rubies/ruby-1.9.2-p180/bin/irb:16:in `<main>'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment