Class: FizzBuzz
- Inherits:
-
Object
- Object
- FizzBuzz
- Includes:
- Enumerable
- Defined in:
- lib/fizzbuzz.rb,
lib/fizzbuzz/version.rb,
ext/fizzbuzz/fizzbuzz.c
Overview
:startdoc:
Defined Under Namespace
Classes: Error, RangeError, TypeError
Constant Summary collapse
- VERSION =
Current version of FizzBuzz.
'0.0.3'
Class Method Summary collapse
-
.[](integer) ⇒ Integer, String
Returns Fizz if the given value is divisible by three, Buzz if the given value is divisible by five and FizzBuzz if the given value is divisible by both three and five, or the given integer value otherwise.
-
.fizzbuzz(start, stop, reverse = false) ⇒ Object
call-seq: FizzBuzz.fizzbuzz( start, stop, reverse ) -> array FizzBuzz.fizzbuzz( start, stop, reverse ) {|value| block } -> self.
-
.is_buzz?(integer) ⇒ Boolean
Returns
trueif a given integer value is divisible by five (given value is a Buzz), orfalseotherwise. -
.is_fizz?(integer) ⇒ Boolean
Returns
trueif a given integer value is divisible by three (given value is a Fizz), orfalseotherwise. -
.is_fizzbuzz?(integer) ⇒ Boolean
Returns
trueif a given integer value is divisible by both three and five (given value is a FizzBuzz), orfalseotherwise.
Instance Method Summary collapse
-
#each ⇒ Object
Calls the block once for each subsequent value for a given range from
starttostop, passing the value as a parameter to the block. -
#new(start, stop) ⇒ self
constructor
Returns a new FizzBuzz.
-
#reverse_each ⇒ Object
Calls the block once for each subsequent value for a given range from
starttostopin an reverse order, passing the value as a parameter to the block. -
#start ⇒ Integer
Returns the current value for
start. -
#start=(integer) ⇒ Integer
Sets the current value of
startif given new value is lower or equal to the current value ofstop, or raises an FizzBuzz::RangeError exception otherwise. -
#stop ⇒ Integer
Returns the current value for
stop. -
#stop=(integer) ⇒ Integer
Sets the current value of
stopif given new value is greater or equal to the current value ofstart, or raises an FizzBuzz::RangeError exception otherwise. -
#to_a ⇒ Array
Returns an array containing results upon calculating an appropriate values for a given range from
starttostop.
Constructor Details
#new(start, stop) ⇒ self
Returns a new FizzBuzz.
The given start and stop values must be of an integer type and will establish a range within which calculation of any relevant FizzBuzz results will have place.
Example:
fb = FizzBuzz.new(1, 100) #=> #<FizzBuzz:0xb6fd3b38 @stop=100, @start=1>
fb = FizzBuzz.new(-100,-1) #=> #<FizzBuzz:0xb72d5700 @stop=-1, @start=-100>
fb = FizzBuzz.new(-15, 15) #=> #<FizzBuzz:0xb6fd0460 @stop=15, @start=-15>
The given value of stop must always be greater than or equal to the given value of start, otherwise raises an FizzBuzz::RangeError exception.
Will raise a FizzBuzz::TypeError exception if given value of either start or stop is not an Integer or Bignum type.
See also: FizzBuzz::fizzbuzz
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'ext/fizzbuzz/fizzbuzz.c', line 71 VALUE rb_fb_initialize(int argc, VALUE *argv, VALUE object) { VALUE start, stop; rb_scan_args(argc, argv, "20", &start, &stop); CHECK_TYPE(start, error(E_INVALID_START_TYPE)); CHECK_TYPE(stop, error(E_INVALID_STOP_TYPE)); CHECK_RANGE(start, stop, error(E_BAD_VALUE_START)); rb_ivar_set(object, id_at_start, start); rb_ivar_set(object, id_at_stop, stop); return object; } |
Class Method Details
.[](integer) ⇒ Integer, String
Returns Fizz if the given value is divisible by three, Buzz if the given value is divisible by five and FizzBuzz if the given value is divisible by both three and five, or the given integer value otherwise.
Example:
FizzBuzz[1] #=> 1
FizzBuzz[3] #=> "Fizz"
FizzBuzz[5] #=> "Buzz"
FizzBuzz[15] #=> "FizzBuzz"
Will raise a FizzBuzz::TypeError exception if given value is not an Integer or Bignum type.
See also: FizzBuzz::is_fizz?, FizzBuzz::is_buzz? and FizzBuzz::is_fizzbuzz?
386 387 388 389 390 391 392 393 |
# File 'ext/fizzbuzz/fizzbuzz.c', line 386 VALUE rb_fb_square(VALUE object, VALUE value) { UNUSED(object); CHECK_TYPE(value, error(E_INVALID_TYPE)); return fizzbuzz_evaluate(value); } |
.fizzbuzz(start, stop, reverse = false) ⇒ Object
call-seq:
FizzBuzz.fizzbuzz( start, stop, reverse ) -> array
FizzBuzz.fizzbuzz( start, stop, reverse ) {|value| block } -> self
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
78 79 80 81 82 83 84 85 86 |
# File 'lib/fizzbuzz.rb', line 78 def self.fizzbuzz(start, stop, reverse = false) fb = FizzBuzz.new(start, stop) if block_given? fb.send(reverse ? :reverse_each : :each) {|i| yield i } else reverse ? fb.to_a.reverse : fb.to_a end end |
.is_buzz?(integer) ⇒ Boolean
Returns true if a given integer value is divisible by five (given value is a Buzz), or false otherwise.
Example:
FizzBuzz.is_buzz?(3) #=> false
FizzBuzz.is_buzz?(5) #=> true
FizzBuzz.is_buzz?(15) #=> false
Will raise a FizzBuzz::TypeError exception if given value is not an Integer or Bignum type.
See also: FizzBuzz::[]
329 330 331 332 333 334 335 336 |
# File 'ext/fizzbuzz/fizzbuzz.c', line 329 VALUE rb_fb_is_buzz(VALUE object, VALUE value) { UNUSED(object); CHECK_TYPE(value, error(E_INVALID_TYPE)); return CBOOL2RVAL(IS_BUZZ(value)); } |
.is_fizz?(integer) ⇒ Boolean
Returns true if a given integer value is divisible by three (given value is a Fizz), or false otherwise.
Example:
FizzBuzz.is_fizz?(3) #=> true
FizzBuzz.is_fizz?(5) #=> false
FizzBuzz.is_fizz?(15) #=> false
Will raise a FizzBuzz::TypeError exception if given value is not an Integer or Bignum type.
See also: FizzBuzz::[]
302 303 304 305 306 307 308 309 |
# File 'ext/fizzbuzz/fizzbuzz.c', line 302 VALUE rb_fb_is_fizz(VALUE object, VALUE value) { UNUSED(object); CHECK_TYPE(value, error(E_INVALID_TYPE)); return CBOOL2RVAL(IS_FIZZ(value)); } |
.is_fizzbuzz?(integer) ⇒ Boolean
Returns true if a given integer value is divisible by both three and five (given value is a FizzBuzz), or false otherwise.
Example:
FizzBuzz.is_fizzbuzz?(3) #=> false
FizzBuzz.is_fizzbuzz?(5) #=> false
FizzBuzz.is_fizzbuzz?(15) #=> true
Will raise a FizzBuzz::TypeError exception if given value is not an Integer or Bignum type.
See also: FizzBuzz::[]
356 357 358 359 360 361 362 363 |
# File 'ext/fizzbuzz/fizzbuzz.c', line 356 VALUE rb_fb_is_fizzbuzz(VALUE object, VALUE value) { UNUSED(object); CHECK_TYPE(value, error(E_INVALID_TYPE)); return CBOOL2RVAL(IS_FIZZBUZZ(value)); } |
Instance Method Details
#each {|value| ... } ⇒ self #each ⇒ 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.
If no block is given, an Enumerator is returned instead.
Example:
fb = FizzBuzz.new(1, 15) #=> #<FizzBuzz:0xf722f8ec @stop=15, @start=1>
fb.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#reverse_each
236 237 238 239 240 |
# File 'ext/fizzbuzz/fizzbuzz.c', line 236 VALUE rb_fb_enumerator(VALUE object) { return fizzbuzz_values(object, R_TYPE_ENUMERATOR, D_LOOP_FORWARD); } |
#reverse_each {|value| ... } ⇒ self #reverse_each ⇒ an Enumerator
Calls the block once for each subsequent value for a given range from start to stop in an reverse order, passing the value as a parameter to the block.
Example:
If no block is given, an Enumerator is returned instead.
fb = FizzBuzz.new(1, 15) #=> #<FizzBuzz:0xb7308664 @stop=15, @start=1>
fb.reverse_each {|value| puts "Got #{value}" }
Produces:
Got FizzBuzz
Got 14
Got 13
Got Fizz
Got 11
Got Buzz
Got Fizz
Got 8
Got 7
Got Fizz
Got Buzz
Got 4
Got Fizz
Got 2
Got 1
See also: FizzBuzz#each
278 279 280 281 282 |
# File 'ext/fizzbuzz/fizzbuzz.c', line 278 VALUE rb_fb_reverse_enumerator(VALUE object) { return fizzbuzz_values(object, R_TYPE_ENUMERATOR, D_LOOP_REVERSE); } |
#start ⇒ Integer
Returns the current value for start.
Example:
fb = FizzBuzz.new(1, 100) #=> #<FizzBuzz:0xf726b48c @stop=100, @start=1>
fb.start #=> 1
100 101 102 103 104 |
# File 'ext/fizzbuzz/fizzbuzz.c', line 100 VALUE rb_fb_get_start(VALUE object) { return rb_ivar_get(object, id_at_start); } |
#start=(integer) ⇒ Integer
Sets the current value of start if given new value is lower or equal to the current value of stop, or raises an FizzBuzz::RangeError exception otherwise.
Examples:
fb = FizzBuzz.new(1, 100) #=> #<FizzBuzz:0xf726f03c @stop=100, @start=1>
fb.start #=> 1
fb.start = 15 #=> 15
fb.start #=> 15
Will raise a FizzBuzz::TypeError exception if given value is not an Integer or Bignum type.
124 125 126 127 128 129 130 131 132 133 |
# File 'ext/fizzbuzz/fizzbuzz.c', line 124 VALUE rb_fb_set_start(VALUE object, VALUE value) { VALUE stop = rb_ivar_get(object, id_at_stop); CHECK_TYPE(value, error(E_INVALID_START_TYPE)); CHECK_RANGE(value, stop, error(E_BAD_VALUE_START)); return rb_ivar_set(object, id_at_start, value); } |
#stop ⇒ Integer
Returns the current value for stop.
Example:
fb = FizzBuzz.new(1, 100) #=> #<FizzBuzz:0xf7272bec @stop=100, @start=1>
fb.stop #=> 100
146 147 148 149 150 |
# File 'ext/fizzbuzz/fizzbuzz.c', line 146 VALUE rb_fb_get_stop(VALUE object) { return rb_ivar_get(object, id_at_stop); } |
#stop=(integer) ⇒ Integer
Sets the current value of stop if given new value is greater or equal to the current value of start, or raises an FizzBuzz::RangeError exception otherwise.
Example:
fb = FizzBuzz.new(1, 100) #=> #<FizzBuzz:0xf727679c @stop=100, @start=1>
fb.stop #=> 100
fb.stop = 15 #=> 15
fb.stop #=> 15
Will raise a FizzBuzz::TypeError exception if given value is not an Integer or Bignum type.
170 171 172 173 174 175 176 177 178 179 |
# File 'ext/fizzbuzz/fizzbuzz.c', line 170 VALUE rb_fb_set_stop(VALUE object, VALUE value) { VALUE start = rb_ivar_get(object, id_at_start); CHECK_TYPE(value, error(E_INVALID_STOP_TYPE)); CHECK_RANGE(start, value, error(E_BAD_VALUE_STOP)); return rb_ivar_set(object, id_at_stop, value); } |
#to_a ⇒ Array
Returns an array containing results upon calculating an appropriate values for a given range from start to stop.
Example:
fb = FizzBuzz.new(1, 15) #=> #<FizzBuzz:0xf727fd60 @stop=15, @start=1>
fb.to_a #=> [1, 2, "Fizz", 4, "Buzz", "Fizz", 7, 8, "Fizz", "Buzz", 11, "Fizz", 13, 14, "FizzBuzz"]
See also: FizzBuzz::fizzbuzz
195 196 197 198 199 |
# File 'ext/fizzbuzz/fizzbuzz.c', line 195 VALUE rb_fb_array(VALUE object) { return fizzbuzz_values(object, R_TYPE_ARRAY, D_LOOP_FORWARD); } |