[ruby-core:83692] [Ruby trunk Feature#14079] Validate argument list without calling method

From: nathanielsullivan00@...
Date: 2017-11-07 03:20:33 UTC
List: ruby-core #83692
Issue #14079 has been updated by nate00 (Nate Sullivan).

Description updated

I've removed the `String#prepend` example from the description. Thanks for catching that, Benoit and Hans.

I agree with Benoit that an instance method on `Method`/`UnboundMethod` would be better than my `respond_to_arguments?` method. His proposal would work for instance methods, even if we haven't yet made an instance.

----------------------------------------
Feature #14079: Validate argument list without calling method
https://bugs.ruby-lang.org/issues/14079#change-67716

* Author: nate00 (Nate Sullivan)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
I would find it useful to check whether a list of arguments matches a method signature, but without calling the method.

I'd like to check the arguments list using a method called, for example, `respond_to_arguments?`. Here's an example:

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

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

# Indeed, we get an ArgumentError if we pass 0 or 2 arguments:
Foobar.baz("one", "two")   # raises ArgumentError
Foobar.baz("one")          # success!
Foobar.baz                 # 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!



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>

In This Thread

Prev Next