Or you could just use 'gsub' instead of 'gsub!' and avoid the nil _Kevin -----Original Message----- From: Jeffrey Schwab [mailto:jeff / schwabcenter.com] Sent: Tuesday, February 28, 2006 11:54 AM To: ruby-talk ML Subject: Re: Formatting to "Thousands" James Edward Gray II wrote: > def commify( number ) > number.to_s.reverse.gsub!(/(\d\d\d)(?=\d)(?!\d*\.)/, '\1,').reverse > end That's nice, except that gsub! returns nil if no substitutions are performed, e.g. if the number has fewer than 4 digits. This can result in an exception like: main.rb:3:in `commify': undefined method `reverse' for nil:NilClass(NoMethodError) Of course, you could just break the chain across multiple lines: def commify(number) s = number.to_s s.reverse! s.gsub!(/(\d\d\d)(?=\d)(?!\d*\.)/, '\1,') s.reverse! end