Skip to content

Commit

Permalink
struct.c: dig
Browse files Browse the repository at this point in the history
* object.c (rb_obj_dig): dig in nested structs too.
* struct.c (rb_struct_dig): new method Struct#dig.
  [Feature #11688]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52596 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
nobu committed Nov 16, 2015
1 parent 1ff30ea commit 2069002
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 1 deletion.
7 changes: 7 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
Mon Nov 16 18:21:52 2015 Nobuyoshi Nakada <nobu@ruby-lang.org>

* object.c (rb_obj_dig): dig in nested structs too.

* struct.c (rb_struct_dig): new method Struct#dig.
[Feature #11688]

Mon Nov 16 17:41:33 2015 Nobuyoshi Nakada <nobu@ruby-lang.org>

* compile.c (iseq_peephole_optimize): optimize tail calls on aref
Expand Down
3 changes: 3 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ with all sufficient information, see the ChangeLog file.
Backtrace doesn't show each methods (show block lines directly).
TracePoint also ignore these calls. [Feature #11569]

* Struct
* Struct#dig [Feature #11686]

* Thread
* Thread#name, Thread#name= are added to handle thread names [Feature #11251]

Expand Down
1 change: 1 addition & 0 deletions internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -1141,6 +1141,7 @@ VALUE rb_cstr_intern(const char *ptr, long len, rb_encoding *enc);

/* struct.c */
VALUE rb_struct_init_copy(VALUE copy, VALUE s);
VALUE rb_struct_lookup(VALUE s, VALUE idx);

/* time.c */
struct timeval rb_time_timeval(VALUE);
Expand Down
9 changes: 8 additions & 1 deletion object.c
Original file line number Diff line number Diff line change
Expand Up @@ -3165,7 +3165,7 @@ dig_basic_p(VALUE obj, struct dig_method *cache)
VALUE
rb_obj_dig(int argc, VALUE *argv, VALUE obj, VALUE notfound)
{
struct dig_method hash = {Qnil}, ary = {Qnil};
struct dig_method hash = {Qnil}, ary = {Qnil}, strt = {Qnil};

for (; argc > 0; ++argv, --argc) {
if (!SPECIAL_CONST_P(obj)) {
Expand All @@ -3181,6 +3181,13 @@ rb_obj_dig(int argc, VALUE *argv, VALUE obj, VALUE notfound)
obj = rb_ary_at(obj, *argv);
continue;
}
break;
case T_STRUCT:
if (dig_basic_p(obj, &strt)) {
obj = rb_struct_lookup(obj, *argv);
continue;
}
break;
}
}
return rb_check_funcall_default(obj, id_dig, argc, argv, notfound);
Expand Down
28 changes: 28 additions & 0 deletions struct.c
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,23 @@ rb_struct_aset(VALUE s, VALUE idx, VALUE val)
return val;
}

FUNC_MINIMIZED(VALUE rb_struct_lookup(VALUE s, VALUE idx));
NOINLINE(static VALUE rb_struct_lookup_default(VALUE s, VALUE idx, VALUE notfound));

VALUE
rb_struct_lookup(VALUE s, VALUE idx)
{
return rb_struct_lookup_default(s, idx, Qnil);
}

static VALUE
rb_struct_lookup_default(VALUE s, VALUE idx, VALUE notfound)
{
int i = rb_struct_pos(s, &idx);
if (i < 0) return notfound;
return RSTRUCT_GET(s, i);
}

static VALUE
struct_entry(VALUE s, long n)
{
Expand Down Expand Up @@ -1109,6 +1126,16 @@ rb_struct_size(VALUE s)
return LONG2FIX(RSTRUCT_LEN(s));
}

static VALUE
rb_struct_dig(int argc, VALUE *argv, VALUE self)
{
rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS);
self = rb_struct_lookup(self, *argv);
if (!--argc) return self;
++argv;
return rb_obj_dig(argc, argv, self, Qnil);
}

/*
* A Struct is a convenient way to bundle a number of attributes together,
* using accessor methods, without having to write an explicit class.
Expand Down Expand Up @@ -1166,6 +1193,7 @@ InitVM_Struct(void)
rb_define_method(rb_cStruct, "values_at", rb_struct_values_at, -1);

rb_define_method(rb_cStruct, "members", rb_struct_members_m, 0);
rb_define_method(rb_cStruct, "dig", rb_struct_dig, -1);
}

#undef rb_intern
Expand Down
7 changes: 7 additions & 0 deletions test/ruby/test_struct.rb
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,13 @@ def test_setter_method_returns_value
assert_equal "[Bug #9353]", x.send(:a=, "[Bug #9353]")
end

def test_dig
klass = @Struct.new(:a)
o = klass.new(klass.new({b: [1, 2, 3]}))
assert_equal(1, o.dig(:a, :a, :b, 0))
assert_nil(o.dig(:b, 0))
end

class TopStruct < Test::Unit::TestCase
include TestStruct

Expand Down

0 comments on commit 2069002

Please sign in to comment.