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, &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
# File 'lib/sleeping_king_studios/tools/object_tools.rb', line 23

def apply base, proc, *args, &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

  value = base.send temporary_method_name, *args, &block

  metaclass.send :remove_method, temporary_method_name

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



41
42
43
# File 'lib/sleeping_king_studios/tools/object_tools.rb', line 41

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