Class: FizzBuzz

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/fizzbuzz.rb,
lib/fizzbuzz/version.rb,
ext/fizzbuzz/fizzbuzz.c

Overview

:startdoc:

Constant Summary collapse

VERSION =

Current version of FizzBuzz.

"0.0.2"

Class Method Summary collapse

Instance Method Summary collapse

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 ArgumentError exception.

Will raise a TypeError exception if given value of either start or stop is not an integer type.

See also:

FizzBuzz::fizzbuzz


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'ext/fizzbuzz/fizzbuzz.c', line 60

VALUE
rb_fb_initialize(int argc, VALUE *argv, VALUE object)
{
    VALUE start, stop;

    rb_scan_args(argc, argv, "20", &start, &stop);

    CHECK_TYPE(start, errors[E_INVALID_START_TYPE]);
    CHECK_TYPE(stop, errors[E_INVALID_STOP_TYPE]);

    CHECK_BOUNDARY(start, stop, errors[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 TypeError exception if given value is not an integer type.

See also: FizzBuzz::is_fizz?, FizzBuzz::is_buzz? and FizzBuzz::is_fizzbuzz?

Returns:



369
370
371
372
373
374
375
376
# File 'ext/fizzbuzz/fizzbuzz.c', line 369

VALUE
rb_fb_square(VALUE object, VALUE value)
{
    UNUSED(object);
    CHECK_TYPE(value, errors[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 TypeError exception if given value is not an integer type.

See also: FizzBuzz::[]

Returns:

  • (Boolean)


314
315
316
317
318
319
320
321
# File 'ext/fizzbuzz/fizzbuzz.c', line 314

VALUE
rb_fb_is_buzz(VALUE object, VALUE value)
{
    UNUSED(object);
    CHECK_TYPE(value, errors[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 TypeError exception if given value is not an integer type.

See also: FizzBuzz::[]

Returns:

  • (Boolean)


288
289
290
291
292
293
294
295
# File 'ext/fizzbuzz/fizzbuzz.c', line 288

VALUE
rb_fb_is_fizz(VALUE object, VALUE value)
{
    UNUSED(object);
    CHECK_TYPE(value, errors[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 TypeError exception if given value is not an integer type.

See also: FizzBuzz::[]

Returns:

  • (Boolean)


340
341
342
343
344
345
346
347
# File 'ext/fizzbuzz/fizzbuzz.c', line 340

VALUE
rb_fb_is_fizzbuzz(VALUE object, VALUE value)
{
    UNUSED(object);
    CHECK_TYPE(value, errors[E_INVALID_TYPE]);

    return CBOOL2RVAL(IS_FIZZBUZZ(value));
}

Instance Method Details

#each {|value| ... } ⇒ self #eachan 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

Overloads:

  • #each {|value| ... } ⇒ self

    Yields:

    • (value)

    Returns:

    • (self)
  • #eachan Enumerator

    Returns:

    • (an Enumerator)


223
224
225
226
227
# File 'ext/fizzbuzz/fizzbuzz.c', line 223

VALUE
rb_fb_enumerator(VALUE object)
{
    return fizzbuzz_values(object, R_TYPE_ENUMERATOR, D_LOOP_FORWARD);
}

#reverse_each {|value| ... } ⇒ self #reverse_eachan 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

Overloads:

  • #reverse_each {|value| ... } ⇒ self

    Yields:

    • (value)

    Returns:

    • (self)
  • #reverse_eachan Enumerator

    Returns:

    • (an Enumerator)


265
266
267
268
269
# File 'ext/fizzbuzz/fizzbuzz.c', line 265

VALUE
rb_fb_reverse_enumerator(VALUE object)
{
    return fizzbuzz_values(object, R_TYPE_ENUMERATOR, D_LOOP_REVERSE);
}

#startInteger

Returns the current value for start.

Example:

fb = FizzBuzz.new(1, 100) #=> #<FizzBuzz:0xf726b48c @stop=100, @start=1>
fb.start                  #=> 1

Returns:



89
90
91
92
93
# File 'ext/fizzbuzz/fizzbuzz.c', line 89

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 ArgumentError 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 TypeError exception if given value is not an integer type.

Returns:



112
113
114
115
116
117
118
119
120
121
# File 'ext/fizzbuzz/fizzbuzz.c', line 112

VALUE
rb_fb_set_start(VALUE object, VALUE value)
{
    VALUE stop = rb_ivar_get(object, id_at_stop);

    CHECK_TYPE(value, errors[E_INVALID_START_TYPE]);
    CHECK_BOUNDARY(value, stop, errors[E_BAD_VALUE_START]);

    return rb_ivar_set(object, id_at_start, value);
}

#stopInteger

Returns the current value for stop.

Example:

fb = FizzBuzz.new(1, 100) #=> #<FizzBuzz:0xf7272bec @stop=100, @start=1>
fb.stop                   #=> 100

Returns:



134
135
136
137
138
# File 'ext/fizzbuzz/fizzbuzz.c', line 134

VALUE
rb_fb_get_stop(VALUE object)
{
    return rb_ivar_get(object, id_at_stop);
}

#start=(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 ArgumentError 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 TypeError exception if given value is not an integer type.

Returns:



157
158
159
160
161
162
163
164
165
166
# File 'ext/fizzbuzz/fizzbuzz.c', line 157

VALUE
rb_fb_set_stop(VALUE object, VALUE value)
{
    VALUE start = rb_ivar_get(object, id_at_start);

    CHECK_TYPE(value, errors[E_INVALID_STOP_TYPE]);
    CHECK_BOUNDARY(start, value, errors[E_BAD_VALUE_STOP]);

    return rb_ivar_set(object, id_at_stop, value);
}

#to_aArray

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

Returns:

  • (Array)


182
183
184
185
186
# File 'ext/fizzbuzz/fizzbuzz.c', line 182

VALUE
rb_fb_array(VALUE object)
{
    return fizzbuzz_values(object, R_TYPE_ARRAY, D_LOOP_FORWARD);
}