X-Git-Url: http://git.code-monkey.de/?a=blobdiff_plain;f=ext%2Fcomments.c;h=f7919dbdac1e20219a8a45dd70d79a1361aa5f95;hb=bf1f7624bee5a466b402825083190d3afbd9c28b;hp=a2569866a62766062a292d9719f0233fd75a1185;hpb=0ebbf4f00aa8b375b4a781b0a0397d0fce5466e1;p=ruby-vorbistagger.git diff --git a/ext/comments.c b/ext/comments.c index a256986..f7919db 100644 --- a/ext/comments.c +++ b/ext/comments.c @@ -17,6 +17,7 @@ */ #include +#include #include #include #include @@ -331,6 +332,45 @@ c_has_key (VALUE self, VALUE key) return Qfalse; } +static int +merge_cb (VALUE key, VALUE value, VALUE self) +{ + c_aset (self, key, value); + + return ST_CONTINUE; +} + +/* + * call-seq: + * object.merge!(hash) -> object + * + * Adds the key-value pairs from *hash* to *object*, overwriting existing + * values if a key already existed in *object*. + */ +static VALUE +c_merge (VALUE self, VALUE hash) +{ + Check_Type (hash, T_HASH); + + rb_hash_foreach (hash, merge_cb, self); + + return self; +} + +/* + * call-seq: + * object.shift(hash) -> array or nil + * + * Removes the first key-value pair from *object* and returns it + * as the two-item array [key, value]. + * If *object* is empty, +nil+ is returned. + */ +static VALUE +c_shift (VALUE self) +{ + return rb_ary_shift (rb_iv_get (self, "items")); +} + /* * call-seq: * object <=> other -> -1, 0 or 1 @@ -463,6 +503,8 @@ Init_Comments (VALUE mVorbis) rb_define_method (c, "empty?", c_get_empty, 0); rb_define_method (c, "keys", c_keys, 0); rb_define_method (c, "values", c_values, 0); + rb_define_method (c, "merge!", c_merge, 1); + rb_define_method (c, "shift", c_shift, 0); rb_include_module (c, rb_mComparable); rb_define_method (c, "<=>", c_compare, 1);