Skip to content

Commit

Permalink
Added rb_hash_lookup2()
Browse files Browse the repository at this point in the history
This is basically rb_hash_lookup() except it returns a custom default
value instead of Qnil.
  • Loading branch information
Yorick Peterse committed May 8, 2015
1 parent a8a1150 commit 3871c2b
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 2 deletions.
10 changes: 10 additions & 0 deletions spec/ruby/optional/capi/ext/hash_spec.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ VALUE hash_spec_rb_hash_lookup_nil(VALUE self, VALUE hash, VALUE key) {
}
#endif

#ifdef HAVE_RB_HASH_LOOKUP2
VALUE hash_spec_rb_hash_lookup2(VALUE self, VALUE hash, VALUE key, VALUE def) {
return rb_hash_lookup2(hash, key, def);
}
#endif

#ifdef HAVE_RB_HASH_NEW
VALUE hash_spec_rb_hash_new(VALUE self) {
return rb_hash_new();
Expand Down Expand Up @@ -135,6 +141,10 @@ void Init_hash_spec() {
rb_define_method(cls, "rb_hash_lookup", hash_spec_rb_hash_lookup, 2);
#endif

#ifdef HAVE_RB_HASH_LOOKUP2
rb_define_method(cls, "rb_hash_lookup2", hash_spec_rb_hash_lookup2, 3);
#endif

#ifdef HAVE_RB_HASH_NEW
rb_define_method(cls, "rb_hash_new", hash_spec_rb_hash_new, 0);
#endif
Expand Down
1 change: 1 addition & 0 deletions spec/ruby/optional/capi/ext/rubyspec.h
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@
#define HAVE_RB_HASH_DELETE_IF 1
#define HAVE_RB_HASH_FOREACH 1
#define HAVE_RB_HASH_LOOKUP 1
#define HAVE_RB_HASH_LOOKUP2 1
#define HAVE_RB_HASH_NEW 1
#define HAVE_RB_HASH_SIZE 1

Expand Down
14 changes: 14 additions & 0 deletions spec/ruby/optional/capi/hash_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,5 +153,19 @@
@s.rb_hash_lookup_nil(hsh, :chunky).should be_true
end
end

describe "rb_hash_lookup2" do
it "returns the value associated with the key" do
hash = {:chunky => 'bacon'}

@s.rb_hash_lookup2(hash, :chunky, nil).should == 'bacon'
end

it "returns the default value if the key does not exist" do
hash = {}

@s.rb_hash_lookup2(hash, :chunky, 10).should == 10
end
end
end
end
9 changes: 7 additions & 2 deletions vm/capi/hash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,16 @@ extern "C" {
}

VALUE rb_hash_lookup(VALUE self, VALUE key) {
VALUE entry = capi_fast_call(self, rb_intern("find_item"), 1, key);
return rb_hash_lookup2(self, key, Qnil);
}

VALUE rb_hash_lookup2(VALUE hash, VALUE key, VALUE def) {
VALUE entry = capi_fast_call(hash, rb_intern("find_item"), 1, key);

if(entry != Qnil) {
return capi_fast_call(entry, rb_intern("value"), 0);
} else {
return Qnil;
return def;
}
}

Expand Down
2 changes: 2 additions & 0 deletions vm/include/capi/ruby/ruby.h
Original file line number Diff line number Diff line change
Expand Up @@ -1434,6 +1434,8 @@ struct RTypedData {
/** Return the value associated with the key, excluding default values. */
VALUE rb_hash_lookup(VALUE self, VALUE key);

VALUE rb_hash_lookup2(VALUE hash, VALUE key, VALUE def);

/** Set the value associated with the key. */
VALUE rb_hash_aset(VALUE self, VALUE key, VALUE value);

Expand Down

0 comments on commit 3871c2b

Please sign in to comment.