Module: Tins::AskAndSend

Included in:
Object
Defined in:
lib/tins/ask_and_send.rb

Overview

A module that provides methods to call private and protected methods on objects.

Instance Method Summary collapse

Instance Method Details

#ask_and_send(method_name, *args, **kwargs) {|block| ... } ⇒ Object?

The ask_and_send method attempts to invoke a given method on the object if that method is available.

method doesn't exist

Parameters:

  • the name of the method to invoke

  • arguments to pass to the method

  • keyword arguments to pass to the method

Yields:

  • (block)

    optional block to pass to the method

Returns:

  • the result of the method call or nil if the



14
15
16
17
18
# File 'lib/tins/ask_and_send.rb', line 14

def ask_and_send(method_name, *args, **kwargs, &block)
  if respond_to?(method_name)
    __send__(method_name, *args, **kwargs, &block)
  end
end

#ask_and_send!(method_name, *args, **kwargs, &block) ⇒ Object?

The ask_and_send! method attempts to invoke a private or protected method on the object.

method doesn't exist

Parameters:

  • the name of the method to call

  • arguments to pass to the method

  • keyword arguments to pass to the method

  • optional block to pass to the method

Returns:

  • the result of the method call or nil if the



30
31
32
33
34
# File 'lib/tins/ask_and_send.rb', line 30

def ask_and_send!(method_name, *args, **kwargs, &block)
  if respond_to?(method_name, true)
    __send__(method_name, *args, **kwargs, &block)
  end
end

#ask_and_send_or_self(method_name, *args, **kwargs, &block) ⇒ Object

The ask_and_send_or_self method attempts to invoke the specified method on the object If the method exists, it calls the method with the provided arguments and block If the method does not exist, it returns the object itself

doesn't exist

Parameters:

  • the name of the method to call

  • the arguments to pass to the method

  • the keyword arguments to pass to the method

  • the block to pass to the method

Returns:

  • the result of the method call or self if the method



48
49
50
51
52
53
54
# File 'lib/tins/ask_and_send.rb', line 48

def ask_and_send_or_self(method_name, *args, **kwargs, &block)
  if respond_to?(method_name)
    __send__(method_name, *args, **kwargs, &block)
  else
    self
  end
end

#ask_and_send_or_self!(method_name, *args, **kwargs, &block) ⇒ Object

The ask_and_send_or_self! method attempts to send a message to the object with the given method name and arguments. If the object responds to the method, it executes the method and returns the result. If the object does not respond to the method, it returns the object itself.

the method is not found

Parameters:

  • the name of the method to send

  • the arguments to pass to the method

  • the keyword arguments to pass to the method

  • the block to pass to the method

Returns:

  • the result of the method call or the object itself if



68
69
70
71
72
73
74
# File 'lib/tins/ask_and_send.rb', line 68

def ask_and_send_or_self!(method_name, *args, **kwargs, &block)
  if respond_to?(method_name, true)
    __send__(method_name, *args, **kwargs, &block)
  else
    self
  end
end