Is there a way to "enable" forced Proc creation created using Proc.new or Kernel.proc so that it behaves like Proc created using lambda ?
My initialize method takes a &action block and assigns it to an instance variable. I want action strictly abide by arity, so when I apply arguments to it later, it raises an ArgumentError , which I can save and make a more meaningful exception. Primarily:
class Command attr_reader :name, :action def initialize(name, &action) @name = name @action = action end def perform(*args) begin action.call(*args) rescue ArgumentError raise(WrongArity.new(args.size)) end end end class WrongArity < StandardError; end
Unfortunately, action does not use arity by default:
c = Command.new('second_argument') { |_, y| y } c.perform(1)
action.to_proc does not work, but lambda(&action) .
Any other ideas? Or are there better approaches to the problem?
Thanks!
source share