fizzbuzzard 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +6 -0
- data/Gemfile +2 -0
- data/Gemfile.lock +24 -0
- data/README.md +39 -0
- data/fizzbuzzard.gemspec +16 -0
- data/lib/fizzbuzzard.rb +21 -0
- data/spec/fizzbuzzard_spec.rb +7 -0
- metadata +70 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
fizzbuzzard (0.0.1)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: http://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.2.4)
|
10
|
+
rspec (2.13.0)
|
11
|
+
rspec-core (~> 2.13.0)
|
12
|
+
rspec-expectations (~> 2.13.0)
|
13
|
+
rspec-mocks (~> 2.13.0)
|
14
|
+
rspec-core (2.13.1)
|
15
|
+
rspec-expectations (2.13.0)
|
16
|
+
diff-lcs (>= 1.1.3, < 2.0)
|
17
|
+
rspec-mocks (2.13.1)
|
18
|
+
|
19
|
+
PLATFORMS
|
20
|
+
ruby
|
21
|
+
|
22
|
+
DEPENDENCIES
|
23
|
+
fizzbuzzard!
|
24
|
+
rspec
|
data/README.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
## FizzBuzzard
|
2
|
+
|
3
|
+
### Get the goodness of FizzBuzz in all of your apps
|
4
|
+
|
5
|
+
Fizzbuzz is a popular interview question for programmers (so I've read, I've never been asked it).
|
6
|
+
|
7
|
+
It goes like this:
|
8
|
+
|
9
|
+
> Write a program that prints the numbers from 1 to 100. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz".
|
10
|
+
<cite>[from Coding Horror](http://www.codinghorror.com/blog/2007/02/why-cant-programmers-program.html "fizzbuzz")</cite>
|
11
|
+
|
12
|
+
Not difficult. But why restrict the magic of FizzBuzz to an interview, throwing away perfectly working code you could be using in production?
|
13
|
+
|
14
|
+
Now you don't have to. FizzBuzzard monkey patches Fixnum so that all multiples of 3 display as "FIZZ", all multiples of 5 as "BUZZ", and all multiples of 3 and 5 as "FIZZBUZZ". All the way through your program, for absolutely every Fixnum.
|
15
|
+
|
16
|
+
|
17
|
+
Here's how to use it:
|
18
|
+
|
19
|
+
gem 'fizzbuzzard' # in your Gemfile
|
20
|
+
|
21
|
+
require 'fizzbuzzard'
|
22
|
+
|
23
|
+
puts 3 # => "FIZZ"
|
24
|
+
puts 5 # => "BUZZ"
|
25
|
+
puts 15 # => "FIZZBUZZ"
|
26
|
+
|
27
|
+
(1..100).each { |n| puts n } # solves FizzBuzz!!
|
28
|
+
|
29
|
+
Donations are what keeps this project alive. Please send money. Please.
|
30
|
+
|
31
|
+
Oh, and in case you're interested, here's the current test status:
|
32
|
+
|
33
|
+
(0h8m|master) [fizzbuzzard] $ rspec
|
34
|
+
.
|
35
|
+
|
36
|
+
Finished in 0 seconds
|
37
|
+
1 example, FIZZBUZZ failures
|
38
|
+
|
39
|
+
Please let me know your success stories using FizzBuzzard in your enterprise projects. I'm @rsslldnphy on that Twitter.
|
data/fizzbuzzard.gemspec
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'fizzbuzzard'
|
3
|
+
s.version = '0.0.1'
|
4
|
+
s.date = '2013-04-24'
|
5
|
+
s.summary = "Get the goodness of FizzBuzz in all of your apps!"
|
6
|
+
s.description = "Makes all multiples of 3 print as FIZZ, etc etc"
|
7
|
+
s.authors = ["Russell Dunphy"]
|
8
|
+
s.email = ['russell@russelldunphy.com']
|
9
|
+
s.files = `git ls-files`.split($\)
|
10
|
+
s.test_files = s.files.grep(%r{^(test|spec|features)/})
|
11
|
+
s.require_paths = ["lib"]
|
12
|
+
s.homepage = 'http://github.com/rsslldnphy/fizzbuzzard'
|
13
|
+
|
14
|
+
s.add_development_dependency "rspec"
|
15
|
+
|
16
|
+
end
|
data/lib/fizzbuzzard.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
class Fixnum
|
2
|
+
alias_method :to_n, :to_s
|
3
|
+
|
4
|
+
def to_s
|
5
|
+
if self % 3 == 0 and self % 5 == 0 then "FIZZBUZZ"
|
6
|
+
elsif self % 3 == 0 then "FIZZ"
|
7
|
+
elsif self % 5 == 0 then "BUZZ"
|
8
|
+
else to_n end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class Float
|
13
|
+
alias_method :to_n, :to_s
|
14
|
+
|
15
|
+
def to_s
|
16
|
+
if self % 3 == 0 and self % 5 == 0 then "FIZZBUZZ"
|
17
|
+
elsif self % 3 == 0 then "FIZZ"
|
18
|
+
elsif self % 5 == 0 then "BUZZ"
|
19
|
+
else to_n end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,7 @@
|
|
1
|
+
require_relative '../lib/fizzbuzzard'
|
2
|
+
|
3
|
+
describe "FizzBuzz" do
|
4
|
+
it "generates the fizz buzz numbers correctly" do
|
5
|
+
(1..100).map(&:to_s).join(", ").should eq "1, 2, FIZZ, 4, BUZZ, FIZZ, 7, 8, FIZZ, BUZZ, 11, FIZZ, 13, 14, FIZZBUZZ, 16, 17, FIZZ, 19, BUZZ, FIZZ, 22, 23, FIZZ, BUZZ, 26, FIZZ, 28, 29, FIZZBUZZ, 31, 32, FIZZ, 34, BUZZ, FIZZ, 37, 38, FIZZ, BUZZ, 41, FIZZ, 43, 44, FIZZBUZZ, 46, 47, FIZZ, 49, BUZZ, FIZZ, 52, 53, FIZZ, BUZZ, 56, FIZZ, 58, 59, FIZZBUZZ, 61, 62, FIZZ, 64, BUZZ, FIZZ, 67, 68, FIZZ, BUZZ, 71, FIZZ, 73, 74, FIZZBUZZ, 76, 77, FIZZ, 79, BUZZ, FIZZ, 82, 83, FIZZ, BUZZ, 86, FIZZ, 88, 89, FIZZBUZZ, 91, 92, FIZZ, 94, BUZZ, FIZZ, 97, 98, FIZZ, BUZZ"
|
6
|
+
end
|
7
|
+
end
|
metadata
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fizzbuzzard
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Russell Dunphy
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-04-24 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
description: Makes all multiples of 3 print as FIZZ, etc etc
|
31
|
+
email:
|
32
|
+
- russell@russelldunphy.com
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- .gitignore
|
38
|
+
- Gemfile
|
39
|
+
- Gemfile.lock
|
40
|
+
- README.md
|
41
|
+
- fizzbuzzard.gemspec
|
42
|
+
- lib/fizzbuzzard.rb
|
43
|
+
- spec/fizzbuzzard_spec.rb
|
44
|
+
homepage: http://github.com/rsslldnphy/fizzbuzzard
|
45
|
+
licenses: []
|
46
|
+
post_install_message:
|
47
|
+
rdoc_options: []
|
48
|
+
require_paths:
|
49
|
+
- lib
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ! '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
requirements: []
|
63
|
+
rubyforge_project:
|
64
|
+
rubygems_version: 1.8.25
|
65
|
+
signing_key:
|
66
|
+
specification_version: 3
|
67
|
+
summary: Get the goodness of FizzBuzz in all of your apps!
|
68
|
+
test_files:
|
69
|
+
- spec/fizzbuzzard_spec.rb
|
70
|
+
has_rdoc:
|