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
-
#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.
-
#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.
-
#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.
-
#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.
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
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
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
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
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 |