From: Ned Konz Date: 2001-09-21T14:07:55+09:00 Subject: [ruby-talk:21478] proposal: add Method reflection for source line, filename I've been using the rbbr (Ruby browser), and have enjoyed it. However, coming from a Smalltalk background, I miss seeing the source code (when it exists). So I added these two methods to eval.c to provide for source file name and lien number: =================================================================== RCS file: /src/ruby/eval.c,v retrieving revision 1.206 diff -c -r1.206 eval.c *** eval.c 2001/09/20 12:51:58 1.206 --- eval.c 2001/09/21 04:59:51 *************** *** 6913,6918 **** --- 6913,6942 ---- return str; } + /* NK: added */ + + static VALUE + method_source_file_name(VALUE method) + { + struct METHOD *data; + const char *filename; + + Data_Get_Struct(method, struct METHOD, data); + filename = data->body->nd_file; + return rb_str_buf_new2( filename == NULL ? "" : filename ); + } + + static VALUE + method_source_line(VALUE method) + { + struct METHOD *data; + + Data_Get_Struct(method, struct METHOD, data); + return INT2FIX( nd_line(data->body) ); + } + + /* NK: end */ + static VALUE mproc() { *************** *** 7027,7032 **** --- 7051,7058 ---- rb_define_method(rb_cMethod, "arity", method_arity, 0); rb_define_method(rb_cMethod, "inspect", method_inspect, 0); rb_define_method(rb_cMethod, "to_s", method_inspect, 0); + rb_define_method(rb_cMethod, "source_file_name", method_source_file_name, 0); /* NK */ + rb_define_method(rb_cMethod, "source_line", method_source_line, 0); /* NK */ rb_define_method(rb_cMethod, "to_proc", method_proc, 0); rb_define_method(rb_cMethod, "unbind", method_unbind, 0); rb_define_method(rb_mKernel, "method", rb_obj_method, 1); ============= I have no idea whether this is a good thing to add, but it certainly makes it easier for a Ruby browser to work. This could have been a single method that returned an Array, as well, I suppose. What do you think? Is this something worth adding to Ruby? This is my first Ruby C extension; please tell me if I did something wrong. BTW, the Ruby source is SO much nicer than Perl's! Thanks, -- Ned Konz currently: Stanwood, WA email: ned@bike-nomad.com homepage: http://bike-nomad.com