Skip to content

Instantly share code, notes, and snippets.

@jpemberthy
Created July 21, 2010 16:57
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jpemberthy/484764 to your computer and use it in GitHub Desktop.
Save jpemberthy/484764 to your computer and use it in GitHub Desktop.
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
Copy link

rdp commented Jul 5, 2011

why is there a rescue in there... ?

@jpemberthy
Copy link
Author

jpemberthy commented Jul 5, 2011

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