I doubt I'll have another release of ZenHacks, so.... STIW4

| | Comments (4)

for apeiros:

class Numeric
  def commify(dec='.', sep=',')
    num = to_s.sub(/\./, dec)
    dec = Regexp.escape dec
    num.reverse.gsub(/(\d\d\d)(?=\d)(?!\d*#{dec})/, "\\1#{sep}").reverse
  end
end

4 Comments

16000324.commify('.') # =>

~> -:4:in `escape': wrong number of arguments (0 for 1) (ArgumentError) ~> from -:4:in `commify' ~> from -:9

Is there a lib I need to get Regexp.escape?

doh. should be fixed.

Excellent; thanks for sharing.

Leave a comment