Module: SleepingKingStudios::Tools::ObjectTools

Extended by:
ObjectTools
Included in:
ObjectTools
Defined in:
lib/sleeping_king_studios/tools/object_tools.rb

Overview

Low-level tools for working with objects.

Instance Method Summary collapse

Instance Method Details

#apply(base, proc, *args, **kwargs, &block) ⇒ Object

Takes a proc or lambda and invokes it with the given object as receiver, with any additional arguments or block provided.

Parameters:

  • base (Object)

    The receiver. The proc will be called in the context of this object.

  • proc (Proc)

    The proc or lambda to call.

  • args (Array)

    Optional. Additional arguments to pass in to the proc or lambda.

  • block (block)

    Optional. If present, will be passed in to proc or lambda.

Returns:

  • The result of calling the proc or lambda with the given receiver and any additional arguments or block.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/sleeping_king_studios/tools/object_tools.rb', line 23

def apply base, proc, *args, **kwargs, &block
  temporary_method_name = :__sleeping_king_studios_tools_object_tools_temporary_method_for_applying_proc__

  metaclass = class << base; self; end
  metaclass.send :define_method, temporary_method_name, &proc

  begin
    if kwargs.empty?
      base.send temporary_method_name, *args, &block
    else
      base.send temporary_method_name, *args, **kwargs, &block
    end # if-else
  ensure
    metaclass.send :remove_method, temporary_method_name if temporary_method_name && defined?(temporary_method_name)
  end
end

#eigenclass(object) ⇒ Class Also known as: metaclass

Returns the object’s eigenclass.

Parameters:

  • object (Object)

    The object for which an eigenclass is required.

Returns:

  • (Class)

    The object’s eigenclass.



45
46
47
# File 'lib/sleeping_king_studios/tools/object_tools.rb', line 45

def eigenclass object
  class << object; self; end
end