Skip to content

Instantly share code, notes, and snippets.

@ssaunier
Last active January 2, 2016 17:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ssaunier/8336988 to your computer and use it in GitHub Desktop.
Save ssaunier/8336988 to your computer and use it in GitHub Desktop.
class IntegerFormatter
def with_commas(integer)
quotient = integer / 1000
remainder = integer % 1000
if quotient == 0
remainder.to_s
else
prepend = with_commas(quotient)
"#{prepend},#{"%03d" % remainder}"
end
end
end
require "./integer_formatter"
describe IntegerFormatter do
subject { IntegerFormatter.new }
describe "#with_commas" do
it "should handle number until 999 without any commas" do
test_with_commas(1, "1")
test_with_commas(10, "10")
test_with_commas(100, "100")
end
it "should handle number between 1000 and 999999" do
test_with_commas(1000, "1,000")
test_with_commas(10000, "10,000")
test_with_commas(100000, "100,000")
end
it "should handle number above 10000000" do
test_with_commas(1000000, "1,000,000")
test_with_commas(35235235, "35,235,235")
end
end
def test_with_commas(integer, expected_string)
subject.with_commas(integer).should eq expected_string
end
end
@simcap
Copy link

simcap commented Oct 2, 2014

👍

Being picky but any reason in the spec file for line subject { IntegerFormatter.new }? It would work without this line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment