Defi

Version Yard documentation Ruby RuboCop License

Defi is a streamlined Ruby library designed for the specification of method arguments while respecting their signatures. Rather than representing method signatures themselves, Defi focuses on providing a way to furnish methods with appropriate arguments, thereby preparing them for invocation.

This approach serves as an alternative to traditional methods like Object#method and UnboundMethod. Where Object#method is bound to a specific object and UnboundMethod requires compatibility with the method’s originating class, Defi offers a more flexible and universal way to prepare method calls. It allows method arguments to be specified in advance and then applied to any compatible object – those equipped with corresponding methods.

Defi is particularly useful in scenarios where you need to apply a set of method arguments across different objects to observe the varying outcomes, whether they be returned values or exceptions. This makes it an ideal tool for testing, method comparison across different implementations, or any situation where method behavior needs to be assessed dynamically across various objects.

Installation

Add this line to your application's Gemfile:

gem "defi"

And then execute:

bundle install

Or install it yourself as:

gem install defi

Usage

The Defi library simplifies the task of applying method signatures to various objects, regardless of their type (instances, modules, etc.). Below are some detailed examples to demonstrate its versatility and ease of use:

Example 1: Multiplying Numbers

Suppose you want to multiply the number 6 by 7. With Defi, this can be elegantly done as follows:

result = Defi(:*, 7).to(6).call
puts result # Output: 42

Here, Defi(:*, 7) creates a challenge with the multiplication method (:*) and the argument 7. The .to(6) method specifies that this challenge should be applied to the number 6. Finally, .call executes the challenge, yielding the result 42.

Example 2: Invoking an Undefined Method

Defi also elegantly handles cases where the method might not exist on the target object. For instance:

begin
  Defi(:boom).to("foo").call
rescue NoMethodError => e
  puts e.message # Output: undefined method `boom' for "foo":String
end

In this example, we attempt to call the non-existent method boom on the string "foo". Defi correctly raises a NoMethodError, showing that the method boom is undefined for a String object.

Contact

Versioning

Defi follows Semantic Versioning 2.0.

License

The gem is available as open source under the terms of the MIT License.


This project is sponsored by:
Sashité