Sha256: f25cfd3414015d535d9761f9fd691a9df94dc2c17f1dc7b0a0d5845fbe35964a

Contents?: true

Size: 950 Bytes

Versions: 1

Compression:

Stored size: 950 Bytes

Contents

class FizzBuzz

  # Returns an array solving the classic 1 to 100 FizzBuzz problem.
  def crunch(start=1, stop=100)
    return nil unless start < stop

    return_values = Array.new

    (start..stop).each do |n|
      if n.fizzbuzz?
         return_values << "FizzBuzz"
      elsif n.buzz?
        return_values << "Buzz"
      elsif n.fizz?
        return_values << "Fizz"
      else
        return_values << n
      end
    end

    return return_values
  end
end


module FizzBuzziness
  # For testing the Fizz-, Buzz-, or Fizzbuzz-ness of a Fixnum
  #   3.fizz?       #  => true
  #   3.buzz?       #  => false
  #   15.fizzbuzz?  #  => true
  #
  # Note that using these methods, fizzbuzzy numbers like 15 will fizz, buzz, AND fizzbuzz.

  def fizz?
    self % 3 == 0
  end

  def buzz?
    self % 5 == 0
  end

  def fizzbuzz?
    self % 15 == 0
  end
end


class Fixnum
  include FizzBuzziness
end


class Float
  include FizzBuzziness
end


Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fizz-buzz-0.4.0 lib/fizz-buzz.rb