supplement 2.29 → 2.31

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/lib/supplement.c +88 -22
  4. data/lib/supplement.h +1 -0
  5. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9d98b193bc4d1cc8ede4e690103285660b92acbef10bc2c8c9fbe9774968b55e
4
- data.tar.gz: ce38aef42ac4a5aceb953eee5090d62b09da647c6badd39eb13fcf6ae75124d7
3
+ metadata.gz: 0ecb9754ff9168f112f5cc9535bdd6fa1dd1877cb7eb4738757a32854a56c325
4
+ data.tar.gz: 983d0b6c6c91258fd74eb0e6fe31f16124654e730d750a09105fe66873086915
5
5
  SHA512:
6
- metadata.gz: c8a1ca6542111abec4919e6b0fafbc665e88baf3de003ccb696586060b4508f0d8c95c5a85d5b2b2bbab85aad0f2c7132bee0458dd64f799d6825c8f404e6754
7
- data.tar.gz: 30703341388fdd2895f68879925eef1d4a1e94c22e177470cc7689b5e7a0addd5693bab20969afa8b127e6a137e5794dba323bdf2d1e2824adfe73b813103dc5
6
+ metadata.gz: 31724b42dca923848441e648d91757c58bb212a63d91a3613fbedb760b6894f64d432bfb30de89f95fbba93002b9e316283efab175ef747e162ca174ac25e3c4
7
+ data.tar.gz: 5f9e2ff5d368ea29e22bc2218c22b2261b582a8d81724ae5fb48f841fd1c6d77d3367950a68372a74fd8287a8b51b08ba0c7e7635d5bed296619362103f2f5bb
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # supplement 2.29 -- Useful Ruby enhancements
1
+ # supplement 2.31 -- Useful Ruby enhancements
2
2
 
3
3
  Some simple Ruby extensions.
4
4
 
data/lib/supplement.c CHANGED
@@ -11,6 +11,8 @@
11
11
  #include <ruby/re.h>
12
12
 
13
13
 
14
+ static long supplement_args_len_default( int, VALUE *);
15
+ static void supplement_make_ellipse( void);
14
16
  static void supplement_ary_assure_notempty( VALUE);
15
17
  static VALUE supplement_index_blk( VALUE);
16
18
  static VALUE supplement_index_ref( VALUE, VALUE);
@@ -19,6 +21,9 @@ static VALUE supplement_rindex_ref( VALUE, VALUE);
19
21
  static VALUE supplement_do_unumask( VALUE);
20
22
 
21
23
 
24
+ static VALUE supplement_ellipse = Qnil;
25
+ static long supplement_len_ell = 0;
26
+
22
27
  static ID id_delete_at = 0;
23
28
  static ID id_cmp = 0;
24
29
  static ID id_eqq = 0;
@@ -111,8 +116,10 @@ rb_krn_tap_bang( VALUE obj)
111
116
  * call-seq:
112
117
  * with { |x| ... } -> obj
113
118
  *
119
+ * Warning. This method will be removed. Use +Kernel#then+ instead.
120
+ *
114
121
  * Yields +x+ to the block.
115
- * This difference to +tap+ is that the block's result will be returned.
122
+ * The difference to +tap+ is that the block's result will be returned.
116
123
  *
117
124
  * Use this to narrow your namespace.
118
125
  *
@@ -121,6 +128,8 @@ rb_krn_tap_bang( VALUE obj)
121
128
  VALUE
122
129
  rb_krn_with( VALUE obj)
123
130
  {
131
+ rb_category_warning(RB_WARN_CATEGORY_DEPRECATED,
132
+ "Kernel#with will be removed from the supplement gem. Use Kernel#then instead.");
124
133
  return rb_yield( obj);
125
134
  }
126
135
 
@@ -391,49 +400,102 @@ rb_str_tail( int argc, VALUE *argv, VALUE str)
391
400
  }
392
401
 
393
402
 
403
+ long
404
+ supplement_args_len_default( int argc, VALUE *argv)
405
+ {
406
+ VALUE n;
407
+ long r;
408
+
409
+ if (rb_scan_args( argc, argv, "01", &n) == 1 && !NIL_P( n))
410
+ r = NUM2LONG( n);
411
+ else
412
+ r = 80;
413
+ return r;
414
+ }
415
+
416
+ void
417
+ supplement_make_ellipse( void)
418
+ {
419
+ if (!NIL_P( supplement_ellipse))
420
+ return;
421
+ supplement_ellipse = rb_str_new2( "...");
422
+ supplement_len_ell = rb_str_strlen( supplement_ellipse);
423
+ }
424
+
394
425
  /*
395
426
  * call-seq:
396
427
  * axe( n = 80) -> str
397
428
  *
398
- * Cut off everthing beyond then <code>n</code>th character. Replace the
429
+ * Cut off everthing beyond the <code>n</code>th character. Replace the
399
430
  * last bytes by ellipses.
400
431
  *
401
- * a = "Redistribution and use in source and binary forms, with or without"
402
- * a.axe( 16) #=> "Redistributio..."
432
+ * a = "Now is the time for all good men to come to the aid of their country etc."
433
+ * a.axe( 16) #=> "Now is the ti..."
403
434
  */
404
435
 
405
436
  VALUE
406
437
  rb_str_axe( int argc, VALUE *argv, VALUE str)
407
438
  {
408
- VALUE n;
409
439
  VALUE ret;
410
440
  long newlen, oldlen;
411
441
 
412
- if (rb_scan_args( argc, argv, "01", &n) == 1 && !NIL_P( n))
413
- newlen = NUM2LONG( n);
414
- else
415
- newlen = 80;
442
+ newlen = supplement_args_len_default( argc, argv);
416
443
  if (newlen < 0)
417
444
  return Qnil;
418
445
 
419
446
  oldlen = rb_str_strlen( str);
420
447
  if (newlen < oldlen) {
421
- VALUE ell;
422
- long e;
423
-
424
- ell = rb_str_new2( "...");
425
- e = rb_str_strlen( ell);
426
- if (newlen > e) {
427
- ret = rb_str_substr( str, 0, newlen - e);
428
- rb_str_append( ret, ell);
448
+ supplement_make_ellipse();
449
+ if (newlen >= supplement_len_ell) {
450
+ ret = rb_str_substr( str, 0, newlen - supplement_len_ell);
451
+ rb_str_append( ret, supplement_ellipse);
429
452
  } else
430
- ret = rb_str_substr( str, 0, newlen);
453
+ ret = rb_str_substr( supplement_ellipse, 0, newlen);
431
454
  } else
432
455
  ret = str;
433
456
  return ret;
434
457
  }
435
458
 
436
459
 
460
+ /*
461
+ * call-seq:
462
+ * axe!( n = 80) -> str
463
+ *
464
+ * Cut off everthing beyond the <code>n</code>th character. Replace the
465
+ * last bytes by ellipses.
466
+ *
467
+ * a = "Now is the time for all good men to come to the aid of their country etc."
468
+ * a.axe!( 16) #=> "Now is the ti..."
469
+ * a #=> "Now is the ti..."
470
+ *
471
+ * If nothing was removed, <code>nil</code> is returned.
472
+ */
473
+
474
+ VALUE
475
+ rb_str_axe_bang( int argc, VALUE *argv, VALUE str)
476
+ {
477
+ long newlen, oldlen;
478
+
479
+ newlen = supplement_args_len_default( argc, argv);
480
+ if (newlen < 0)
481
+ return Qnil;
482
+
483
+ rb_str_modify( str);
484
+
485
+ oldlen = rb_str_strlen( str);
486
+ if (newlen < oldlen) {
487
+ long re;
488
+
489
+ rb_str_update( str, newlen, oldlen - newlen, rb_str_new( NULL, 0));
490
+ supplement_make_ellipse();
491
+ re = newlen < supplement_len_ell ? newlen : supplement_len_ell;
492
+ rb_str_update( str, newlen - re, re, rb_str_substr( supplement_ellipse, 0, re));
493
+ return str;
494
+ } else
495
+ return Qnil;
496
+ }
497
+
498
+
437
499
  /*
438
500
  * call-seq:
439
501
  * starts_with?( *oth) -> nil or int
@@ -443,7 +505,7 @@ rb_str_axe( int argc, VALUE *argv, VALUE str)
443
505
  *
444
506
  * "sys-apps".starts_with?( "sys-") #=> 4
445
507
  *
446
- * Caution! The Ruby 1.9.3 method #start_with? (notice the missing s)
508
+ * Caution! The Ruby 1.9.3 method #start_with? (note the missing s)
447
509
  * just returns +true+ or +false+. Mnemonics: "s" = prepare for
448
510
  * <code>#slice</code>.
449
511
  */
@@ -485,7 +547,7 @@ rb_str_starts_with_p( int argc, VALUE *argv, VALUE str)
485
547
  *
486
548
  * "sys-apps".ends_with?( "-apps") #=> 3
487
549
  *
488
- * Caution! The Ruby 1.9.3 method #start_with? (notice the missing s)
550
+ * Caution! The Ruby 1.9.3 method #start_with? (note the missing s)
489
551
  * just returns +true+ or +false+.
490
552
  */
491
553
 
@@ -525,7 +587,7 @@ rb_str_ends_with_p( int argc, VALUE *argv, VALUE str)
525
587
  *
526
588
  * :"sys-apps".starts_with?( "sys-") #=> 4
527
589
  *
528
- * Caution! The Ruby 1.9.3 method #start_with? (notice the missing s)
590
+ * Caution! The Ruby 1.9.3 method #start_with? (note the missing s)
529
591
  * just returns +true+ or +false+. Mnemonics: "s" = prepare for
530
592
  * <code>#slice</code>.
531
593
  */
@@ -546,7 +608,7 @@ rb_sym_starts_with_p( int argc, VALUE *argv, VALUE sym)
546
608
  *
547
609
  * :"sys-apps".ends_with?( "-apps") #=> 3
548
610
  *
549
- * Caution! The Ruby 1.9.3 method #start_with? (notice the missing s)
611
+ * Caution! The Ruby 1.9.3 method #start_with? (note the missing s)
550
612
  * just returns +true+ or +false+.
551
613
  */
552
614
 
@@ -1181,6 +1243,7 @@ void Init_supplement( void)
1181
1243
  rb_define_method( rb_cString, "rest", rb_str_rest, -1);
1182
1244
  rb_define_method( rb_cString, "tail", rb_str_tail, -1);
1183
1245
  rb_define_method( rb_cString, "axe", rb_str_axe, -1);
1246
+ rb_define_method( rb_cString, "axe!", rb_str_axe_bang, -1);
1184
1247
  rb_define_method( rb_cString, "starts_with?", rb_str_starts_with_p, -1);
1185
1248
  rb_define_method( rb_cString, "ends_with?", rb_str_ends_with_p, -1);
1186
1249
  rb_define_method( rb_cSymbol, "starts_with?", rb_sym_starts_with_p, -1);
@@ -1202,12 +1265,15 @@ void Init_supplement( void)
1202
1265
 
1203
1266
  rb_define_method( rb_cHash, "notempty?", rb_hash_notempty_p, 0);
1204
1267
 
1268
+ rb_undef_method( CLASS_OF( rb_cFile), "umask");
1205
1269
  rb_define_singleton_method( rb_cFile, "umask", rb_file_s_umask, -1);
1206
1270
 
1207
1271
  rb_define_singleton_method( rb_cDir, "current", rb_dir_s_current, 0);
1208
1272
  rb_define_singleton_method( rb_cDir, "mkdir!", rb_dir_s_mkdir_bang, -1);
1209
1273
  rb_define_alias( rb_cDir, "entries!", "children");
1210
1274
 
1275
+ rb_undef_method( rb_cMatch, "begin");
1276
+ rb_undef_method( rb_cMatch, "end");
1211
1277
  rb_define_method( rb_cMatch, "begin", rb_match_begin, -1);
1212
1278
  rb_define_method( rb_cMatch, "end", rb_match_end, -1);
1213
1279
 
data/lib/supplement.h CHANGED
@@ -27,6 +27,7 @@ extern VALUE rb_str_head( int, VALUE *, VALUE);
27
27
  extern VALUE rb_str_rest( int, VALUE *, VALUE);
28
28
  extern VALUE rb_str_tail( int, VALUE *, VALUE);
29
29
  extern VALUE rb_str_axe( int, VALUE *, VALUE);
30
+ extern VALUE rb_str_axe_bang( int, VALUE *, VALUE);
30
31
  extern VALUE rb_str_starts_with_p( int argc, VALUE *argv, VALUE);
31
32
  extern VALUE rb_str_ends_with_p( int argc, VALUE *argv, VALUE);
32
33
  extern VALUE rb_sym_starts_with_p( int argc, VALUE *argv, VALUE);
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: supplement
3
3
  version: !ruby/object:Gem::Version
4
- version: '2.29'
4
+ version: '2.31'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bertram Scharpf