Project

General

Profile

Feature #14079

Updated by nate00 (Nate Sullivan) over 6 years ago

I would find it useful to check whether a list of arguments matches will cause an `ArgumentError` when passed to a method signature, method, but without calling the that method. 

 I'd like to check the arguments list using a Maybe this method called, for example, would be called `respond_to_arguments?`. Here's an example: example, where I check whether I can pass various argument lists to `String#prepend`: 

 ~~~ruby 
 class Foobar 
   def self.baz(str) 
   end 
 end str = "hello" 

 # Foobar.baz String#prepend accepts 1 argument, not 0 or 2: 
 Foobar.respond_to_arguments?(:baz, "one", "two")    str.respond_to_arguments?(:prepend, "foo", "bar")     # => false 
 Foobar.respond_to_arguments?(:baz, "one")           str.respond_to_arguments?(:prepend, "foo")            # => true 
 Foobar.respond_to_arguments?(:baz)                  str.respond_to_arguments?(:prepend)                   # => false 

 # Indeed, we get an ArgumentError if we pass 0 or 2 arguments: 
 Foobar.baz("one", "two") str.prepend("foo", "bar")     # raises ArgumentError 
 Foobar.baz("one") str.prepend("foo")            # success! 
 Foobar.baz str.prepend                   # raises ArgumentError 
 ~~~ 

 My use case is a background job processing system. It works like this: I call `MyWorker.perform_async` with some arguments; the arguments are serialized and put into a queue; and then a background worker takes those arguments from the queue, deserializes them and passes them to `MyWorker.perform`. If I passed invalid arguments, I don't know they were invalid until the background worker tries to call `perform`. But I'd like to know immediately when I call `perform_async`. 

 Perhaps a `respond_to_arguments_missing?` method would be required also. 

 Maybe `respond_to_arguments?` is a bad name. You could reasonably assume that it takes the same optional second parameter as `respond_to?` (i.e., `include_all`), but my proposal doesn't support an optional second parameter. 

 Thank you for your consideration!

Back