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) {|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, &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, &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, &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) {|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
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
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
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
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 |