module SingletonClassHelpers::Refinement

Refinement module that adds to Module the following methods:

translated_name: translates singleton class pointers in the class name to a more human readable name

singleton_of: returns the class from which the class is the singleton class.

Constants

SINGLETON_MATCHER

Public Instance Methods

singleton_of() click to toggle source

Returns the class for which the module/class is the singleton class.

It returns nil when the module/class is not a singleton class.

# File lib/singleton_class_helpers/refinement.rb, line 45
def singleton_of
  return unless singleton_class?

  ObjectSpace.reachable_objects_from(self).find do |reachable_obj|
    reachable_obj.singleton_class == self
  end
end
translated_name() click to toggle source

Returns the class name with singleton_classes names resolved.

Example:

using SingletonClassHelpers::Refinement

class TestClass; end

singleton_class = class << TestClass
  module SomeModule
    class << self
      self
    end
   end
end

singleton_class.name #<Class:0x00007fee7fa7c4a0>::AnotherClass
singleton_class.translated_name #<Class:#<Class:TestClass>::SomeModule>::AnotherClass
# File lib/singleton_class_helpers/refinement.rb, line 32
def translated_name
  if singleton_class?
    translate(inspect)
  else
    inspect.split("::").collect do |part|
      translate(part)
    end.join("::")
  end
end