Skip to content

Commit

Permalink
Add a C-API "rb_hash_clear"
Browse files Browse the repository at this point in the history
  • Loading branch information
kachick committed Jul 30, 2015
1 parent f8daad8 commit f68f9a0
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 0 deletions.
10 changes: 10 additions & 0 deletions spec/ruby/optional/capi/ext/hash_spec.c
Expand Up @@ -40,6 +40,12 @@ VALUE hash_spec_rb_hash_aset(VALUE self, VALUE hash, VALUE key, VALUE val) {
}
#endif

#ifdef HAVE_RB_HASH_CLEAR
VALUE hash_spec_rb_hash_clear(VALUE self, VALUE hash) {
return rb_hash_clear(hash);
}
#endif

#ifdef HAVE_RB_HASH_DELETE
VALUE hash_spec_rb_hash_delete(VALUE self, VALUE hash, VALUE key) {
return rb_hash_delete(hash, key);
Expand Down Expand Up @@ -148,6 +154,10 @@ void Init_hash_spec() {
rb_define_method(cls, "rb_hash_aset", hash_spec_rb_hash_aset, 3);
#endif

#ifdef HAVE_RB_HASH_CLEAR
rb_define_method(cls, "rb_hash_clear", hash_spec_rb_hash_clear, 1);
#endif

#ifdef HAVE_RB_HASH_DELETE
rb_define_method(cls, "rb_hash_delete", hash_spec_rb_hash_delete, 2);
#endif
Expand Down
1 change: 1 addition & 0 deletions spec/ruby/optional/capi/ext/rubyspec.h
Expand Up @@ -322,6 +322,7 @@
#define HAVE_RB_HASH_FREEZE 1
#define HAVE_RB_HASH_AREF 1
#define HAVE_RB_HASH_ASET 1
#define HAVE_RB_HASH_CLEAR 1
#define HAVE_RB_HASH_DELETE 1
#define HAVE_RB_HASH_DELETE_IF 1
#define HAVE_RB_HASH_FOREACH 1
Expand Down
8 changes: 8 additions & 0 deletions spec/ruby/optional/capi/hash_spec.rb
Expand Up @@ -91,6 +91,14 @@
end
end

describe "rb_hash_clear" do
it "returns self that cleared keys and values" do
hsh = { :key => 'value' }
@s.rb_hash_clear(hsh).should equal(hsh)
hsh.should == {}
end
end

describe "rb_hash_delete" do
it "removes the key and returns the value" do
hsh = {:chunky => 'bacon'}
Expand Down
4 changes: 4 additions & 0 deletions vm/capi/hash.cpp
Expand Up @@ -26,6 +26,10 @@ extern "C" {
return capi_fast_call(self, rb_intern("[]="), 2, key, value);
}

VALUE rb_hash_clear(VALUE self) {
return capi_fast_call(self, rb_intern("clear"), 0);
}

VALUE rb_hash_delete(VALUE self, VALUE key) {
return capi_fast_call(self, rb_intern("delete"), 1, key);
}
Expand Down
3 changes: 3 additions & 0 deletions vm/include/capi/ruby/ruby.h
Expand Up @@ -1453,6 +1453,9 @@ struct RTypedData {
/** Set the value associated with the key. */
VALUE rb_hash_aset(VALUE self, VALUE key, VALUE value);

/** Clear the Hash object */
VALUE rb_hash_clear(VALUE self);

/** Remove the key and return the associated value. */
VALUE rb_hash_delete(VALUE self, VALUE key);

Expand Down

0 comments on commit f68f9a0

Please sign in to comment.