Skip to content

Commit f68f9a0

Browse files
committedJul 30, 2015
Add a C-API "rb_hash_clear"
1 parent f8daad8 commit f68f9a0

File tree

5 files changed

+26
-0
lines changed

5 files changed

+26
-0
lines changed
 

‎spec/ruby/optional/capi/ext/hash_spec.c

+10
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ VALUE hash_spec_rb_hash_aset(VALUE self, VALUE hash, VALUE key, VALUE val) {
4040
}
4141
#endif
4242

43+
#ifdef HAVE_RB_HASH_CLEAR
44+
VALUE hash_spec_rb_hash_clear(VALUE self, VALUE hash) {
45+
return rb_hash_clear(hash);
46+
}
47+
#endif
48+
4349
#ifdef HAVE_RB_HASH_DELETE
4450
VALUE hash_spec_rb_hash_delete(VALUE self, VALUE hash, VALUE key) {
4551
return rb_hash_delete(hash, key);
@@ -148,6 +154,10 @@ void Init_hash_spec() {
148154
rb_define_method(cls, "rb_hash_aset", hash_spec_rb_hash_aset, 3);
149155
#endif
150156

157+
#ifdef HAVE_RB_HASH_CLEAR
158+
rb_define_method(cls, "rb_hash_clear", hash_spec_rb_hash_clear, 1);
159+
#endif
160+
151161
#ifdef HAVE_RB_HASH_DELETE
152162
rb_define_method(cls, "rb_hash_delete", hash_spec_rb_hash_delete, 2);
153163
#endif

‎spec/ruby/optional/capi/ext/rubyspec.h

+1
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@
322322
#define HAVE_RB_HASH_FREEZE 1
323323
#define HAVE_RB_HASH_AREF 1
324324
#define HAVE_RB_HASH_ASET 1
325+
#define HAVE_RB_HASH_CLEAR 1
325326
#define HAVE_RB_HASH_DELETE 1
326327
#define HAVE_RB_HASH_DELETE_IF 1
327328
#define HAVE_RB_HASH_FOREACH 1

‎spec/ruby/optional/capi/hash_spec.rb

+8
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,14 @@
9191
end
9292
end
9393

94+
describe "rb_hash_clear" do
95+
it "returns self that cleared keys and values" do
96+
hsh = { :key => 'value' }
97+
@s.rb_hash_clear(hsh).should equal(hsh)
98+
hsh.should == {}
99+
end
100+
end
101+
94102
describe "rb_hash_delete" do
95103
it "removes the key and returns the value" do
96104
hsh = {:chunky => 'bacon'}

‎vm/capi/hash.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ extern "C" {
2626
return capi_fast_call(self, rb_intern("[]="), 2, key, value);
2727
}
2828

29+
VALUE rb_hash_clear(VALUE self) {
30+
return capi_fast_call(self, rb_intern("clear"), 0);
31+
}
32+
2933
VALUE rb_hash_delete(VALUE self, VALUE key) {
3034
return capi_fast_call(self, rb_intern("delete"), 1, key);
3135
}

‎vm/include/capi/ruby/ruby.h

+3
Original file line numberDiff line numberDiff line change
@@ -1453,6 +1453,9 @@ struct RTypedData {
14531453
/** Set the value associated with the key. */
14541454
VALUE rb_hash_aset(VALUE self, VALUE key, VALUE value);
14551455

1456+
/** Clear the Hash object */
1457+
VALUE rb_hash_clear(VALUE self);
1458+
14561459
/** Remove the key and return the associated value. */
14571460
VALUE rb_hash_delete(VALUE self, VALUE key);
14581461

0 commit comments

Comments
 (0)
Please sign in to comment.