Project

General

Profile

Bug #12671

Hash#to_proc result is not a lambda, but enforces arity

Added by headius (Charles Nutter) over 3 years ago. Updated 18 days ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Target version:
-
ruby -v:
ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-darwin14]
[ruby-core:76846]

Description

 $ ruby23 -e 'pr = {foo:1}.to_proc; puts pr.lambda?; pr.call rescue puts $!; pr.call(1, 2) rescue puts $!'
false
wrong number of arguments (given 0, expected 1)
wrong number of arguments (given 2, expected 1)

I believe it should be marked as a lambda, since it enforces arity.

Updated by nobu (Nobuyoshi Nakada) over 3 years ago

None-lambda doesn't mean that it never checks its arguments.
And if it's a lambda, it doesn't match the arity value.

As for the implementation detail, there is no room for arity in ifuncs.

Updated by headius (Charles Nutter) over 3 years ago

JRuby implements Hash#to_proc as:

class Hash
  def to_proc
    method(:[]).to_proc
  end
end

This allows us to present the proc as a lambda with correct arity:

$ jruby -e "pr = {}.to_proc; puts pr.arity; puts pr.lambda?"
1
true

It works for MRI too:

$ ruby23 -e "class Hash; def to_proc; method(:[]).to_proc; end; end; pr = {}.to_proc; puts pr.arity; puts pr.lambda?"
1
true

I think this is more representative of this proc's behavior. Can MRI do it this way?

Updated by mame (Yusuke Endoh) 18 days ago

This ticket was briefly discussed at today's dev meeting, and matz said that the result of Hash#to_proc should be a lambda.

#4

Updated by mame (Yusuke Endoh) 18 days ago

  • Status changed from Open to Closed

Applied in changeset git|d514ba8e17106c6d159c3902ac5456d6269731f8.


Proc made by Hash#to_proc should be a lambda [Bug #12671]

Like Symbol#to_proc (f0b815dc670b61eba1daaa67a8613ac431d32b16)

Also available in: Atom PDF