Created
September 21, 2012 08:17
-
-
Save f440/3760343 to your computer and use it in GitHub Desktop.
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
| #!/usr/bin/env ruby | |
| #-*- coding: utf-8 -*- | |
| # | |
| # This work is in the public domain. | |
| # | |
| class FizzBuzz | |
| def initialize(argument) | |
| if argument < 1 | |
| throw InvalidArgumentError | |
| end | |
| @target = argument | |
| end | |
| def result | |
| result = [] | |
| (1 .. @target).each do |i| | |
| tmp = (i % 3 == 0) ? "Fizz" : "" | |
| tmp += (i % 5 == 0) ? "Buzz" : "" | |
| result << (tmp == "" ? i : tmp) | |
| end | |
| result | |
| end | |
| end | |
| if __FILE__ == $0 | |
| target = ARGV[0].to_i | |
| begin | |
| fizzbuzz = FizzBuzz.new(target) | |
| rescue | |
| abort "usage: #{$0} target_number" | |
| end | |
| fizzbuzz.result.each{|i| puts i } | |
| else /rspec$/.match $0 | |
| require 'rspec' | |
| describe "FizzBuzz.result" do | |
| subject { @result = FizzBuzz.new(20).result } | |
| it { subject.at(0).should == 1 } | |
| it { subject.at(1).should == 2 } | |
| it { subject.at(2).should == "Fizz" } | |
| it { subject.at(3).should == 4 } | |
| it { subject.at(4).should == "Buzz" } | |
| it { subject.at(5).should == "Fizz" } | |
| it { subject.at(6).should == 7 } | |
| it { subject.at(13).should == 14 } | |
| it { subject.at(14).should == "FizzBuzz" } | |
| it { subject.at(15).should == 16 } | |
| end | |
| describe "argument == 0" do | |
| subject { lambda{ FizzBuzz.new(0) } } | |
| it { should raise_error } | |
| end | |
| describe "argument < 0" do | |
| subject { lambda{ FizzBuzz.new(-1) } } | |
| it { should raise_error } | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment