From bec77ef49adfa3192c53f681236b189f02068f8f Mon Sep 17 00:00:00 2001 From: usa Date: Sat, 10 Jun 2006 06:15:02 +0000 Subject: * eval.c (rb_f_method_name, rb_f_callee_name): new functions. new global method `__method__' and `__callee__'. git-svn-id: http://svn.ruby-lang.org/repos/ruby/trunk@10240 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- eval.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) (limited to 'eval.c') diff --git a/eval.c b/eval.c index e3a240eda..e8d4f7d14 100644 --- a/eval.c +++ b/eval.c @@ -7691,6 +7691,54 @@ rb_exec_end_proc(void) ruby_safe_level = safe; } +/* + * call-seq: + * __method__ => string + * + * Returns the name of the current method as a Symbol. + * If called from inside of an aliased method it will return the original + * nonaliased name. + * If called outside of a method, it returns nil. + * See also \_\_callee__. + * + */ + +static VALUE +rb_f_method_name(void) +{ + struct FRAME* prev = ruby_frame->prev; + if (prev && prev->this_func) { + return ID2SYM(prev->this_func); + } + else { + return Qnil; + } +} + +/* + * call-seq: + * __callee__ => string + * + * Returns the name of the current method as Symbol. + * If called from inside of an aliased method it will return the aliased + * name. + * If called outside of a method, it returns nil. + * See also \_\_method__. + * + */ + +static VALUE +rb_f_callee_name(void) +{ + struct FRAME* prev = ruby_frame->prev; + if (prev && prev->callee) { + return ID2SYM(prev->callee); + } + else { + return Qnil; + } +} + void Init_eval(void) { @@ -7745,6 +7793,9 @@ Init_eval(void) rb_define_global_function("global_variables", rb_f_global_variables, 0); /* in variable.c */ rb_define_global_function("local_variables", rb_f_local_variables, 0); + rb_define_global_function("__method__", rb_f_method_name, 0); + rb_define_global_function("__callee__", rb_f_callee_name, 0); + rb_define_method(rb_cBasicObject, "send", rb_f_send, -1); rb_define_method(rb_cBasicObject, "__send__", rb_f_send, -1); rb_define_method(rb_cBasicObject, "funcall", rb_f_funcall, -1); -- cgit