Bug #16640
closedSymbol#to_proc reports -1 for arity
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 kddnewton (Kevin Newton) over 5 years ago
          Updated by kddnewton (Kevin Newton) over 5 years 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 kddnewton (Kevin Newton) over 5 years ago
          Updated by kddnewton (Kevin Newton) over 5 years 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.
        
           Updated by kddnewton (Kevin Newton) over 5 years ago
          Updated by kddnewton (Kevin Newton) over 5 years ago
          
          
        
        
      
      - Subject changed from Symbol#to_proc has incorrect arity to Symbol#to_proc reports -1 for arity
        
           Updated by zverok (Victor Shepelev) over 5 years ago
          Updated by zverok (Victor Shepelev) over 5 years ago
          
          
        
        
      
      
    
        
           Updated by kddnewton (Kevin Newton) over 5 years ago
          Updated by kddnewton (Kevin Newton) over 5 years ago
          
          
        
        
      
      Oh nice I didn't see that @zverok (Victor Shepelev).
        
           Updated by kddnewton (Kevin Newton) over 5 years ago
          Updated by kddnewton (Kevin Newton) over 5 years ago
          
          
        
        
      
      Actually this can be closed, as it doesn't make sense for arity > 1.
        
           Updated by nobu (Nobuyoshi Nakada) over 5 years ago
          Updated by nobu (Nobuyoshi Nakada) over 5 years ago
          
          
        
        
      
      - Description updated (diff)
- Status changed from Open to Rejected
        
           Updated by Eregon (Benoit Daloze) over 5 years ago
          Updated by Eregon (Benoit Daloze) over 5 years ago
          
          
        
        
      
      The arity should actually be -2 which means 1 required arguments + any number of optional arguments.
        
           Updated by nobu (Nobuyoshi Nakada) over 5 years ago
          Updated by nobu (Nobuyoshi Nakada) over 5 years ago
          
          
        
        
      
      - Status changed from Rejected to Open
        
           Updated by nobu (Nobuyoshi Nakada) over 5 years ago
          Updated by nobu (Nobuyoshi Nakada) over 5 years 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
        
           Updated by Eregon (Benoit Daloze) over 5 years ago
          Updated by Eregon (Benoit Daloze) over 5 years ago
          
          
        
        
      
      Thanks for the fix nobu! (https://github.com/ruby/ruby/commit/5b29ea0845c14092abd866ce0183c52635bade4c)