Created
December 27, 2011 10:48
-
-
Save macks/1523256 to your computer and use it in GitHub Desktop.
Integr#commalize
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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