Amenable
Removes excessive parameters from function calls.
The rule: amenable drops only the arguments Ruby would reject with ArgumentError. Any call Ruby already accepts behaves exactly as it would without amenable.
require 'amenable'
class Dog
using Amenable
amenable def bark(n = 1)
([ :woof ] * n).join(" ")
end
end
Dog.new.bark
> "woof"
Dog.new.bark(2)
> "woof woof"
Dog.new.bark(2, 3, 4, foo: 5)
> "woof woof"Usage
gem "amenable" # requires Ruby >= 3.3Activate the refinement with using Amenable, then wrap a method as you define it (amenable def bark ...) or afterwards by name (amenable :bark). It works on instance, private, protected, and class methods (amenable def self.build), on module_function methods, and at the top level.
For a Method or Proc you already have, skip the refinement and use Amenable.call(fn, *args, **kwargs, &block):
Amenable.call(->(a) { a * 2 }, 3, 4, x: 1)
> 6Keywords
Extra keywords are dropped when the method can't take them. If it has an open positional slot, they land there as a Hash, as in plain Ruby:
class Greeter
using Amenable
amenable def hello(name, greeting: "hi")
"#{greeting} #{name}"
end
amenable def configure(opts = {})
opts
end
end
Greeter.new.hello("bob", greeting: "yo", volume: 11)
> "yo bob"
Greeter.new.configure(verbose: true)
> {verbose: true}Missing arguments still raise. Amenable removes; it never fills in.
Limits
- Only wraps methods defined directly on the receiver, not inherited ones (raises
NoMethodError). - When a class method and an instance method share a name,
amenable def fooin the class body wraps the instance method. Useclass << selfto wrap the class one. - Wrapped methods report the wrapper's
parameters,arity, andsource_location. - Curried lambdas and
Symbol#to_proccan't be stripped: their real signature isn't visible.
Contributing
Yes please :)
- Fork it
- Create your feature branch (
git checkout -b my-feature) - Ensure the tests pass (
bundle exec rspec) - Commit your changes (
git commit -am 'awesome new feature') - Push your branch (
git push origin my-feature) - Create a Pull Request