Separate number by comma with using `gsub`:
def separate_by_comma(number) number.to_s.reverse.gsub(/(\d{3})/, '\1,').reverse endwithout:
def spearate_by_comma(number) number.to_s.chars.reverse.each_slice(3).map(&:join).join(",").reverse endFormat phone number with `gsub`:
def formatPhone(number) numbers = number.to_s.chars "(###) ###-####".gsub(/#/) { numbers.shift } endwithout:
def formatPhone(number) '(%d%d%d) %d%d%d-%d%d%d%d' % number.to_s.chars end
No comments:
Post a Comment