Project

General

Profile

Actions

Bug #2212

closed

Using a Lambda with Inappropriate Arity for Hash#default_proc=

Added by runpaint (Run Paint Run Run) over 14 years ago. Updated almost 13 years ago.

Status:
Closed
Target version:
-
ruby -v:
ruby 1.9.2dev (2009-10-14 trunk 25330) [i686-linux]
Backport:
[ruby-core:26087]

Description

=begin
If the default_proc of a Hash is a lambda whose arity != 2, key lookups raise an ArgumentError. To understand this exception the user must know that the hash has a default proc, lambdas perform strict argument checking, and that the lambda is passed two arguments.

Instead, perhaps Hash#default_proc and Hash.new could raise a helpful error message if they're passed a lambda with an arity other than 2. This localises the exception to where the default_proc is set, as opposed to where an element is referenced, and better explains the problem.

h={}
=> {}
h.default_proc=->{ true }
=> #<Proc:0x9eaaea8@(irb):12 (lambda)>
h[:key]
ArgumentError: wrong number of arguments (2 for 0)
from (irb):13:in yield' from (irb):13:in default'
from (irb):13
from /usr/local/bin/irb:12:in `'
=end

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0