diff options
| author | Yukihiro "Matz" Matsumoto <[email protected]> | 2012-11-12 13:00:24 -0800 |
|---|---|---|
| committer | Yukihiro "Matz" Matsumoto <[email protected]> | 2012-11-12 13:00:24 -0800 |
| commit | 1cd9d7e40abb8112fe9f05193f9af6ec357a71eb (patch) | |
| tree | 7260feb794228246b8ffe4563d1f3b012e5cc45e /src/variable.c | |
| parent | 191d1391c66ddca71bf9e19f5af18478e73428d7 (diff) | |
| parent | 611cfee3c2ff1a06760cbe4d0d1415f65087b505 (diff) | |
| download | mruby-1cd9d7e40abb8112fe9f05193f9af6ec357a71eb.tar.gz mruby-1cd9d7e40abb8112fe9f05193f9af6ec357a71eb.zip | |
Merge pull request #546 from skandhas/pr-add-class-variables
add Module#class_variables
Diffstat (limited to 'src/variable.c')
| -rw-r--r-- | src/variable.c | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/src/variable.c b/src/variable.c index b3b3b3d87..19853685c 100644 --- a/src/variable.c +++ b/src/variable.c @@ -615,6 +615,50 @@ mrb_obj_instance_variables(mrb_state *mrb, mrb_value self) return ary; } +static int +cv_i(mrb_state *mrb, mrb_sym sym, mrb_value v, void *p) +{ + mrb_value ary; + const char* s; + int len; + + ary = *(mrb_value*)p; + s = mrb_sym2name_len(mrb, sym, &len); + if (len > 2 && s[0] == '@' && s[1] == '@') { + mrb_ary_push(mrb, ary, mrb_symbol_value(sym)); + } + return 0; +} + +/* 15.2.2.4.19 */ +/* + * call-seq: + * mod.class_variables -> array + * + * Returns an array of the names of class variables in <i>mod</i>. + * + * class One + * @@var1 = 1 + * end + * class Two < One + * @@var2 = 2 + * end + * One.class_variables #=> [:@@var1] + * Two.class_variables #=> [:@@var2] + */ +mrb_value +mrb_mod_class_variables(mrb_state *mrb, mrb_value mod) +{ + mrb_value ary; + + ary = mrb_ary_new(mrb); + if (obj_iv_p(mod) && mrb_obj_ptr(mod)->iv) { + iv_foreach(mrb, mrb_obj_ptr(mod)->iv, cv_i, &ary); + } + return ary; +} + + mrb_value mrb_vm_cv_get(mrb_state *mrb, mrb_sym sym) { |
