gvars 0.9.0 → 0.9.5
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.
- checksums.yaml +4 -4
- data/ext/gvars/gvars.c +51 -1
- data/lib/gvars/version.rb +1 -1
- data/lib/gvars.rb +14 -7
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 7a3f32ce689ea5b4077c4736674afc353494fafd71bec8dc0e108cc95c33424a
|
|
4
|
+
data.tar.gz: 237e9f7383637a345dde01342598ec24b01c8e4f8e953f33cde03fc164cb1ba8
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 54094ee144a422dca37c6ae607d1250829b987c33f92b38f7fb6c2b5ef6aa291a8249bb170db0330c0eac2020178ae6fb4579ee95e82f3f810561f978dc8f1d5
|
|
7
|
+
data.tar.gz: 7052348eee6adec41144417564e472575b943f03b4a9edfcf3d2a1e9e715dcde50eb3f56393c8423c42319629ba0a4a97ef0846f4b38b841fb0b063d72f0b280
|
data/ext/gvars/gvars.c
CHANGED
|
@@ -20,6 +20,7 @@ static char *get_global_name(VALUE *name) {
|
|
|
20
20
|
return namestr;
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
+
|
|
23
24
|
static VALUE
|
|
24
25
|
gvars_f_global_variable_get(VALUE self, VALUE name)
|
|
25
26
|
{
|
|
@@ -46,6 +47,41 @@ gvars_f_alias_global_variable(VALUE self, VALUE new, VALUE old)
|
|
|
46
47
|
return ID2SYM(newid);
|
|
47
48
|
}
|
|
48
49
|
|
|
50
|
+
static VALUE
|
|
51
|
+
gvars_enum_length(VALUE self, VALUE args, VALUE eobj)
|
|
52
|
+
{
|
|
53
|
+
return LONG2NUM(RARRAY_LEN(rb_f_global_variables()));
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
static VALUE
|
|
57
|
+
gvars_f_each(VALUE self)
|
|
58
|
+
{
|
|
59
|
+
RETURN_SIZED_ENUMERATOR(self, 0, 0, gvars_enum_length);
|
|
60
|
+
|
|
61
|
+
VALUE gvars = gvars_f_global_variables(self);
|
|
62
|
+
for (long i = 0; i < RARRAY_LEN(gvars); i++) {
|
|
63
|
+
VALUE key = RARRAY_AREF(gvars, i);
|
|
64
|
+
rb_yield(rb_ary_new3(2, key, gvars_f_global_variable_get(self, key)));
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return self;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
static VALUE
|
|
71
|
+
gvars_f_to_h(VALUE self)
|
|
72
|
+
{
|
|
73
|
+
VALUE gvars = gvars_f_global_variables(self);
|
|
74
|
+
VALUE hash = rb_hash_new_capa(RARRAY_LEN(gvars));
|
|
75
|
+
|
|
76
|
+
for (long i = 0; i < RARRAY_LEN(gvars); i++) {
|
|
77
|
+
VALUE key = RARRAY_AREF(gvars, i);
|
|
78
|
+
rb_hash_aset(hash, key, gvars_f_global_variable_get(self, key));
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return hash;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
|
|
49
85
|
VALUE gvars_module;
|
|
50
86
|
|
|
51
87
|
void
|
|
@@ -58,6 +94,20 @@ Init_gvars(void)
|
|
|
58
94
|
rb_define_module_function(gvars_module, "global_variable_set", gvars_f_global_variable_set, 2);
|
|
59
95
|
rb_define_module_function(gvars_module, "alias_global_variable", gvars_f_alias_global_variable, 2);
|
|
60
96
|
|
|
61
|
-
// Don't make
|
|
97
|
+
// Don't make it an instance method, as it exists in Kernel
|
|
62
98
|
rb_define_singleton_method(gvars_module, "global_variables", gvars_f_global_variables, 0);
|
|
99
|
+
|
|
100
|
+
// Enumerable methods
|
|
101
|
+
rb_extend_object(gvars_module, rb_mEnumerable);
|
|
102
|
+
rb_define_singleton_method(gvars_module, "each", gvars_f_each, 0);
|
|
103
|
+
rb_define_singleton_method(gvars_module, "to_h", gvars_f_to_h, 0);
|
|
104
|
+
|
|
105
|
+
// Aliases
|
|
106
|
+
VALUE gvars_singleton = rb_singleton_class(gvars_module);
|
|
107
|
+
rb_define_alias(gvars_singleton, "get", "global_variable_get");
|
|
108
|
+
rb_define_alias(gvars_singleton, "[]", "global_variable_get");
|
|
109
|
+
rb_define_alias(gvars_singleton, "set", "global_variable_set");
|
|
110
|
+
rb_define_alias(gvars_singleton, "[]=", "global_variable_set");
|
|
111
|
+
rb_define_alias(gvars_singleton, "alias", "alias_global_variable");
|
|
112
|
+
rb_define_alias(gvars_singleton, "list", "global_variables");
|
|
63
113
|
}
|
data/lib/gvars/version.rb
CHANGED
data/lib/gvars.rb
CHANGED
|
@@ -4,12 +4,19 @@ require_relative "gvars/version"
|
|
|
4
4
|
require_relative "gvars/gvars"
|
|
5
5
|
|
|
6
6
|
module GVars
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
7
|
+
=begin
|
|
8
|
+
# TODO: make this non-eval; unfortunately, it's not easy to fully emulate right now. maybe in the future?
|
|
9
|
+
#
|
|
10
|
+
# NOTE: THIS DOESN'T SUPPORT `$_` or regex vars!
|
|
11
|
+
module_function def global_variable_defined?(key)
|
|
12
|
+
warn "GVArs.global_variable_defined? is currently experimental", category: :experimental, uplevel: 1
|
|
13
|
+
key = Symbol === key ? key : key.to_str.to_sym
|
|
14
|
+
|
|
15
|
+
unless key.match? /\A\$[[:alpha:]_][[:alnum:]_]*\z/
|
|
16
|
+
raise NameError, format("'%s' is not allowed as a global variable name", key)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
!! eval "defined? #{key}"
|
|
14
20
|
end
|
|
21
|
+
=end
|
|
15
22
|
end
|