rmtools 2.3.6 → 2.4.0

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.
data/ext/rmtools.h CHANGED
@@ -1,162 +1,162 @@
1
- #ifndef __RUBY_INTERNALS_HH__
2
- #define __RUBY_INTERNALS_HH__
3
-
4
- #include <ruby.h>
5
-
6
- #ifdef RUBY_IS_19
7
-
8
- #include <ruby/intern.h>
9
- #include <ruby/defines.h>
10
- #include <ruby/re.h>
11
- #include <assert.h>
12
-
13
- # define ARY_EMBED_P(ary) \
14
- (assert(!FL_TEST(ary, ELTS_SHARED) || !FL_TEST(ary, RARRAY_EMBED_FLAG)), \
15
- FL_TEST(ary, RARRAY_EMBED_FLAG))
16
- #define ARY_SET_EMBED_LEN(ary, n) do { \
17
- long tmp_n = n; \
18
- RBASIC(ary)->flags &= ~RARRAY_EMBED_LEN_MASK; \
19
- RBASIC(ary)->flags |= (tmp_n) << RARRAY_EMBED_LEN_SHIFT; \
20
- } while (0)
21
- #define ARY_SET_HEAP_LEN(ary, n) do { \
22
- RARRAY(ary)->as.heap.len = n; \
23
- } while (0)
24
- #define ARY_SET_LEN(ary, n) do { \
25
- if (ARY_EMBED_P(ary)) { \
26
- ARY_SET_EMBED_LEN(ary, n); \
27
- } \
28
- else { \
29
- ARY_SET_HEAP_LEN(ary, n); \
30
- } \
31
- } while (0)
32
-
33
- #define BDIGITS(x) ((BDIGIT*)RBIGNUM_DIGITS(x))
34
-
35
- // copied from utilrb gem
36
- typedef struct RNode {
37
- unsigned long flags;
38
- char *nd_file;
39
- union {
40
- struct RNode *node;
41
- ID id;
42
- VALUE value;
43
- VALUE (*cfunc)(ANYARGS);
44
- ID *tbl;
45
- } u1;
46
- union {
47
- struct RNode *node;
48
- ID id;
49
- long argc;
50
- VALUE value;
51
- } u2;
52
- union {
53
- struct RNode *node;
54
- ID id;
55
- long state;
56
- struct global_entry *entry;
57
- long cnt;
58
- VALUE value;
59
- } u3;
60
- } NODE;
61
-
62
- typedef struct RVALUE {
63
- union {
64
- struct {
65
- VALUE flags; /* always 0 for freed obj */
66
- struct RVALUE *next;
67
- } free;
68
- struct RBasic basic;
69
- struct RObject object;
70
- struct RClass klass;
71
- struct RFloat flonum;
72
- struct RString string;
73
- struct RArray array;
74
- struct RRegexp regexp;
75
- struct RHash hash;
76
- struct RData data;
77
- struct RStruct rstruct;
78
- struct RBignum bignum;
79
- struct RFile file;
80
- struct RNode node;
81
- struct RMatch match;
82
- struct RRational rational;
83
- struct RComplex complex;
84
- } as;
85
- } RVALUE;
86
-
87
- static const size_t SLOT_SIZE = sizeof(RVALUE);
88
-
89
- #else
90
-
91
- #include <intern.h>
92
- #include <node.h>
93
- #include <env.h>
94
- #include <re.h>
95
-
96
- # define STR_SET_LEN(x, i) (RSTRING(x)->len = (i))
97
-
98
- #ifndef RFLOAT_VALUE
99
- # define RFLOAT_VALUE(f) (RFLOAT(f)->value)
100
- # define RBIGNUM_DIGITS(f) (RBIGNUM(f)->digits)
101
- # define RBIGNUM_LEN(f) (RBIGNUM(f)->len)
102
- #endif
103
-
104
- # define ARY_SET_LEN(x, i) (RARRAY(x)->len = (i))
105
-
106
- # define BDIGITS(x) ((BDIGIT*)RBIGNUM(x)->digits)
107
-
108
- // copied from utilrb gem
109
- typedef struct RVALUE {
110
- union {
111
- struct {
112
- unsigned long flags; /* always 0 for freed obj */
113
- struct RVALUE *next;
114
- } free;
115
- struct RBasic basic;
116
- struct RObject object;
117
- struct RClass klass;
118
- struct RFloat flonum;
119
- struct RString string;
120
- struct RArray array;
121
- struct RRegexp regexp;
122
- struct RHash hash;
123
- struct RData data;
124
- struct RStruct rstruct;
125
- struct RBignum bignum;
126
- struct RFile file;
127
- struct RNode node;
128
- struct RMatch match;
129
- struct RVarmap varmap;
130
- struct SCOPE scope;
131
- } as;
132
- #ifdef GC_DEBUG
133
- char *file;
134
- int line;
135
- #endif
136
- } RVALUE;
137
- static const size_t SLOT_SIZE = sizeof(RVALUE);
138
-
139
-
140
- #endif // 1.9
141
-
142
-
143
-
144
- // shortcuts for debug r_r
145
- extern void rb_log(const char* str)
146
- {
147
- rb_funcall(rb_gv_get("$log"), rb_intern("debug"), 1, rb_str_new2(str));
148
- }
149
- extern void rb_log_obj(VALUE obj)
150
- {
151
- rb_funcall(rb_gv_get("$log"), rb_intern("debug"), 1, obj);
152
- }
153
- extern void rb_log_file(const char* str)
154
- {
155
- rb_funcall(rb_gv_get("$log"), rb_intern("log"), 1, rb_str_new2(str));
156
- }
157
- extern void rb_log_file_obj(VALUE obj)
158
- {
159
- rb_funcall(rb_gv_get("$log"), rb_intern("log"), 1, obj);
160
- }
161
-
1
+ #ifndef __RUBY_INTERNALS_HH__
2
+ #define __RUBY_INTERNALS_HH__
3
+
4
+ #include <ruby.h>
5
+
6
+ #ifdef RUBY_IS_19
7
+
8
+ #include <ruby/intern.h>
9
+ #include <ruby/defines.h>
10
+ #include <ruby/re.h>
11
+ #include <assert.h>
12
+
13
+ # define ARY_EMBED_P(ary) \
14
+ (assert(!FL_TEST(ary, ELTS_SHARED) || !FL_TEST(ary, RARRAY_EMBED_FLAG)), \
15
+ FL_TEST(ary, RARRAY_EMBED_FLAG))
16
+ #define ARY_SET_EMBED_LEN(ary, n) do { \
17
+ long tmp_n = n; \
18
+ RBASIC(ary)->flags &= ~RARRAY_EMBED_LEN_MASK; \
19
+ RBASIC(ary)->flags |= (tmp_n) << RARRAY_EMBED_LEN_SHIFT; \
20
+ } while (0)
21
+ #define ARY_SET_HEAP_LEN(ary, n) do { \
22
+ RARRAY(ary)->as.heap.len = n; \
23
+ } while (0)
24
+ #define ARY_SET_LEN(ary, n) do { \
25
+ if (ARY_EMBED_P(ary)) { \
26
+ ARY_SET_EMBED_LEN(ary, n); \
27
+ } \
28
+ else { \
29
+ ARY_SET_HEAP_LEN(ary, n); \
30
+ } \
31
+ } while (0)
32
+
33
+ #define BDIGITS(x) ((BDIGIT*)RBIGNUM_DIGITS(x))
34
+
35
+ // copied from utilrb gem
36
+ typedef struct RNode {
37
+ unsigned long flags;
38
+ char *nd_file;
39
+ union {
40
+ struct RNode *node;
41
+ ID id;
42
+ VALUE value;
43
+ VALUE (*cfunc)(ANYARGS);
44
+ ID *tbl;
45
+ } u1;
46
+ union {
47
+ struct RNode *node;
48
+ ID id;
49
+ long argc;
50
+ VALUE value;
51
+ } u2;
52
+ union {
53
+ struct RNode *node;
54
+ ID id;
55
+ long state;
56
+ struct global_entry *entry;
57
+ long cnt;
58
+ VALUE value;
59
+ } u3;
60
+ } NODE;
61
+
62
+ typedef struct RVALUE {
63
+ union {
64
+ struct {
65
+ VALUE flags; /* always 0 for freed obj */
66
+ struct RVALUE *next;
67
+ } free;
68
+ struct RBasic basic;
69
+ struct RObject object;
70
+ struct RClass klass;
71
+ struct RFloat flonum;
72
+ struct RString string;
73
+ struct RArray array;
74
+ struct RRegexp regexp;
75
+ struct RHash hash;
76
+ struct RData data;
77
+ struct RStruct rstruct;
78
+ struct RBignum bignum;
79
+ struct RFile file;
80
+ struct RNode node;
81
+ struct RMatch match;
82
+ struct RRational rational;
83
+ struct RComplex complex;
84
+ } as;
85
+ } RVALUE;
86
+
87
+ static const size_t SLOT_SIZE = sizeof(RVALUE);
88
+
89
+ #else
90
+
91
+ #include <intern.h>
92
+ #include <node.h>
93
+ #include <env.h>
94
+ #include <re.h>
95
+
96
+ # define STR_SET_LEN(x, i) (RSTRING(x)->len = (i))
97
+
98
+ #ifndef RFLOAT_VALUE
99
+ # define RFLOAT_VALUE(f) (RFLOAT(f)->value)
100
+ # define RBIGNUM_DIGITS(f) (RBIGNUM(f)->digits)
101
+ # define RBIGNUM_LEN(f) (RBIGNUM(f)->len)
102
+ #endif
103
+
104
+ # define ARY_SET_LEN(x, i) (RARRAY(x)->len = (i))
105
+
106
+ # define BDIGITS(x) ((BDIGIT*)RBIGNUM(x)->digits)
107
+
108
+ // copied from utilrb gem
109
+ typedef struct RVALUE {
110
+ union {
111
+ struct {
112
+ unsigned long flags; /* always 0 for freed obj */
113
+ struct RVALUE *next;
114
+ } free;
115
+ struct RBasic basic;
116
+ struct RObject object;
117
+ struct RClass klass;
118
+ struct RFloat flonum;
119
+ struct RString string;
120
+ struct RArray array;
121
+ struct RRegexp regexp;
122
+ struct RHash hash;
123
+ struct RData data;
124
+ struct RStruct rstruct;
125
+ struct RBignum bignum;
126
+ struct RFile file;
127
+ struct RNode node;
128
+ struct RMatch match;
129
+ struct RVarmap varmap;
130
+ struct SCOPE scope;
131
+ } as;
132
+ #ifdef GC_DEBUG
133
+ char *file;
134
+ int line;
135
+ #endif
136
+ } RVALUE;
137
+ static const size_t SLOT_SIZE = sizeof(RVALUE);
138
+
139
+
140
+ #endif // 1.9
141
+
142
+
143
+
144
+ // shortcuts for debug r_r
145
+ extern void rb_log(const char* str)
146
+ {
147
+ rb_funcall(rb_gv_get("$log"), rb_intern("debug"), 1, rb_str_new2(str));
148
+ }
149
+ extern void rb_log_obj(VALUE obj)
150
+ {
151
+ rb_funcall(rb_gv_get("$log"), rb_intern("debug"), 1, obj);
152
+ }
153
+ extern void rb_log_file(const char* str)
154
+ {
155
+ rb_funcall(rb_gv_get("$log"), rb_intern("log"), 1, rb_str_new2(str));
156
+ }
157
+ extern void rb_log_file_obj(VALUE obj)
158
+ {
159
+ rb_funcall(rb_gv_get("$log"), rb_intern("log"), 1, obj);
160
+ }
161
+
162
162
  #endif
@@ -32,7 +32,7 @@ module RMTools
32
32
  method_defined? :p and undef_method :p
33
33
 
34
34
  def demo(str, pattern=nil)
35
- %w[black red green yellow blue purple cyan gray].xprod(%w[bold underline graybg boldbg]).each {|color, effect|
35
+ %w[black red green yellow blue purple cyan gray].product(%w[bold underline graybg boldbg]).each {|color, effect|
36
36
  if pattern
37
37
  puts ghl(str, pattern, "#{color}_#{effect}")
38
38
  else
@@ -15,7 +15,7 @@ module Enumerable
15
15
  # => "a=10&a=20&b=10&b=20"
16
16
  # Stringifies hashes with hash-value values:
17
17
  # {'a'=>{0=>10, 'b'=>{'c'=>20, 'd'=>30}}}.urlencode
18
- # => "a[0]=10&a[b]=20"
18
+ # => "a[0]=10&a[b][c]=20&a[b][d]=30"
19
19
 
20
20
  # TODO: deal with it
21
21
  def urlencode
@@ -72,8 +72,8 @@ rescue LoadError
72
72
  end
73
73
 
74
74
  class Object
75
- # handy when data may not be encodable (active_record objects, recursive structures, etc)
76
- def to_json_safe(options=nil)
77
- timeout(10) {to_json(options)}
75
+ # handy when data may appear non-encodable (active_record objects, recursive structures, etc)
76
+ def to_json_safe(options=nil, timeout: 10)
77
+ timeout(timeout) {to_json(options)}
78
78
  end
79
79
  end
@@ -37,7 +37,6 @@ class Array
37
37
  defaults.slice! 0, size
38
38
  concat defaults << opts
39
39
  end
40
- alias :fetch_options :fetch_opts
41
40
  alias :get_opts :fetch_opts
42
41
 
43
42
  def valid_types(pattern_ary)
@@ -12,8 +12,10 @@ class Numeric
12
12
  def b; !zero? && self end
13
13
  end
14
14
 
15
- class String
16
- def b; !empty? && self end
15
+ if RUBY_VERSION < '2.1'
16
+ class String
17
+ def b; !empty? && self end
18
+ end
17
19
  end
18
20
 
19
21
  class Proc
@@ -2,7 +2,6 @@
2
2
 
3
3
  # Javascript hash getter/setter and string concat logic
4
4
  class Hash
5
- alias :throw_no :method_missing
6
5
 
7
6
  # hash = {}
8
7
  # hash.abc = 123
@@ -19,7 +18,7 @@ class Hash
19
18
  if str =~ /=$/
20
19
  self[str[0..-2]] = args[0]
21
20
  elsif !args.empty? or str =~ /[!?]$/
22
- throw_no method
21
+ super
23
22
  else
24
23
  a = self[method]
25
24
  (a == default) ? self[str] : a
@@ -35,6 +34,10 @@ class Hash
35
34
  a = self[:id]
36
35
  (a == default) ? self['id'] : a
37
36
  end
37
+ def index
38
+ a = self[:index]
39
+ (a == default) ? self['index'] : a
40
+ end
38
41
 
39
42
  end
40
43
 
@@ -1,24 +1,31 @@
1
1
  # encoding: utf-8
2
2
  class Numeric
3
3
 
4
+ # ceil-round to i-fold
4
5
  def ceil_to(i)
5
- self + i - self%i
6
+ self + i - self % i
6
7
  end
7
8
 
9
+ # floor-round to i-fold
8
10
  def floor_to(i)
9
- self - self%i
11
+ self - self % i
10
12
  end
11
13
 
14
+ # closest-round to i-fold
12
15
  def round_to(i)
13
- [ceil_to(i), floor_to(i)].max
16
+ ceil = ceil_to i
17
+ floor = floor_to i
18
+ ceil - self < self - floor ? ceil : floor
14
19
  end
15
20
 
21
+ # is self lies between two numerics
16
22
  def between(min, max)
17
23
  min < self and self < max
18
24
  end
19
25
 
26
+ # is self multiple of numeric
20
27
  def mult_of(subj)
21
- self%subj == 0
28
+ self % subj == 0
22
29
  end
23
30
 
24
31
  def hex
@@ -29,7 +36,8 @@ end
29
36
 
30
37
  module Math
31
38
 
32
- def logb(b, x) log(x)/log(b) end
39
+ def logb(b, x)
40
+ log(x)/log(b)
41
+ end
33
42
 
34
- end
35
-
43
+ end