public
Last active

  • Download Gist
gistfile1.rb
Ruby
1 2 3 4 5 6 7 8 9
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

why is there a rescue in there... ?

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.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.