Class: Utils::IRB::Shell::MethodWrapper

Inherits:
WrapperBase show all
Defined in:
lib/utils/irb.rb

Overview

A wrapper class for Ruby method objects that provides enhanced introspection and display capabilities.

This class extends WrapperBase to create specialized wrappers for Ruby method objects, offering detailed information about methods including their source location, arity, and owner. It facilitates interactive exploration of Ruby methods in environments like IRB by providing structured access to method metadata and enabling sorting and comparison operations based on method descriptions.

Instance Attribute Summary collapse

Attributes inherited from WrapperBase

#description, #name

Instance Method Summary collapse

Methods inherited from WrapperBase

#==, #hash

Constructor Details

#initialize(obj, name, modul) ⇒ MethodWrapper

The initialize method sets up a new instance with the specified object, method name, and module flag.

This method creates and configures a new instance by storing the method object and its description, handling both instance methods and regular methods based on the module flag parameter.

Parameters:

  • obj (Object)

    the object from which to retrieve the method

  • name (String)

    the name of the method to retrieve

  • modul (TrueClass, FalseClass)

    flag indicating whether to retrieve an instance method



334
335
336
337
338
# File 'lib/utils/irb.rb', line 334

def initialize(obj, name, modul)
  super(name)
  @method = modul ? obj.instance_method(name) : obj.method(name)
  @description = @method.description(style: :namespace)
end

Instance Attribute Details

#methodObject (readonly)

The method reader returns the method object associated with the instance.



342
343
344
# File 'lib/utils/irb.rb', line 342

def method
  @method
end

Instance Method Details

#<=>(other) ⇒ Integer

The <=> method compares the descriptions of two objects for ordering purposes.

Parameters:

  • other (Object)

    the other object to compare against

Returns:

  • (Integer)

    -1 if this object’s description is less than the other’s, 0 if they are equal, or 1 if this object’s description is greater than the other’s



381
382
383
# File 'lib/utils/irb.rb', line 381

def <=>(other)
  @description <=> other.description
end

#arityInteger

The arity method returns the number of parameters expected by the method.

Returns:

  • (Integer)

    the number of required parameters for the method



357
358
359
# File 'lib/utils/irb.rb', line 357

def arity
  method.arity
end

#ownerObject?

The owner method retrieves the owner of the method object.

This method checks if the wrapped method object responds to the owner message and returns the owner if available, otherwise it returns nil.

Returns:

  • (Object, nil)

    the owner of the method or nil if not applicable



350
351
352
# File 'lib/utils/irb.rb', line 350

def owner
  method.respond_to?(:owner) ? method.owner : nil
end

#source_locationArray<String, Integer>

The source_location method retrieves the file path and line number where the method is defined.

This method accesses the underlying source location information for the method object, returning an array that contains the filename and line number of the method’s definition.

Returns:

  • (Array<String, Integer>)

    an array containing the filename and line number where the method is defined, or nil if the location cannot be determined



370
371
372
# File 'lib/utils/irb.rb', line 370

def source_location
  method.source_location
end