Merge pull request #3430 from ksss/local_variables

Kernel#local_variables: Make result array unique
This commit is contained in:
Yukihiro "Matz" Matsumoto
2017-02-06 15:58:53 +09:00
committed by GitHub
2 changed files with 9 additions and 9 deletions
+6 -5
View File
@@ -6,6 +6,7 @@
#include <mruby.h>
#include <mruby/array.h>
#include <mruby/hash.h>
#include <mruby/class.h>
#include <mruby/proc.h>
#include <mruby/string.h>
@@ -1106,8 +1107,8 @@ mrb_obj_ceqq(mrb_state *mrb, mrb_value self)
static mrb_value
mrb_local_variables(mrb_state *mrb, mrb_value self)
{
mrb_value ret;
struct RProc *proc;
mrb_value vars;
struct mrb_irep *irep;
size_t i;
@@ -1121,10 +1122,10 @@ mrb_local_variables(mrb_state *mrb, mrb_value self)
if (!irep->lv) {
return mrb_ary_new(mrb);
}
ret = mrb_ary_new_capa(mrb, irep->nlocals - 1);
vars = mrb_hash_new(mrb);
for (i = 0; i + 1 < irep->nlocals; ++i) {
if (irep->lv[i].name) {
mrb_ary_push(mrb, ret, mrb_symbol_value(irep->lv[i].name));
mrb_hash_set(mrb, vars, mrb_symbol_value(irep->lv[i].name), mrb_true_value());
}
}
if (proc->env) {
@@ -1137,7 +1138,7 @@ mrb_local_variables(mrb_state *mrb, mrb_value self)
if (irep->lv) {
for (i = 0; i + 1 < irep->nlocals; ++i) {
if (irep->lv[i].name) {
mrb_ary_push(mrb, ret, mrb_symbol_value(irep->lv[i].name));
mrb_hash_set(mrb, vars, mrb_symbol_value(irep->lv[i].name), mrb_true_value());
}
}
}
@@ -1146,7 +1147,7 @@ mrb_local_variables(mrb_state *mrb, mrb_value self)
}
}
return ret;
return mrb_hash_keys(mrb, vars);
}
void
+3 -4
View File
@@ -549,11 +549,10 @@ assert('Kernel.local_variables', '15.3.1.2.7') do
vars = Kernel.local_variables.sort
assert_equal [:a, :b, :vars], vars
Proc.new {
assert_equal [:a, :b, :c, :vars], Proc.new { |a, b|
c = 2
vars = Kernel.local_variables.sort
assert_equal [:a, :b, :c, :vars], vars
}.call
Kernel.local_variables.sort
}.call(-1, -2)
end
assert('Kernel#!=') do