Sha256: 82bfeb8495ce5de987281274a3fe66102d76ec3498be8475430d87b2763632cf
Contents?: true
Size: 1.17 KB
Versions: 3
Compression:
Stored size: 1.17 KB
Contents
module ObjectInspector # ObjectInspector::ObjectInterrogator collaborates with {#object} to return # Object#{#method_name} if {#object} responds to the method. # # If Object#{#method_name} accepts the supplied {#kargs} then they are passed # in as well. If not, then any supplied {#kargs} will be ignored. class ObjectInterrogator attr_reader :object, :method_name, :kargs def initialize(object:, method_name:, kargs: {}) @object = object @method_name = method_name @kargs = kargs end # @return [String, ...] whatever type Object#{#method} returns # # @raise [ArgumentError] if Object#{#method} has an unexpected method # signature def call return unless object_responds_to_method_name? if object.method(method_name).arity != 0 call_with_kargs else object.send(method_name) end end private def call_with_kargs object.send(method_name, **kargs) rescue ArgumentError object.send(method_name) end def object_responds_to_method_name?(include_private: true) object.respond_to?(method_name, include_private) end end end
Version data entries
3 entries across 3 versions & 1 rubygems