Project

General

Profile

Actions

Feature #19617

open

Add Method#binding and UnboundMethod#binding, similar to Proc#binding

Added by nevans (Nicholas Evans) over 1 year ago. Updated over 1 year ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:113342]

Description

When a method is defined dynamically with define_method, it would be useful to obtain access to the defining block's binding and the local variables it has captured, based on the defining block's binding. For methods defined using the def keyword, the binding's local variables might be empty or might be all of the names in the method's locals table, with all values set to nil.

For UnboundMethod, it is unclear (to me) what the appropriate receiver for the binding would be, so perhaps unbound.binding.receiver should raise an exception.

Alternatively (or additionally), something like Method#defining_proc and UnboundMethod#defining_proc might be added and return nil for def definitions and the proc for define_method definitions.

This would be a useful tool when debugging from the console. As another example, it might be used to scan a code base for dynamically generated regexps which are only reachable via the enclosed local variables and test that they are all linear time (see https://github.com/ruby/net-imap/blob/92db350b24c388d2a2104f36cac9caa49a1044df/test/net/imap/regexp_collector.rb).

Actions

Also available in: Atom PDF

Like0
Like1Like0Like0Like0Like0Like0Like0Like0