ruby-fizzbuzz 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data.tar.gz.sig CHANGED
Binary file
data/CHANGES CHANGED
@@ -1,3 +1,19 @@
1
+ fizzbuzz (0.0.3)
2
+
3
+ * Added custom exceptions and improved error handling;
4
+ * Added more variety of Rubies on which tests are being run with Travis CI;
5
+ * Added project to Rubygems (http://rubygems.org) so it can be installed
6
+ with just "gem install ruby-fizzbuzz" command;
7
+ * Renamed project from "fizzbuzz" to "ruby-fizzbuzz" to avoid clashes with
8
+ other Ruby gems hosted on Rubygems web site;
9
+ * Made "mkmf" in extconf.rb more user-friendly (it now reports missing
10
+ build-time dependencies better);
11
+ * Split Ruby gem specification (or gemspec if you wish) away from Rakefile
12
+ into its own file (namely ruby-fizzbuzz.gemspec);
13
+ * Improved documentation and removed bunch of small code smells.
14
+
15
+ -- Krzysztof Wilczynski <krzysztof.wilczynski@linux.com> Sat, 31 Aug 2013 17:54:54 +0000
16
+
1
17
  fizzbuzz (0.0.2)
2
18
 
3
19
  * Added is_fizz?, is_buzz? and is_fizzbuzz? singleton methods to FizzBuzz class;
data/CHANGES.rdoc CHANGED
@@ -1,3 +1,19 @@
1
+ fizzbuzz (0.0.3)
2
+
3
+ * Added custom exceptions and improved error handling;
4
+ * Added more variety of Rubies on which tests are being run with Travis CI;
5
+ * Added project to Rubygems (http://rubygems.org) so it can be installed
6
+ with just "gem install ruby-fizzbuzz" command;
7
+ * Renamed project from "fizzbuzz" to "ruby-fizzbuzz" to avoid clashes with
8
+ other Ruby gems hosted on Rubygems web site;
9
+ * Made "mkmf" in extconf.rb more user-friendly (it now reports missing
10
+ build-time dependencies better);
11
+ * Split Ruby gem specification (or gemspec if you wish) away from Rakefile
12
+ into its own file (namely ruby-fizzbuzz.gemspec);
13
+ * Improved documentation and removed bunch of small code smells.
14
+
15
+ -- Krzysztof Wilczynski <krzysztof.wilczynski@linux.com> Sat, 31 Aug 2013 17:54:54 +0000
16
+
1
17
  fizzbuzz (0.0.2)
2
18
 
3
19
  * Added is_fizz?, is_buzz? and is_fizzbuzz? singleton methods to FizzBuzz class;
data/README.rdoc CHANGED
@@ -4,4 +4,5 @@ to a popular FizzBuzz problem for Ruby.
4
4
  Written in C as an example of using Ruby's C API - with the support
5
5
  for arbitrary large numeric values via the Bignum class.
6
6
 
7
- {<img src="https://travis-ci.org/kwilczynski/ruby-fizzbuzz.png?branch=master"/>}[https://travis-ci.org/kwilczynski/ruby-fizzbuzz]
7
+ {<img src="https://travis-ci.org/kwilczynski/ruby-fizzbuzz.png?branch=master" alt="Build Status"/>}[https://travis-ci.org/kwilczynski/ruby-fizzbuzz]
8
+ {<img src="https://badge.fury.io/rb/ruby-fizzbuzz.png" alt="Gem Version" />}[http://badge.fury.io/rb/ruby-fizzbuzz]
data/TODO CHANGED
@@ -4,19 +4,20 @@ fizzbuzz (1.0.0)
4
4
 
5
5
  -- Krzysztof Wilczynski <krzysztof.wilczynski@linux.com> Mon, 01 Oct 2012 17:57:18 +0100
6
6
 
7
- fizzbuzz (0.0.4)
7
+ fizzbuzz (0.0.5)
8
8
 
9
9
  * Improve github project page;
10
- * Re-factor unit tests to make them more idiotmatic (maybe move to Rspec and BDD?).
10
+ * Re-factor unit tests to make them more idiomatic (maybe move to Rspec and BDD?).
11
11
 
12
- -- Krzysztof Wilczynski <krzysztof.wilczynski@linux.com> Mon, 08 Oct 2012 00:14:08 +0100
12
+ -- Krzysztof Wilczynski <krzysztof.wilczynski@linux.com> Sun, 31 Aug 2013 17:50:06 +0000
13
13
 
14
- fizzbuzz (0.0.3)
14
+ fizzbuzz (0.0.4)
15
15
 
16
+ * Move to YARD from RDoc (as RDoc is very bad, really);
16
17
  * Add pure-Ruby implementation;
17
18
  * Add benchmarks;
18
19
  * Add some examples;
19
20
  * Add Doxygen style comments for other functions in the source code;
20
21
  * Improve documentation i.e. source code commenting, README file, etc.
21
22
 
22
- -- Krzysztof Wilczynski <krzysztof.wilczynski@linux.com> Sat, 13 Oct 2012 16:08:26 +0100
23
+ -- Krzysztof Wilczynski <krzysztof.wilczynski@linux.com> Sun, 31 Aug 2013 17:50:06 +0000
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.0.3
@@ -29,7 +29,31 @@ $LDFLAGS << " %s" % ENV['LDFLAGS'] if ENV['LDFLAGS']
29
29
  $CFLAGS << " %s" % ENV['CFLAGS'] if ENV['CFLAGS']
30
30
  $CFLAGS << ' -std=c99 -g -Wall -Wextra -pedantic'
31
31
 
32
- have_header('ruby.h') or missing('ruby.h')
32
+ unless have_header('ruby.h')
33
+ abort <<-EOS
34
+
35
+ You appear to be missing Ruby development libraries and/or header
36
+ files. You can install missing compile-time dependencies in one of
37
+ the following ways:
38
+
39
+ - Debian / Ubuntu
40
+
41
+ apt-get install ruby-dev
42
+
43
+ - Red Hat / CentOS / Fedora
44
+
45
+ yum install ruby-devel
46
+
47
+
48
+ Alternatively, you can use either of the following Ruby version
49
+ managers in order to install Ruby locally (for your user only)
50
+ and/or system-wide:
51
+
52
+ - Ruby Version Manager (for RVM, see http://rvm.io/)
53
+ - Ruby Environment (for rbenv, see http://github.com/sstephenson/rbenv)
54
+
55
+ EOS
56
+ end
33
57
 
34
58
  dir_config('fizzbuzz')
35
59
 
@@ -18,19 +18,31 @@
18
18
  * limitations under the License.
19
19
  */
20
20
 
21
- /* :startdoc: */
22
-
23
21
  #include <fizzbuzz.h>
24
22
 
25
23
  ID id_at_start, id_at_stop;
24
+
26
25
  VALUE rb_cFizzBuzz = Qnil;
27
26
 
27
+ VALUE rb_fb_eError = Qnil;
28
+ VALUE rb_fb_eTypeError = Qnil;
29
+ VALUE rb_fb_eRangeError = Qnil;
30
+
28
31
  void Init_fizzbuzz(void);
29
32
 
30
33
  static VALUE fizzbuzz_evaluate(VALUE value);
31
34
  static VALUE fizzbuzz_values(VALUE object, return_type_t type,
32
35
  direction_t direction);
33
36
 
37
+ static VALUE fizzbuzz_exception_wrapper(VALUE value);
38
+ static VALUE fizzbuzz_exception(void *data);
39
+
40
+ static VALUE fizzbuzz_type_error(VALUE klass, const char *message);
41
+ static VALUE fizzbuzz_range_error(VALUE klass, VALUE start, VALUE stop,
42
+ const char *message);
43
+
44
+ /* :startdoc: */
45
+
34
46
  /*
35
47
  * call-seq:
36
48
  * FizzBuzz.new( start, stop ) -> self
@@ -48,14 +60,13 @@ static VALUE fizzbuzz_values(VALUE object, return_type_t type,
48
60
  * fb = FizzBuzz.new(-15, 15) #=> #<FizzBuzz:0xb6fd0460 @stop=15, @start=-15>
49
61
  *
50
62
  * The given value of +stop+ must always be greater than or equal to the
51
- * given value of +start+, otherwise raises an +ArgumentError+ exception.
52
- *
53
- * Will raise a +TypeError+ exception if given value of either +start+
54
- * or +stop+ is not an integer type.
63
+ * given value of +start+, otherwise raises an <i>FizzBuzz::RangeError</i>
64
+ * exception.
55
65
  *
56
- * See also:
66
+ * Will raise a <i>FizzBuzz::TypeError</i> exception if given value of either
67
+ * +start+ or +stop+ is not an _Integer_ or _Bignum_ type.
57
68
  *
58
- * FizzBuzz::fizzbuzz
69
+ * See also: FizzBuzz::fizzbuzz
59
70
  */
60
71
  VALUE
61
72
  rb_fb_initialize(int argc, VALUE *argv, VALUE object)
@@ -64,10 +75,10 @@ rb_fb_initialize(int argc, VALUE *argv, VALUE object)
64
75
 
65
76
  rb_scan_args(argc, argv, "20", &start, &stop);
66
77
 
67
- CHECK_TYPE(start, errors[E_INVALID_START_TYPE]);
68
- CHECK_TYPE(stop, errors[E_INVALID_STOP_TYPE]);
78
+ CHECK_TYPE(start, error(E_INVALID_START_TYPE));
79
+ CHECK_TYPE(stop, error(E_INVALID_STOP_TYPE));
69
80
 
70
- CHECK_BOUNDARY(start, stop, errors[E_BAD_VALUE_START]);
81
+ CHECK_RANGE(start, stop, error(E_BAD_VALUE_START));
71
82
 
72
83
  rb_ivar_set(object, id_at_start, start);
73
84
  rb_ivar_set(object, id_at_stop, stop);
@@ -83,8 +94,8 @@ rb_fb_initialize(int argc, VALUE *argv, VALUE object)
83
94
  *
84
95
  * Example:
85
96
  *
86
- * fb = FizzBuzz.new(1, 100) #=> #<FizzBuzz:0xf726b48c @stop=100, @start=1>
87
- * fb.start #=> 1
97
+ * fb = FizzBuzz.new(1, 100) #=> #<FizzBuzz:0xf726b48c @stop=100, @start=1>
98
+ * fb.start #=> 1
88
99
  */
89
100
  VALUE
90
101
  rb_fb_get_start(VALUE object)
@@ -97,25 +108,26 @@ rb_fb_get_start(VALUE object)
97
108
  * fizzfuzz.start= (integer) -> integer
98
109
  *
99
110
  * Sets the current value of +start+ if given new value is lower or equal
100
- * to the current value of +stop+, or raises an +ArgumentError+ exception
101
- * otherwise.
111
+ * to the current value of +stop+, or raises an <i>FizzBuzz::RangeError</i>
112
+ * exception otherwise.
102
113
  *
103
114
  * Examples:
104
115
  *
105
- * fb = FizzBuzz.new(1, 100) #=> #<FizzBuzz:0xf726f03c @stop=100, @start=1>
106
- * fb.start #=> 1
107
- * fb.start = 15 #=> 15
108
- * fb.start #=> 15
116
+ * fb = FizzBuzz.new(1, 100) #=> #<FizzBuzz:0xf726f03c @stop=100, @start=1>
117
+ * fb.start #=> 1
118
+ * fb.start = 15 #=> 15
119
+ * fb.start #=> 15
109
120
  *
110
- * Will raise a +TypeError+ exception if given value is not an integer type.
121
+ * Will raise a <i>FizzBuzz::TypeError</i> exception if given value is not
122
+ * an _Integer_ or _Bignum_ type.
111
123
  */
112
124
  VALUE
113
125
  rb_fb_set_start(VALUE object, VALUE value)
114
126
  {
115
127
  VALUE stop = rb_ivar_get(object, id_at_stop);
116
128
 
117
- CHECK_TYPE(value, errors[E_INVALID_START_TYPE]);
118
- CHECK_BOUNDARY(value, stop, errors[E_BAD_VALUE_START]);
129
+ CHECK_TYPE(value, error(E_INVALID_START_TYPE));
130
+ CHECK_RANGE(value, stop, error(E_BAD_VALUE_START));
119
131
 
120
132
  return rb_ivar_set(object, id_at_start, value);
121
133
  }
@@ -128,8 +140,8 @@ rb_fb_set_start(VALUE object, VALUE value)
128
140
  *
129
141
  * Example:
130
142
  *
131
- * fb = FizzBuzz.new(1, 100) #=> #<FizzBuzz:0xf7272bec @stop=100, @start=1>
132
- * fb.stop #=> 100
143
+ * fb = FizzBuzz.new(1, 100) #=> #<FizzBuzz:0xf7272bec @stop=100, @start=1>
144
+ * fb.stop #=> 100
133
145
  */
134
146
  VALUE
135
147
  rb_fb_get_stop(VALUE object)
@@ -139,28 +151,29 @@ rb_fb_get_stop(VALUE object)
139
151
 
140
152
  /*
141
153
  * call-seq:
142
- * fizzfuzz.start= (integer) -> integer
154
+ * fizzfuzz.stop= (integer) -> integer
143
155
  *
144
156
  * Sets the current value of +stop+ if given new value is greater or equal
145
- * to the current value of +start+, or raises an +ArgumentError+ exception
146
- * otherwise.
157
+ * to the current value of +start+, or raises an <i>FizzBuzz::RangeError</i>
158
+ * exception otherwise.
147
159
  *
148
160
  * Example:
149
161
  *
150
- * fb = FizzBuzz.new(1, 100) #=> #<FizzBuzz:0xf727679c @stop=100, @start=1>
151
- * fb.stop #=> 100
152
- * fb.stop = 15 #=> 15
153
- * fb.stop #=> 15
162
+ * fb = FizzBuzz.new(1, 100) #=> #<FizzBuzz:0xf727679c @stop=100, @start=1>
163
+ * fb.stop #=> 100
164
+ * fb.stop = 15 #=> 15
165
+ * fb.stop #=> 15
154
166
  *
155
- * Will raise a +TypeError+ exception if given value is not an integer type.
167
+ * Will raise a <i>FizzBuzz::TypeError</i> exception if given value is not
168
+ * an _Integer_ or _Bignum_ type.
156
169
  */
157
170
  VALUE
158
171
  rb_fb_set_stop(VALUE object, VALUE value)
159
172
  {
160
173
  VALUE start = rb_ivar_get(object, id_at_start);
161
174
 
162
- CHECK_TYPE(value, errors[E_INVALID_STOP_TYPE]);
163
- CHECK_BOUNDARY(start, value, errors[E_BAD_VALUE_STOP]);
175
+ CHECK_TYPE(value, error(E_INVALID_STOP_TYPE));
176
+ CHECK_RANGE(start, value, error(E_BAD_VALUE_STOP));
164
177
 
165
178
  return rb_ivar_set(object, id_at_stop, value);
166
179
  }
@@ -174,8 +187,8 @@ rb_fb_set_stop(VALUE object, VALUE value)
174
187
  *
175
188
  * Example:
176
189
  *
177
- * fb = FizzBuzz.new(1, 15) #=> #<FizzBuzz:0xf727fd60 @stop=15, @start=1>
178
- * fb.to_a #=> [1, 2, "Fizz", 4, "Buzz", "Fizz", 7, 8, "Fizz", "Buzz", 11, "Fizz", 13, 14, "FizzBuzz"]
190
+ * fb = FizzBuzz.new(1, 15) #=> #<FizzBuzz:0xf727fd60 @stop=15, @start=1>
191
+ * fb.to_a #=> [1, 2, "Fizz", 4, "Buzz", "Fizz", 7, 8, "Fizz", "Buzz", 11, "Fizz", 13, 14, "FizzBuzz"]
179
192
  *
180
193
  * See also: FizzBuzz::fizzbuzz
181
194
  */
@@ -197,7 +210,7 @@ rb_fb_array(VALUE object)
197
210
  *
198
211
  * Example:
199
212
  *
200
- * fb = FizzBuzz.new(1, 15) #=> #<FizzBuzz:0xf722f8ec @stop=15, @start=1>
213
+ * fb = FizzBuzz.new(1, 15) #=> #<FizzBuzz:0xf722f8ec @stop=15, @start=1>
201
214
  * fb.each {|value| puts "Got #{value}" }
202
215
  *
203
216
  * Produces:
@@ -239,7 +252,7 @@ rb_fb_enumerator(VALUE object)
239
252
  *
240
253
  * If no block is given, an +Enumerator+ is returned instead.
241
254
  *
242
- * fb = FizzBuzz.new(1, 15) #=> #<FizzBuzz:0xb7308664 @stop=15, @start=1>
255
+ * fb = FizzBuzz.new(1, 15) #=> #<FizzBuzz:0xb7308664 @stop=15, @start=1>
243
256
  * fb.reverse_each {|value| puts "Got #{value}" }
244
257
  *
245
258
  * Produces:
@@ -281,7 +294,8 @@ rb_fb_reverse_enumerator(VALUE object)
281
294
  * FizzBuzz.is_fizz?(5) #=> false
282
295
  * FizzBuzz.is_fizz?(15) #=> false
283
296
  *
284
- * Will raise a +TypeError+ exception if given value is not an integer type.
297
+ * Will raise a <i>FizzBuzz::TypeError</i> exception if given value is not
298
+ * an _Integer_ or _Bignum_ type.
285
299
  *
286
300
  * See also: FizzBuzz::[]
287
301
  */
@@ -289,7 +303,7 @@ VALUE
289
303
  rb_fb_is_fizz(VALUE object, VALUE value)
290
304
  {
291
305
  UNUSED(object);
292
- CHECK_TYPE(value, errors[E_INVALID_TYPE]);
306
+ CHECK_TYPE(value, error(E_INVALID_TYPE));
293
307
 
294
308
  return CBOOL2RVAL(IS_FIZZ(value));
295
309
  }
@@ -307,7 +321,8 @@ rb_fb_is_fizz(VALUE object, VALUE value)
307
321
  * FizzBuzz.is_buzz?(5) #=> true
308
322
  * FizzBuzz.is_buzz?(15) #=> false
309
323
  *
310
- * Will raise a +TypeError+ exception if given value is not an integer type.
324
+ * Will raise a <i>FizzBuzz::TypeError</i> exception if given value is not
325
+ * an _Integer_ or _Bignum_ type.
311
326
  *
312
327
  * See also: FizzBuzz::[]
313
328
  */
@@ -315,7 +330,7 @@ VALUE
315
330
  rb_fb_is_buzz(VALUE object, VALUE value)
316
331
  {
317
332
  UNUSED(object);
318
- CHECK_TYPE(value, errors[E_INVALID_TYPE]);
333
+ CHECK_TYPE(value, error(E_INVALID_TYPE));
319
334
 
320
335
  return CBOOL2RVAL(IS_BUZZ(value));
321
336
  }
@@ -333,7 +348,8 @@ rb_fb_is_buzz(VALUE object, VALUE value)
333
348
  * FizzBuzz.is_fizzbuzz?(5) #=> false
334
349
  * FizzBuzz.is_fizzbuzz?(15) #=> true
335
350
  *
336
- * Will raise a +TypeError+ exception if given value is not an integer type.
351
+ * Will raise a <i>FizzBuzz::TypeError</i> exception if given value is not
352
+ * an _Integer_ or _Bignum_ type.
337
353
  *
338
354
  * See also: FizzBuzz::[]
339
355
  */
@@ -341,7 +357,7 @@ VALUE
341
357
  rb_fb_is_fizzbuzz(VALUE object, VALUE value)
342
358
  {
343
359
  UNUSED(object);
344
- CHECK_TYPE(value, errors[E_INVALID_TYPE]);
360
+ CHECK_TYPE(value, error(E_INVALID_TYPE));
345
361
 
346
362
  return CBOOL2RVAL(IS_FIZZBUZZ(value));
347
363
  }
@@ -362,7 +378,8 @@ rb_fb_is_fizzbuzz(VALUE object, VALUE value)
362
378
  * FizzBuzz[5] #=> "Buzz"
363
379
  * FizzBuzz[15] #=> "FizzBuzz"
364
380
  *
365
- * Will raise a +TypeError+ exception if given value is not an integer type.
381
+ * Will raise a <i>FizzBuzz::TypeError</i> exception if given value is not
382
+ * an _Integer_ or _Bignum_ type.
366
383
  *
367
384
  * See also: FizzBuzz::is_fizz?, FizzBuzz::is_buzz? and FizzBuzz::is_fizzbuzz?
368
385
  */
@@ -370,7 +387,7 @@ VALUE
370
387
  rb_fb_square(VALUE object, VALUE value)
371
388
  {
372
389
  UNUSED(object);
373
- CHECK_TYPE(value, errors[E_INVALID_TYPE]);
390
+ CHECK_TYPE(value, error(E_INVALID_TYPE));
374
391
 
375
392
  return fizzbuzz_evaluate(value);
376
393
  }
@@ -442,6 +459,60 @@ fizzbuzz_values(VALUE object, return_type_t type, direction_t direction)
442
459
  return WANT_ARRAY(type) ? array : object;
443
460
  }
444
461
 
462
+ VALUE
463
+ fizzbuzz_exception_wrapper(VALUE value)
464
+ {
465
+ exception_t *e = (struct exception *)value;
466
+
467
+ return rb_exc_new2(e->klass, e->message);
468
+ }
469
+
470
+ VALUE
471
+ fizzbuzz_exception(void *data)
472
+ {
473
+ int exception = 0;
474
+ VALUE object = Qnil;
475
+
476
+ exception_t *e = data;
477
+
478
+ object = rb_protect(fizzbuzz_exception_wrapper, (VALUE)e, &exception);
479
+
480
+ if (exception != 0) {
481
+ rb_jump_tag(exception);
482
+ }
483
+
484
+ rb_iv_set(object, "@start", e->start);
485
+ rb_iv_set(object, "@stop", e->stop);
486
+
487
+ return object;
488
+ }
489
+
490
+ VALUE
491
+ fizzbuzz_type_error(VALUE klass, const char *message)
492
+ {
493
+ exception_t e;
494
+
495
+ e.start = Qnil;
496
+ e.stop = Qnil;
497
+ e.message = message;
498
+ e.klass = klass;
499
+
500
+ return fizzbuzz_exception(&e);
501
+ }
502
+
503
+ VALUE
504
+ fizzbuzz_range_error(VALUE klass, VALUE start, VALUE stop, const char *message)
505
+ {
506
+ exception_t e;
507
+
508
+ e.start = start;
509
+ e.stop = stop;
510
+ e.message = message;
511
+ e.klass = klass;
512
+
513
+ return fizzbuzz_exception(&e);
514
+ }
515
+
445
516
  void
446
517
  Init_fizzbuzz(void)
447
518
  {
@@ -449,9 +520,36 @@ Init_fizzbuzz(void)
449
520
  id_at_stop = rb_intern("@stop");
450
521
 
451
522
  rb_cFizzBuzz = rb_define_class("FizzBuzz", rb_cObject);
452
-
453
523
  rb_include_module(rb_cFizzBuzz, rb_mEnumerable);
454
524
 
525
+ /*
526
+ * Raised when _FizzBuzz_ encounters an error.
527
+ */
528
+ rb_fb_eError = rb_define_class_under(rb_cFizzBuzz, "Error", rb_eStandardError);
529
+
530
+ /*
531
+ * Stores current value of +start+ for which the exception might have been
532
+ * raised, or +nil+ otherwise.
533
+ */
534
+ rb_define_attr(rb_fb_eError, "start", 1, 0);
535
+
536
+ /*
537
+ * Stores current value of +stop+ for which the exception might have been
538
+ * raised, or +nil+ otherwise.
539
+ */
540
+ rb_define_attr(rb_fb_eError, "stop", 1, 0);
541
+
542
+ /*
543
+ * Raised when the arguments are wrong or of an incompatible type.
544
+ */
545
+ rb_fb_eTypeError = rb_define_class_under(rb_cFizzBuzz, "TypeError", rb_fb_eError);
546
+
547
+ /*
548
+ * Raised when the arguments are wrong or given range from +start+ to +stop+
549
+ * is incorrect.
550
+ */
551
+ rb_fb_eRangeError = rb_define_class_under(rb_cFizzBuzz, "RangeError", rb_fb_eError);
552
+
455
553
  rb_define_method(rb_cFizzBuzz, "initialize", RUBY_METHOD_FUNC(rb_fb_initialize), -1);
456
554
 
457
555
  rb_define_method(rb_cFizzBuzz, "start", RUBY_METHOD_FUNC(rb_fb_get_start), 0);
@@ -66,18 +66,24 @@ extern "C" {
66
66
  #define LOOP_FORWARD(x) ((x) == D_LOOP_FORWARD)
67
67
  #define LOOP_REVERSE(x) ((x) == D_LOOP_REVERSE)
68
68
 
69
- #define CHECK_TYPE(x, m) \
70
- do { \
71
- if (!INTEGER_P(x)) \
72
- rb_raise(rb_eTypeError, "%s", (m)); \
69
+ #define CHECK_TYPE(x, m) \
70
+ do { \
71
+ if (!INTEGER_P(x)) { \
72
+ VALUE __e_type = fizzbuzz_type_error(rb_fb_eTypeError, (m)); \
73
+ rb_exc_raise(__e_type); \
74
+ } \
73
75
  } while (0)
74
76
 
75
- #define CHECK_BOUNDARY(a, b, m) \
76
- do { \
77
- if (GREATER((a), (b))) \
78
- rb_raise(rb_eArgError, "%s", (m)); \
77
+ #define CHECK_RANGE(x, y, m) \
78
+ do { \
79
+ if (GREATER(x, y)) { \
80
+ VALUE __e_range = fizzbuzz_range_error(rb_fb_eRangeError, (x), (y), (m)); \
81
+ rb_exc_raise(__e_range); \
82
+ } \
79
83
  } while (0)
80
84
 
85
+ #define error(t) errors[(t)]
86
+
81
87
  enum error {
82
88
  E_INVALID_TYPE = 0,
83
89
  E_INVALID_START_TYPE,
@@ -96,14 +102,21 @@ enum direction {
96
102
  D_LOOP_REVERSE
97
103
  };
98
104
 
99
- typedef enum error error_t;
105
+ struct exception {
106
+ VALUE start;
107
+ VALUE stop;
108
+ const char *message;
109
+ VALUE klass;
110
+ };
111
+
100
112
  typedef enum return_type return_type_t;
101
113
  typedef enum direction direction_t;
114
+ typedef struct exception exception_t;
102
115
 
103
116
  static const char *errors[] = {
104
- "must be an integer value",
105
- "must be an integer value for start",
106
- "must be an integer value for stop",
117
+ "must be an Integer or Bignum type",
118
+ "must be an Integer or Bignum type for start",
119
+ "must be an Integer or Bignum type for stop",
107
120
  "start value is higher than stop value",
108
121
  "stop value is lower than start value",
109
122
  NULL
@@ -205,8 +218,13 @@ fizzbuzz_less_equal(VALUE a, VALUE b)
205
218
  }
206
219
 
207
220
  RUBY_EXTERN ID id_at_start, id_at_stop;
221
+
208
222
  RUBY_EXTERN VALUE rb_cFizzBuzz;
209
223
 
224
+ RUBY_EXTERN VALUE rb_fb_eError;
225
+ RUBY_EXTERN VALUE rb_fb_eTypeError;
226
+ RUBY_EXTERN VALUE rb_fb_eRangeError;
227
+
210
228
  RUBY_EXTERN VALUE rb_fb_initialize(int argc, VALUE *argv, VALUE object);
211
229
 
212
230
  RUBY_EXTERN VALUE rb_fb_get_start(VALUE object);
data/lib/fizzbuzz.rb CHANGED
@@ -35,8 +35,8 @@ require 'fizzbuzz/bignum'
35
35
  class FizzBuzz
36
36
  #
37
37
  # call-seq:
38
- # FizzBuzz.fizzbuzz( start, stop, reverse ) -> array
39
- # FizzBuzz.fizzbuzz( start, stop, reverse ) {|value| block } -> self
38
+ # FizzBuzz.fizzbuzz( start, stop, reverse ) -> array
39
+ # FizzBuzz.fizzbuzz( start, stop, reverse ) {|value| block } -> self
40
40
  #
41
41
  # Returns either an array or accepts a block if such is given. When a block is given
42
42
  # then it will call the block once for each subsequent value for a given range from
@@ -48,30 +48,30 @@ class FizzBuzz
48
48
  #
49
49
  # Example:
50
50
  #
51
- # FizzBuzz.fizzbuzz(1, 15) #=> [1, 2, "Fizz", 4, "Buzz", "Fizz", 7, 8, "Fizz", "Buzz", 11, "Fizz", 13, 14, "FizzBuzz"]
52
- # FizzBuzz.fizzbuzz(1, 15, true) #=> ["FizzBuzz", 14, 13, "Fizz", 11, "Buzz", "Fizz", 8, 7, "Fizz", "Buzz", 4, "Fizz", 2, 1]
51
+ # FizzBuzz.fizzbuzz(1, 15) #=> [1, 2, "Fizz", 4, "Buzz", "Fizz", 7, 8, "Fizz", "Buzz", 11, "Fizz", 13, 14, "FizzBuzz"]
52
+ # FizzBuzz.fizzbuzz(1, 15, true) #=> ["FizzBuzz", 14, 13, "Fizz", 11, "Buzz", "Fizz", 8, 7, "Fizz", "Buzz", 4, "Fizz", 2, 1]
53
53
  #
54
54
  # Example:
55
55
  #
56
- # FizzBuzz.fizzbuzz(1, 15) {|value| puts "Got #{value}" }
56
+ # FizzBuzz.fizzbuzz(1, 15) {|value| puts "Got #{value}" }
57
57
  #
58
58
  # Produces:
59
59
  #
60
- # Got 1
61
- # Got 2
62
- # Got Fizz
63
- # Got 4
64
- # Got Buzz
65
- # Got Fizz
66
- # Got 7
67
- # Got 8
68
- # Got Fizz
69
- # Got Buzz
70
- # Got 11
71
- # Got Fizz
72
- # Got 13
73
- # Got 14
74
- # Got FizzBuzz
60
+ # Got 1
61
+ # Got 2
62
+ # Got Fizz
63
+ # Got 4
64
+ # Got Buzz
65
+ # Got Fizz
66
+ # Got 7
67
+ # Got 8
68
+ # Got Fizz
69
+ # Got Buzz
70
+ # Got 11
71
+ # Got Fizz
72
+ # Got 13
73
+ # Got 14
74
+ # Got FizzBuzz
75
75
  #
76
76
  # See also: FizzBuzz::[], FizzBuzz::new, FizzBuzz#to_a, FizzBuzz#each and FizzBuzz#reverse_each
77
77
  #
@@ -28,16 +28,16 @@
28
28
  class Bignum
29
29
  #
30
30
  # call-seq:
31
- # Bignum.fizz? -> true or false
31
+ # Bignum.fizz? -> true or false
32
32
  #
33
33
  # Returns +true+ if a given integer value is divisible by *three* (given
34
34
  # value is a _Fizz_), or +false+ otherwise.
35
35
  #
36
36
  # Example:
37
37
  #
38
- # 1000000000000.fizz? #=> false
39
- # 1000000000002.fizz? #=> true
40
- # 1000000000005.fizz? #=> false
38
+ # 1000000000000.fizz? #=> false
39
+ # 1000000000002.fizz? #=> true
40
+ # 1000000000005.fizz? #=> false
41
41
  #
42
42
  # See also: FizzBuzz::[] and FizzBuzz::is_fizz?
43
43
  #
@@ -47,16 +47,16 @@ class Bignum
47
47
 
48
48
  #
49
49
  # call-seq:
50
- # Bignum.buzz? -> true or false
50
+ # Bignum.buzz? -> true or false
51
51
  #
52
52
  # Returns +true+ if a given integer value is divisible by *five* (given
53
53
  # value is a _Buzz_), or +false+ otherwise.
54
54
  #
55
55
  # Example:
56
56
  #
57
- # 1000000000000.buzz? #=> true
58
- # 1000000000002.buzz? #=> false
59
- # 1000000000005.buzz? #=> false
57
+ # 1000000000000.buzz? #=> true
58
+ # 1000000000002.buzz? #=> false
59
+ # 1000000000005.buzz? #=> false
60
60
  #
61
61
  # See also: FizzBuzz::[] and FizzBuzz::is_buzz?
62
62
  #
@@ -66,16 +66,16 @@ class Bignum
66
66
 
67
67
  #
68
68
  # call-seq:
69
- # Bignum.fizzbuzz? -> true or false
69
+ # Bignum.fizzbuzz? -> true or false
70
70
  #
71
71
  # Returns +true+ if a given integer value is divisible by both *three*
72
72
  # and *five* (given value is a _FizzBuzz_), or +false+ otherwise.
73
73
  #
74
74
  # Example:
75
75
  #
76
- # 1000000000000.fizzbuzz? #=> false
77
- # 1000000000002.fizzbuzz? #=> false
78
- # 1000000000005.fizzbuzz? #=> true
76
+ # 1000000000000.fizzbuzz? #=> false
77
+ # 1000000000002.fizzbuzz? #=> false
78
+ # 1000000000005.fizzbuzz? #=> true
79
79
  #
80
80
  # See also: FizzBuzz::[] and FizzBuzz::is_fizzbuzz?
81
81
  #
@@ -28,16 +28,16 @@
28
28
  class Integer
29
29
  #
30
30
  # call-seq:
31
- # Integer.fizz? -> true or false
31
+ # Integer.fizz? -> true or false
32
32
  #
33
33
  # Returns +true+ if a given integer value is divisible by *three* (given
34
34
  # value is a _Fizz_), or +false+ otherwise.
35
35
  #
36
36
  # Example:
37
37
  #
38
- # 3.fizz? #=> true
39
- # 5.fizz? #=> false
40
- # 15.fizz? #=> false
38
+ # 3.fizz? #=> true
39
+ # 5.fizz? #=> false
40
+ # 15.fizz? #=> false
41
41
  #
42
42
  # See also: FizzBuzz::[] and FizzBuzz::is_fizz?
43
43
  #
@@ -47,16 +47,16 @@ class Integer
47
47
 
48
48
  #
49
49
  # call-seq:
50
- # Integer.buzz? -> true or false
50
+ # Integer.buzz? -> true or false
51
51
  #
52
52
  # Returns +true+ if a given integer value is divisible by *five* (given
53
53
  # value is a _Buzz_), or +false+ otherwise.
54
54
  #
55
55
  # Example:
56
56
  #
57
- # 3.buzz? #=> false
58
- # 5.buzz? #=> true
59
- # 15.buzz? #=> false
57
+ # 3.buzz? #=> false
58
+ # 5.buzz? #=> true
59
+ # 15.buzz? #=> false
60
60
  #
61
61
  # See also: FizzBuzz::[] and FizzBuzz::is_buzz?
62
62
  #
@@ -73,9 +73,9 @@ class Integer
73
73
  #
74
74
  # Example:
75
75
  #
76
- # 3.fizzbuzz? #=> false
77
- # 5.fizzbuzz? #=> false
78
- # 15.fizzbuzz? #=> true
76
+ # 3.fizzbuzz? #=> false
77
+ # 5.fizzbuzz? #=> false
78
+ # 15.fizzbuzz? #=> true
79
79
  #
80
80
  # See also: FizzBuzz::[] and FizzBuzz::is_fizzbuzz?
81
81
  #
@@ -26,7 +26,7 @@ class FizzBuzz
26
26
  #
27
27
  # Current version of _FizzBuzz_.
28
28
  #
29
- VERSION = "0.0.2"
29
+ VERSION = '0.0.3'
30
30
  end
31
31
 
32
32
  # :enddoc:
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-fizzbuzz
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-08-11 00:00:00.000000000 Z
12
+ date: 2013-08-31 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -75,7 +75,17 @@ dependencies:
75
75
  - - ! '>='
76
76
  - !ruby/object:Gem::Version
77
77
  version: 0.7.1
78
- description: Yet another FizzBuzz in Ruby
78
+ description: ! 'Yet another FizzBuzz in Ruby
79
+
80
+
81
+ Provides simple and fast solution to a popular FizzBuzz problem for Ruby.
82
+
83
+
84
+ Written in C as an example of using Ruby''s C API - with the support for
85
+
86
+ arbitrary large numeric values via the Bignum class.
87
+
88
+ '
79
89
  email: krzysztof.wilczynski@linux.com
80
90
  executables:
81
91
  - fizzbuzz
@@ -84,8 +94,8 @@ extensions:
84
94
  extra_rdoc_files: []
85
95
  files:
86
96
  - ext/fizzbuzz/fizzbuzz.c
87
- - ext/fizzbuzz/fizzbuzz.h
88
97
  - ext/fizzbuzz/common.h
98
+ - ext/fizzbuzz/fizzbuzz.h
89
99
  - ext/fizzbuzz/extconf.rb
90
100
  - lib/fizzbuzz/bignum.rb
91
101
  - lib/fizzbuzz/integer.rb
@@ -127,7 +137,5 @@ rubyforge_project: ruby-fizzbuzz
127
137
  rubygems_version: 1.8.23
128
138
  signing_key:
129
139
  specification_version: 3
130
- summary: Provides simple and fast solution to a popular FizzBuzz problem for Ruby. Written
131
- in C as an example of using Ruby's C API - with the support for arbitrary large
132
- numeric values via the Bignum class.
140
+ summary: Yet another FizzBuzz in Ruby
133
141
  test_files: []
metadata.gz.sig CHANGED
Binary file