Skip to content

Instantly share code, notes, and snippets.

@macks
Created December 27, 2011 10:48
Show Gist options
  • Save macks/1523256 to your computer and use it in GitHub Desktop.
Save macks/1523256 to your computer and use it in GitHub Desktop.
Integr#commalize
class Integer
def commalize
to_s.gsub(/\d(?=.{3}+$)/, '\0,')
end
end
if defined?(RSpec::Core::Runner)
describe 'Integer#commalize' do
[
[ 321, '321'],
[ -321, '-321'],
[ 4_321, '4,321'],
[ -4_321, '-4,321'],
[ 654_321, '654,321'],
[ -654_321, '-654,321'],
[ 7_654_321, '7,654,321'],
[-7_654_321, '-7,654,321'],
].each do |(int, string)|
it "returns #{string.inspect} for #{int}" do
int.commalize.should == string
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment