Class: Proc
Overview
Re-open the Proc class to define #to_lambda
Constant Summary collapse
- WRONG_NUMBER_ARGUMENTS_MESSAGE =
"Wrong number of arguments (given %<args>s, expected %<arity>s)".freeze
Instance Method Summary collapse
- #receiver ⇒ Object
-
#to_lambda ⇒ Object
rubocop: disable Metrics/MethodLength rubocop: disable Style/Lambda.
Instance Method Details
#receiver ⇒ Object
42 43 44 |
# File 'lib/runtime/command.rb', line 42 def receiver binding.eval('self') end |
#to_lambda ⇒ Object
rubocop: disable Metrics/MethodLength rubocop: disable Style/Lambda
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/runtime/events.rb', line 47 def to_lambda return self if lambda? # Save local reference to self so we can use it in the following lambda scope source_proc = self ->(*args, **kwargs, &block) do # Enforce strict arity like a lambda source_arity = source_proc.arity if source_arity >= 0 && args.length != source_arity raise ArgumentError, format( WRONG_NUMBER_ARGUMENTS_MESSAGE, args: args.length, arity: source_arity) elsif args.length < (min = -source_arity - 1) raise ArgumentError, format( WRONG_NUMBER_ARGUMENTS_MESSAGE, args: args.length, arity: "#{min}+") end # Preserve original binding/locals/self source_proc.call(*args, **kwargs, &block) end end |