Unverified Commit 3103da85 authored by Yukihiro "Matz" Matsumoto's avatar Yukihiro "Matz" Matsumoto Committed by GitHub

Merge pull request #4594 from shuujii/move-NilClass-to_h-to-mruby-object-ext-from-mruby-enum-ext

Move `NilClass#to_h` to `mruby-object-ext` from `mruby-enum-ext`
parents 381a6ddd 2d520eaf
......@@ -811,10 +811,6 @@ module Enumerable
h
end
def nil.to_h
{}
end
def uniq(&block)
hash = {}
if block
......
......@@ -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
#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());
......
......@@ -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
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment