Project

General

Profile

Actions

Feature #19484

open

Calling `binding` on a C-level proc raises an `ArgumentError`

Added by joel@drapper.me (Joel Drapper) almost 2 years ago. Updated almost 2 years ago.

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

Description

Calling binding on a C-level proc (from &:symbol) raises an ArgumentError, "Can't create Binding from C level Proc" but there is no way to tell if a given proc is a C-level proc before calling binding on it. It’s possible to rescue this error, but rescuing an exception is slow.

Given that a C-level proc doesn't have a binding, would it make more sense to respond to binding with nil rather than raise an error? That would allow us to, for example, look up the receiver and fall back to self, e.g. block.binding&.receiver || self.

Alternatively, it would be useful to be able to check whether a given proc is a C-Level proc using something like this.

case block
when CProc
  block.call(self)
else
  block.call
end
Actions #1

Updated by joel@drapper.me (Joel Drapper) almost 2 years ago

  • Description updated (diff)

Updated by Eregon (Benoit Daloze) almost 2 years ago

block.binding is not meant to be fast anyway. So is there a real performance problem caused by that?
nil would cause the error to happen later and probably be less clear for the user.

Note that "C-level proc" is not a good classification, similar restrictions apply to methods defined in any language but Ruby (e.g. Java for TruffleRuby).

Actions

Also available in: Atom PDF

Like0
Like0Like1