Commit 48ac1f74 authored by Yukihiro "Matz" Matsumoto's avatar Yukihiro "Matz" Matsumoto

Merge pull request #2218 from take-cheeze/move_method

Move `__method__` to mruby-kernel-ext since it's not ISO method.
parents 64b30b65 d0c0beb8
#include "mruby.h"
#include "mruby/error.h"
/*
* call-seq:
* __method__ -> symbol
*
* Returns the name at the definition of the current method as a
* Symbol.
* If called outside of a method, it returns <code>nil</code>.
*
*/
static mrb_value
mrb_f_method(mrb_state *mrb, mrb_value self)
{
mrb_callinfo *ci = mrb->c->ci;
ci--;
if (ci->mid)
return mrb_symbol_value(ci->mid);
else
return mrb_nil_value();
}
void
mrb_mruby_kernel_ext_gem_init(mrb_state *mrb)
{
struct RClass *krn = mrb->kernel_module;
mrb_define_module_function(mrb, krn, "fail", mrb_f_raise, MRB_ARGS_NONE());
mrb_define_method(mrb, krn, "__method__", mrb_f_method, MRB_ARGS_NONE());
}
void
......
......@@ -2,3 +2,17 @@ assert('Kernel.fail, Kernel#fail') do
assert_raise(RuntimeError) { fail }
assert_raise(RuntimeError) { Kernel.fail }
end
assert('Kernel#__method__') do
assert_equal(:m, Class.new {def m; __method__; end}.new.m)
assert_equal(:m, Class.new {define_method(:m) {__method__}}.new.m)
c = Class.new do
[:m1, :m2].each do |m|
define_method(m) do
__method__
end
end
end
assert_equal(:m1, c.new.m1)
assert_equal(:m2, c.new.m2)
end
......@@ -210,26 +210,6 @@ mrb_f_block_given_p_m(mrb_state *mrb, mrb_value self)
return mrb_bool_value(given_p);
}
/*
* call-seq:
* __method__ -> symbol
*
* Returns the name at the definition of the current method as a
* Symbol.
* If called outside of a method, it returns <code>nil</code>.
*
*/
static mrb_value
mrb_f_method(mrb_state *mrb, mrb_value self)
{
mrb_callinfo *ci = mrb->c->ci;
ci--;
if (ci->mid)
return mrb_symbol_value(ci->mid);
else
return mrb_nil_value();
}
/* 15.3.1.3.7 */
/*
* call-seq:
......@@ -1153,7 +1133,6 @@ mrb_init_kernel(mrb_state *mrb)
mrb_define_method(mrb, krn, "===", mrb_equal_m, MRB_ARGS_REQ(1)); /* 15.3.1.3.2 */
mrb_define_method(mrb, krn, "__id__", mrb_obj_id_m, MRB_ARGS_NONE()); /* 15.3.1.3.3 */
mrb_define_method(mrb, krn, "__send__", mrb_f_send, MRB_ARGS_ANY()); /* 15.3.1.3.4 */
mrb_define_method(mrb, krn, "__method__", mrb_f_method, MRB_ARGS_NONE());
mrb_define_method(mrb, krn, "block_given?", mrb_f_block_given_p_m, MRB_ARGS_NONE()); /* 15.3.1.3.6 */
mrb_define_method(mrb, krn, "class", mrb_obj_class_m, MRB_ARGS_NONE()); /* 15.3.1.3.7 */
mrb_define_method(mrb, krn, "clone", mrb_obj_clone, MRB_ARGS_NONE()); /* 15.3.1.3.8 */
......
......@@ -528,20 +528,6 @@ assert('Kernel#global_variables') do
end
end
assert('Kernel#__method__') do
assert_equal(:m, Class.new {def m; __method__; end}.new.m)
assert_equal(:m, Class.new {define_method(:m) {__method__}}.new.m)
c = Class.new do
[:m1, :m2].each do |m|
define_method(m) do
__method__
end
end
end
assert_equal(:m1, c.new.m1)
assert_equal(:m2, c.new.m2)
end
assert('Kernel#define_singleton_method') do
o = Object.new
ret = o.define_singleton_method(:test_method) do
......
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