From d1a664a464639645acde4b49e98c2886309e4603 Mon Sep 17 00:00:00 2001 From: usa Date: Sat, 10 Jun 2006 06:15:02 +0000 Subject: [PATCH] * eval.c (rb_f_method_name, rb_f_callee_name): new functions. new global method `__method__' and `__callee__'. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@10240 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 5 +++++ eval.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/ChangeLog b/ChangeLog index e71b8ac635..c02ce0fe2e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Sat Jun 10 15:12:29 2006 NAKAMURA Usaku + + * eval.c (rb_f_method_name, rb_f_callee_name): new functions. + new global method `__method__' and `__callee__'. + Sat Jun 10 10:13:13 2006 NAKAMURA Usaku * lib/getoptlong.rb (GetoptLong#set_options): recieve arguments diff --git a/eval.c b/eval.c index e3a240eda1..e8d4f7d14c 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);