Last active
August 29, 2015 14:19
-
-
Save cardoni/58daa93eb1f293a65c6d to your computer and use it in GitHub Desktop.
quick ruby fizzbuzz
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
| # encoding: UTF-8 | |
| class FizzBuzz | |
| def initialize( times=100 ) | |
| @times = times.to_i | |
| end | |
| def count | |
| ( 1..@times ).count | |
| end | |
| def start | |
| ( 1..@times ).each do | number | | |
| fizzbuzz( number ) | |
| end | |
| end | |
| def fizzbuzz( number ) | |
| number = number.to_i | |
| result = '' | |
| if number % 3 == 0 | |
| result += 'Fizz' | |
| end | |
| if number % 5 == 0 | |
| result += 'Buzz' | |
| end | |
| if result.empty? | |
| result = number | |
| end | |
| puts "#{number}. #{result}" | |
| end | |
| end | |
| fb = FizzBuzz.new() | |
| # fb = FizzBuzz.new( 500 ) # or, pass more than 100 in.... | |
| puts "Count of items to print: #{fb.count}" | |
| fb.start |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment