Move `NilClass#to_h` to `mruby-object-ext` from `mruby-enum-ext`
MODIFY
mrbgems/mruby-enum-ext/mrblib/enum.rb
mrbgems/mruby-enum-ext/mrblib/enum.rb
@@ -811,10 +811,6 @@ module Enumerable
h
end
- def nil.to_h
- {}
- end
-
def uniq(&block)
hash = {}
if block

MODIFY
mrbgems/mruby-enum-ext/test/enum.rb
mrbgems/mruby-enum-ext/test/enum.rb
@@ -186,8 +186,5 @@ assert("Enumerable#to_h") do
h = c.new.to_h
assert_equal Hash, h.class
assert_equal h0, h
- # mruby-enum-ext also provides nil.to_h
- assert_equal Hash.new, nil.to_h
-
assert_equal({1=>4,3=>8}, c.new.to_h{|k,v|[k,v*2]})
end

MODIFY
mrbgems/mruby-object-ext/src/object.c
mrbgems/mruby-object-ext/src/object.c
#include <mruby.h>
#include <mruby/array.h>
#include <mruby/class.h>
+#include <mruby/hash.h>
#include <mruby/proc.h>
/*
@@ -31,6 +32,19 @@ nil_to_f(mrb_state *mrb, mrb_value obj)
}
#endif
+/*
+ * call-seq:
+ * nil.to_h -> {}
+ *
+ * Always returns an empty hash.
+ */
+
+static mrb_value
+nil_to_h(mrb_state *mrb, mrb_value obj)
+{
+ return mrb_hash_new(mrb);
+}
+
/*
* call-seq:
* nil.to_i -> 0
@@ -117,6 +131,7 @@ mrb_mruby_object_ext_gem_init(mrb_state* mrb)
#ifndef MRB_WITHOUT_FLOAT
mrb_define_method(mrb, n, "to_f", nil_to_f, MRB_ARGS_NONE());
#endif
+ mrb_define_method(mrb, n, "to_h", nil_to_h, MRB_ARGS_NONE());
mrb_define_method(mrb, n, "to_i", nil_to_i, MRB_ARGS_NONE());
mrb_define_method(mrb, mrb->kernel_module, "itself", mrb_f_itself, MRB_ARGS_NONE());

MODIFY
mrbgems/mruby-object-ext/test/nil.rb
mrbgems/mruby-object-ext/test/nil.rb
@@ -7,6 +7,10 @@ assert('NilClass#to_f') do
assert_equal 0.0, nil.to_f
end
+assert('NilClass#to_h') do
+ assert_equal Hash.new, nil.to_h
+end
+
assert('NilClass#to_i') do
assert_equal 0, nil.to_i
end

Move `NilClass#to_h` to `mruby-object-ext` from `mruby-enum-ext`