Class: FizzBuzz

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

Overview

:startdoc:

Constant Summary collapse

VERSION =

Current version of FizzBuzz.

'0.7.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


84
85
86
87
88
89
90
91
92
93
# File 'lib/fizzbuzz.rb', line 84

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


215
216
217
# File 'lib/fizzbuzz.rb', line 215

def self.json_create(object)
  new(*object['fizzbuzz'].values_at('start', 'stop'))
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


150
151
152
# File 'lib/fizzbuzz.rb', line 150

def as_json(*)
  { JSON.create_id => self.class.name }.merge(to_hash)
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


117
118
119
120
121
122
123
124
# File 'lib/fizzbuzz.rb', line 117

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


177
178
179
# File 'lib/fizzbuzz.rb', line 177

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