Issue #14230 has been updated by matz (Yukihiro Matsumoto).


Agreed.

Matz.


----------------------------------------
Feature #14230: Binding#source_location
https://bugs.ruby-lang.org/issues/14230#change-68969

* Author: mame (Yusuke Endoh)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
How about providing `Binding#source_location`?

    b = binding
    p binding.source_location #=> ["test.rb", 1]

To fetch the information, people uses a dirty idiom: `eval("[__FILE__, __LINE__]", binding)`.

* https://github.com/ruby/ruby/blob/trunk/lib/irb/workspace.rb#L111
* https://github.com/rspec-given/rspec-given/blob/19e9ef24c159f5a014a9f22ebb94161d6ac5af43/lib/given/module_methods.rb#L35
* https://github.com/quix/live_ast/blob/7adaabdbb99dbc649fef4dfd8a821d9f347cc9f2/lib/live_ast/common.rb#L37
* https://github.com/wesabe/robot-army/blob/29c50d3a5f2258c917fef821103fb0f107edb428/lib/robot-army/eval_builder.rb#L14

I noticed this issue when changing the semantics of eval so that it does not inherit `__FILE__` from the binding. [#4352]

```diff
diff --git a/lib/irb/workspace.rb b/lib/irb/workspace.rb
index 9ce84b60fa..5216520f32 100644
--- a/lib/irb/workspace.rb
+++ b/lib/irb/workspace.rb
@@ -108,7 +108,7 @@ def filter_backtrace(bt)
     end
 
     def code_around_binding
-      file, pos = @binding.eval('[__FILE__, __LINE__]')
+      file, pos = @binding.source_location
 
       unless defined?(::SCRIPT_LINES__[file]) && lines = ::SCRIPT_LINES__[file]
         begin
diff --git a/proc.c b/proc.c
index 9dbc78ae4a..23c4d3a2d4 100644
--- a/proc.c
+++ b/proc.c
@@ -12,6 +12,7 @@
 #include "eval_intern.h"
 #include "internal.h"
 #include "gc.h"
+#include "vm_core.h"
 #include "iseq.h"
 
 /* Proc.new with no block will raise an exception in the future
@@ -611,6 +612,24 @@ bind_receiver(VALUE bindval)
     return vm_block_self(&bind->block);
 }
 
+/*
+ *  call-seq:
+ *     binding.source_location  -> [String, Integer]
+ *
+ *  Returns the Ruby source filename and line number of the binding object.
+ */
+static VALUE
+bind_location(VALUE bindval)
+{
+    VALUE loc[2];
+    const rb_binding_t *bind;
+    GetBindingPtr(bindval, bind);
+    loc[0] = pathobj_path(bind->pathobj);
+    loc[1] = INT2FIX(bind->first_lineno);
+
+    return rb_ary_new4(2, loc);
+}
+
 static VALUE
 cfunc_proc_new(VALUE klass, VALUE ifunc, int8_t is_lambda)
 {
@@ -3223,5 +3242,6 @@ Init_Binding(void)
     rb_define_method(rb_cBinding, "local_variable_set", bind_local_variable_set, 2);
     rb_define_method(rb_cBinding, "local_variable_defined?", bind_local_variable_defined_p, 1);
     rb_define_method(rb_cBinding, "receiver", bind_receiver, 0);
+    rb_define_method(rb_cBinding, "source_location", bind_location, 0);
     rb_define_global_function("binding", rb_f_binding, 0);
 }
```



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>