Issue #11214 has been reported by Richard Schneeman.

----------------------------------------
Bug #11214: Cannot Get Correct Binding from inside of C Method
https://bugs.ruby-lang.org/issues/11214

* Author: Richard Schneeman
* Status: Open
* Priority: Normal
* Assignee: 
* ruby -v: 2.2.2
* Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN
----------------------------------------
I am trying to get the arguments passed into a method using the binding. This is possible using pure Ruby:


```ruby
class RubyBindingClass
  def foo(arg = nil)
    return binding
  end
end

binding = RubyBindingClass.new.foo
puts binding.eval("self")
# => #<RubyBindingClass:0x007fdc5224f7f8>

puts binding.local_variables.inspect
# => [arg]
```

You can see that the `#<RubyBindingClass:0x007fdc5224f7f8>` is returned as self and the `local_variables` correctly reports that `arg` is in scope. When we access the binding from a C method, we do not get the same information

```ruby
# Thanks to Frederick Cheung for the code snippet
require 'inline' # $ gem install RubyInline

random_main_variable = 2

class CBindingClass
  inline do |builder|
    builder.include "<time.h>"
    builder.c_raw <<-SRC, :arity => 1
      VALUE foo(VALUE self, VALUE arg){
        VALUE ret = rb_funcall(self, rb_intern("binding"), 0);
        return ret;
      }
    SRC
  end
end

binding = CBindingClass.new.foo(10)

puts binding.eval("self")
# => main

puts binding.local_variables.inspect
# => [:binding, :random_main_variable]
```

Here you can see that self is reported as `main` and that `local_variables` is returning variables from the main scope instead of from within the C method. 

I originally stumbled upon this while trying to get access to arguments via TracePoint: http://stackoverflow.com/questions/30584454/get-method-arguments-using-rubys-tracepoint. Is this binding behavior intentional? Should it be possible to get programatic access to arguments passed into a C defined method? 




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