Project

General

Profile

Bug #16640

Symbol#to_proc reports -1 for arity

Added by kddeisz (Kevin Deisz) about 2 months ago. Updated about 2 months ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:97190]

Description

When you create a proc using Symbol#to_proc syntax, it reports the arity as -1, even though the required number of arguments is actually 1. This can happen based on lambda(&:hash), proc(&:hash), or some_method(&:hash). Opened a PR with a fix here: https://github.com/ruby/ruby/pull/2913.

Updated by kddeisz (Kevin Deisz) about 2 months ago

Ooo just found https://bugs.ruby-lang.org/issues/9520. I kind of get the argument to keep it consistent with other procs, but this is still a special case within block types, and it will fail if you call it without an argument.

Updated by kddeisz (Kevin Deisz) about 2 months ago

To add some context, this came up because of https://github.com/rails/rails/pull/38474 - specifically https://github.com/rails/rails/blob/2fa1f7c48151617e13b4ad6ca065e47c8d7b42d0/activesupport/lib/active_support/callbacks.rb#L460-L461. The idea being that it's checking the arity to determine what kind of callback to build, but because Symbol#to_proc says it's -1, it actually results in an error.

#3

Updated by kddeisz (Kevin Deisz) about 2 months ago

  • Subject changed from Symbol#to_proc has incorrect arity to Symbol#to_proc reports -1 for arity

Updated by zverok (Victor Shepelev) about 2 months ago

#9250 was closed with the reason

The proc created by Symbol#to_proc is not a lambda, but a proc

But in #16260, it was discussed that Symbol#to_proc behaves more like lambda, and so hopefully all of its introspection methods (#lambda?, #arity, #parameters) will be fixed.

Updated by kddeisz (Kevin Deisz) about 2 months ago

Oh nice I didn't see that @zverok.

Updated by kddeisz (Kevin Deisz) about 2 months ago

Actually this can be closed, as it doesn't make sense for arity > 1.

#7

Updated by nobu (Nobuyoshi Nakada) about 2 months ago

  • Status changed from Open to Rejected
  • Description updated (diff)

Updated by Eregon (Benoit Daloze) about 2 months ago

The arity should actually be -2 which means 1 required arguments + any number of optional arguments.

#9

Updated by nobu (Nobuyoshi Nakada) about 2 months ago

  • Status changed from Rejected to Open
#10

Updated by nobu (Nobuyoshi Nakada) about 2 months ago

  • Status changed from Open to Closed

Applied in changeset git|5b29ea0845c14092abd866ce0183c52635bade4c.


Proc from Symbol needs a receiver

So its arity should be -2 instead of -1.

[Bug #16640]
https://bugs.ruby-lang.org/issues/16640#change-84337

Also available in: Atom PDF