Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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
rdp commented

why is there a rescue in there... ?

@jpemberthy
Owner

calling number_with_delimeter("") will rise:

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>'

Anyways, I guess that's a programmer's responsibility, probably that rescue is not needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.