Module: Kernel

Defined in:
lib/kernel.rb

Overview

Extension of the Kernel module to include the Defi method. The Defi method is a convenient way to create Method objects that encapsulate a method name and its arguments, offering a dynamic approach to method invocation.

Instance Method Summary collapse

Instance Method Details

#Defi(method_name) ⇒ Defi::Method

Creates a new Defi::Method instance. This method provides a simple and elegant way to encapsulate a method name and its arguments for later invocation.

Examples:

Creating a Defi method without arguments

Defi(:foo).inspect # => "Defi(name: :foo, args: [], opts: {}, block: nil)"

Adding 2 to 1

# Create a Defi method object for addition with an argument of 2
addition = Defi(:+, 2)
addition.inspect # => "Defi(name: :+, args: [2], opts: {}, block: nil)"

# Apply the addition to the number 1
result = addition.to(1)
result # => Value(object: 3, raised: false)

# Execute the addition and get the result
result.call # => 3

Parameters:

  • method_name (Symbol)

    The method name to be sent to an object.

Returns:

  • (Defi::Method)

    An instance of Defi::Method encapsulating the method name and provided arguments.



36
37
38
# File 'lib/kernel.rb', line 36

def Defi(method_name, ...)
  ::Defi::Method.new(method_name, ...)
end