Should clarify the role of `mruby-kernel-ext` and `mruby-object-ext`; close #4449

The former should contain function like methods, and the latter should
contain methods shared by all objects.
parent 1cdb3ec9
module Kernel
# call-seq:
# obj.yield_self {|_obj|...} -> an_object
# obj.then {|_obj|...} -> an_object
#
# Yields <i>obj</i> and returns the result.
#
# 'my string'.yield_self {|s|s.upcase} #=> "MY STRING"
#
def yield_self(&block)
return to_enum :yield_self unless block
block.call(self)
end
alias then yield_self
end
......@@ -206,22 +206,6 @@ mrb_f_hash(mrb_state *mrb, mrb_value self)
return mrb_ensure_hash_type(mrb, arg);
}
/*
* call-seq:
* obj.itself -> an_object
*
* Returns <i>obj</i>.
*
* string = 'my string' #=> "my string"
* string.itself.object_id == string.object_id #=> true
*
*/
static mrb_value
mrb_f_itself(mrb_state *mrb, mrb_value self)
{
return self;
}
void
mrb_mruby_kernel_ext_gem_init(mrb_state *mrb)
{
......@@ -237,7 +221,6 @@ mrb_mruby_kernel_ext_gem_init(mrb_state *mrb)
mrb_define_module_function(mrb, krn, "String", mrb_f_string, MRB_ARGS_REQ(1));
mrb_define_module_function(mrb, krn, "Array", mrb_f_array, MRB_ARGS_REQ(1));
mrb_define_module_function(mrb, krn, "Hash", mrb_f_hash, MRB_ARGS_REQ(1));
mrb_define_module_function(mrb, krn, "itself", mrb_f_itself, MRB_ARGS_NONE());
}
void
......
class Object
module Kernel
# call-seq:
# obj.yield_self {|_obj|...} -> an_object
# obj.then {|_obj|...} -> an_object
#
# Yields <i>obj</i> and returns the result.
#
# 'my string'.yield_self {|s|s.upcase} #=> "MY STRING"
#
def yield_self(&block)
return to_enum :yield_self unless block
block.call(self)
end
alias then yield_self
##
# call-seq:
# obj.tap{|x|...} -> obj
......
......@@ -44,6 +44,22 @@ nil_to_i(mrb_state *mrb, mrb_value obj)
return mrb_fixnum_value(0);
}
/*
* call-seq:
* obj.itself -> an_object
*
* Returns <i>obj</i>.
*
* string = 'my string' #=> "my string"
* string.itself.object_id == string.object_id #=> true
*
*/
static mrb_value
mrb_f_itself(mrb_state *mrb, mrb_value self)
{
return self;
}
/*
* call-seq:
* obj.instance_exec(arg...) {|var...| block } -> obj
......@@ -102,6 +118,7 @@ mrb_mruby_object_ext_gem_init(mrb_state* mrb)
mrb_define_method(mrb, n, "to_f", nil_to_f, MRB_ARGS_NONE());
#endif
mrb_define_method(mrb, n, "to_i", nil_to_i, MRB_ARGS_NONE());
mrb_define_module_function(mrb, mrb->kernel_module, "itself", mrb_f_itself, MRB_ARGS_NONE());
mrb_define_method(mrb, mrb->kernel_module, "instance_exec", mrb_obj_instance_exec, MRB_ARGS_ANY() | MRB_ARGS_BLOCK());
}
......
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