Created
January 31, 2013 20:40
-
-
Save aviflombaum/4686236 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
| # Here is how I would implement Fizzbuzz with objects | |
| # The main issue with your class is what's called | |
| # single responsibility principle | |
| # classes should do one thing and be about | |
| # instances of those classes | |
| # the fizzbuzz class should be about the loop | |
| # just solving fizzbuzz individually | |
| class Fizzbuzz | |
| attr_accessor :value | |
| def initialize(value) | |
| @value = value | |
| end | |
| def fizz? | |
| value % 3 == 0 | |
| end | |
| def buzz? | |
| value % 5 == 0 | |
| end | |
| def fizzbuzz? | |
| fizz? && buzz? | |
| end | |
| def answer | |
| if fizzbuzz? | |
| "fizzbuzz" | |
| elsif buzz? | |
| "buzz" | |
| elsif fizz? | |
| "fizz" | |
| end | |
| end | |
| end | |
| (1..50).each do |i| | |
| puts "#{i} is #{Fizzbuzz.new(i).answer}" | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment