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) {|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:

  • method_name (Symbol)

    the name of the method to invoke

  • args (Array)

    arguments to pass to the method

Yields:

  • (block)

    optional block to pass to the method

Returns:

  • (Object, nil)

    the result of the method call or nil if the



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

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

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

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

method doesn’t exist

Parameters:

  • method_name (Symbol)

    the name of the method to call

  • args (Array)

    arguments to pass to the method

  • block (Proc)

    optional block to pass to the method

Returns:

  • (Object, nil)

    the result of the method call or nil if the



28
29
30
31
32
# File 'lib/tins/ask_and_send.rb', line 28

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

#ask_and_send_or_self(method_name, *args, &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:

  • method_name (Symbol)

    the name of the method to call

  • args (Array)

    the arguments to pass to the method

  • block (Proc)

    the block to pass to the method

Returns:

  • (Object)

    the result of the method call or self if the method



45
46
47
48
49
50
51
# File 'lib/tins/ask_and_send.rb', line 45

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

#ask_and_send_or_self!(method_name, *args, &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:

  • method_name (Symbol)

    the name of the method to send

  • args (Array)

    the arguments to pass to the method

  • block (Proc)

    the block to pass to the method

Returns:

  • (Object)

    the result of the method call or the object itself if



64
65
66
67
68
69
70
# File 'lib/tins/ask_and_send.rb', line 64

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