Class: FizzBuzz

Inherits:
Object
  • Object
show all
Defined in:
lib/fizzbuzz.rb,
lib/fizzbuzz/version.rb

Overview

Yet another FizzBuzz in Ruby.

Provides simple and fast solution to a popular FizzBuzz problem for Ruby.

Constant Summary collapse

VERSION =

Current version of FizzBuzz.

'0.8.0'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.fizzbuzz(start, stop, reverse = false, &block) ⇒ Object

call-seq:

FizzBuzz.fizzbuzz( start, stop, reverse ) {|value| block } -> self
FizzBuzz.fizzbuzz( start, stop, reverse )                  -> array

Returns either an array or accepts a block if such is given. When a block is given then it will call the block once for each subsequent value for a given range from start to stop, passing the value as a parameter to the block.

Additionally, if the value of reverse is set to be true then the results will be given in an reverse order whether in a resulting array or when passing values to a block given.

Example:

FizzBuzz.fizzbuzz(1, 15)          #=> [1, 2, "Fizz", 4, "Buzz", "Fizz", 7, 8, "Fizz", "Buzz", 11, "Fizz", 13, 14, "FizzBuzz"]
FizzBuzz.fizzbuzz(1, 15, true)    #=> ["FizzBuzz", 14, 13, "Fizz", 11, "Buzz", "Fizz", 8, 7, "Fizz", "Buzz", 4, "Fizz", 2, 1]

Example:

FizzBuzz.fizzbuzz(1, 15) {|value| puts "Got #{value}" }

Produces:

Got 1
Got 2
Got Fizz
Got 4
Got Buzz
Got Fizz
Got 7
Got 8
Got Fizz
Got Buzz
Got 11
Got Fizz
Got 13
Got 14
Got FizzBuzz

See also: FizzBuzz::[], FizzBuzz::new, FizzBuzz#to_a, FizzBuzz#each and FizzBuzz#reverse_each


64
65
66
67
68
69
70
71
72
73
# File 'lib/fizzbuzz.rb', line 64

def self.fizzbuzz(start, stop, reverse = false, &block)
  fb = new(start, stop)

  if block_given?
    fb.send(reverse ? :reverse_each : :each, &block)
    self
  else
    fb.to_a.send(reverse ? :reverse : :to_a)
  end
end

.json_create(object) ⇒ Object

call-seq:

FizzBuzz.json_create( object ) -> FizzBuzz

Creates a new FizzBuzz object with both the start and stop values set accordingly given a JSON string that is an representation of the FizzBuzz object.

Example:

json = <<-EOS
  {
    "json_class": "FizzBuzz",
    "fizzbuzz": {
      "start": 1,
      "stop": 15
    }
  }
EOS

fb = JSON.load(json)    #=> #<FizzBuzz:0x007fc082 @start=1, @stop=15>
fb.to_hash

Produces:

{
  "fizzbuzz" => {
    "start" => 1,
     "stop" => 15
  }
}

See also: FizzBuzz#to_json, FizzBuzz::[], FizzBuzz::new and FizzBuzz::fizzbuzz


286
287
288
# File 'lib/fizzbuzz.rb', line 286

def self.json_create(object)
  new(*object['fizzbuzz'].values_at('start', 'stop'))
end

.step(start = 1, stop = nil, step = 1, &block) ⇒ Object

call-seq:

FizzBuzz.step( start, stop, step ) {|value| block } -> an integer
FizzBuzz.step( start, stop, step )                  -> an Enumerator

Calls the block once for each subsequent value for a given range from start to stop, passing the value as a parameter to the block in the increments of step.

If no block is given, an Enumerator is returned instead.

Example:

s = FizzBuzz.step    #=> #<Enumerator: #<Enumerator::Generator:0x559db8e7>:each>
s.take(15).each {|value| puts "Got #{value}" }

Produces:

Got 1
Got 2
Got Fizz
Got 4
Got Buzz
Got Fizz
Got 7
Got 8
Got Fizz
Got Buzz
Got 11
Got Fizz
Got 13
Got 14
Got FizzBuzz

See also: FizzBuzz::fizzbuzz, FizzBuzz::[] and FizzBuzz#step


111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/fizzbuzz.rb', line 111

def self.step(start = 1, stop = nil, step = 1, &block)
  if block_given?
    start.step(stop, step) do |n|
      yield FB[n]
    end
  else
    Enumerator.new do |y|
      start.step(stop, step).each do |n|
        y << FB[n]
      end
    end
  end
end

Instance Method Details

#as_jsonObject

call-seq:

fizzbuzz.as_json( arguments ) -> hash

Returns a hash representing the FizzBuzz object that will be used when generating a JSON string representation.

Example:

fb = FizzBuzz.new(1, 15)    #=> #<FizzBuzz:0x007f90c1 @start=1, @stop=15>
fb.as_json

Produces:

{
  "json_class" => "FizzBuzz",
    "fizzbuzz" => {
    "start" => 1,
     "stop" => 15
  }
}

See also: FizzBuzz#to_json and FizzBuzz#to_hash


221
222
223
# File 'lib/fizzbuzz.rb', line 221

def as_json(*)
  { JSON.create_id => self.class.name }.merge(to_hash)
end

#step(start = @start, stop = @stop, step = 1, &block) ⇒ Object

call-seq:

fizzbuzz.step( start, stop, step ) {|value| block } -> an integer
fizzbuzz.step( start, stop, step )                  -> an Enumerator

Calls the block once for each subsequent value for a given range from start to stop, passing the value as a parameter to the block in the increments of step.

If no block is given, an Enumerator is returned instead.

Example:

fb = FizzBuzz.new(1, 15)    #=> #<FizzBuzz:0x5653a2d1 @start=1, @stop=15>
s = fb.step                 #=> #<Enumerator: #<Enumerator::Generator:0x559db8e7>:each>
s.take(15).each {|value| puts "Got #{value}" }

Produces:

Got 1
Got 2
Got Fizz
Got 4
Got Buzz
Got Fizz
Got 7
Got 8
Got Fizz
Got Buzz
Got 11
Got Fizz
Got 13
Got 14
Got FizzBuzz

See also: FizzBuzz::step, FizzBuzz::fizzbuzz and FizzBuzz::[]


162
163
164
# File 'lib/fizzbuzz.rb', line 162

def step(start = @start, stop = @stop, step = 1, &block)
  self.class.step(start, stop, step, &block)
end

#to_hashObject Also known as: to_h

call-seq:

fizzbuzz.to_hash -> hash

Returns a hash representing the FizzBuzz object.

Example:

fb = FizzBuzz.new(1, 15)    #=> #<FizzBuzz:0x007fbe84 @start=1, @stop=15>
fb.to_hash

Produces:

{
  "fizzbuzz" => {
    "start" => 1,
     "stop" => 15
  }
}

See also: FizzBuzz#as_json and FizzBuzz#to_json


188
189
190
191
192
193
194
195
# File 'lib/fizzbuzz.rb', line 188

def to_hash
  {
    'fizzbuzz' => {
      'start' => @start,
      'stop' => @stop
    }
  }
end

#to_json(*arguments) ⇒ Object

call-seq:

fizzbuzz.to_json( arguments ) -> string

Returns a JSON string representing the FizzBuzz object.

Example:

fb = FizzBuzz.new(1, 15)    #=> #<FizzBuzz:0x007fce83 @start=1, @stop=15>
fb.to_json

Produces:

{
  "json_class": "FizzBuzz",
  "fizzbuzz": {
    "start": 1,
    "stop": 15
  }
}

See also: FizzBuzz::json_create, FizzBuzz#as_json and FizzBuzz#to_hash


248
249
250
# File 'lib/fizzbuzz.rb', line 248

def to_json(*arguments)
  as_json.to_json(*arguments)
end