Module: Looksee

Defined in:
lib/looksee.rb,
lib/looksee/version.rb,
lib/looksee/shortcuts.rb,
lib/looksee/wirble_compatibility.rb,
ext/looksee/looksee.c

Overview

Looksee lets you inspect the method lookup path of an object. There are two ways to use it:

  1. Keep all methods contained in the Looksee namespace:

    require 'looksee'
    
  2. Let it all hang out:

    require 'looksee/shortcuts'
    

The latter adds the following shortcuts to the built-in classes:

Object#lookup_path
Object#dump_lookup_path
Object#lp
Object#lpi

See their docs.

Usage

In irb:

require 'looksee/shortcuts'
lp some_object

lp returns a LookupPath object, which has inspect defined to print things out pretty. By default, it shows public, protected, undefined, and overridden methods. They’re all colored, which makes showing overridden methods not such a strange idea.

Some examples of the other shortcuts:

lpi Array
some_object.lookup_path
foo.bar.baz.dump_lookup_path.and.more

If you’re being namespace-clean, you’ll need to do:

require 'looksee'
Looksee.lookup_path(thing)  # like "lp thing"

For a quick reference:

Looksee.help

Configuration

Set these:

Looksee.default_lookup_path_options
Looksee.default_width
Looksee.styles

See their docs.

Defined Under Namespace

Modules: Columnizer, WirbleCompatibility Classes: Help, LookupPath

Constant Summary collapse

VERSION =
'0.2.1'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.default_lookup_path_optionsObject

The default options passed to lookup_path.

Default: {:public => true, :protected => true, :undefined => true, :overridden => true}



95
96
97
# File 'lib/looksee.rb', line 95

def default_lookup_path_options
  @default_lookup_path_options
end

.default_widthObject

The width to use for displaying output, when not available in the COLUMNS environment variable.

Default: 80



103
104
105
# File 'lib/looksee.rb', line 103

def default_width
  @default_width
end

.stylesObject

The default styles to use for the inspect strings.

This is a hash with keys:

  • :module

  • :public

  • :protected

  • :private

  • :undefined

  • :overridden

The values are format strings. They should all contain a single “%s”, which is where the name is inserted.

Default:

{
  :module     => "\e[1;37m%s\e[0m", # white
  :public     => "\e[1;32m%s\e[0m", # green
  :protected  => "\e[1;33m%s\e[0m", # yellow
  :private    => "\e[1;31m%s\e[0m", # red
  :undefined  => "\e[1;34m%s\e[0m", # blue
  :overridden => "\e[1;30m%s\e[0m", # black
}


131
132
133
# File 'lib/looksee.rb', line 131

def styles
  @styles
end

Class Method Details

.helpObject

Show a quick reference.



60
61
62
# File 'lib/looksee/shortcuts.rb', line 60

def self.help
  Help.new
end

.internal_class(object) ⇒ Object

Return the internal class of the given object.

This is either the object’s singleton class, if it exists, or the object’s birth class.



38
39
40
# File 'ext/looksee/looksee.c', line 38

VALUE Looksee_internal_class(VALUE self, VALUE object) {
  return CLASS_OF(object);
}

.internal_class_to_module(internal_class) ⇒ Object

Return the class or module that the given internal class represents.

If a class is given, this is the class. If an iclass is given, this is the module it represents in the lookup chain.



49
50
51
52
53
54
55
56
57
58
59
# File 'ext/looksee/looksee.c', line 49

VALUE Looksee_internal_class_to_module(VALUE self, VALUE internal_class) {
  if (!SPECIAL_CONST_P(internal_class)) {
    switch (BUILTIN_TYPE(internal_class)) {
    case T_ICLASS:
      return RBASIC(internal_class)->klass;
    case T_CLASS:
      return internal_class;
    }
  }
  rb_raise(rb_eArgError, "not an internal class: %s", RSTRING_PTR(rb_inspect(internal_class)));
}

.internal_private_instance_methods(klass) ⇒ Object

Return the list of private instance methods (as Symbols) of the given internal class.



114
115
116
# File 'ext/looksee/looksee.c', line 114

VALUE Looksee_internal_private_instance_methods(VALUE self, VALUE klass) {
  return internal_instance_methods(klass, NOEX_PRIVATE);
}

.internal_protected_instance_methods(klass) ⇒ Object

Return the list of protected instance methods (as Symbols) of the given internal class.



106
107
108
# File 'ext/looksee/looksee.c', line 106

VALUE Looksee_internal_protected_instance_methods(VALUE self, VALUE klass) {
  return internal_instance_methods(klass, NOEX_PROTECTED);
}

.internal_public_instance_methods(klass) ⇒ Object

Return the list of public instance methods (as Symbols) of the given internal class.



98
99
100
# File 'ext/looksee/looksee.c', line 98

VALUE Looksee_internal_public_instance_methods(VALUE self, VALUE klass) {
  return internal_instance_methods(klass, NOEX_PUBLIC);
}

.internal_superclass(internal_class) ⇒ Object

Return the internal superclass of this class.

This is either a Class or “IClass.” IClasses represent Modules included in the ancestry, and should be treated as opaque objects in ruby space. Convert the IClass to a Module using #iclass_to_module before using it in ruby.



25
26
27
28
29
30
# File 'ext/looksee/looksee.c', line 25

VALUE Looksee_internal_superclass(VALUE self, VALUE internal_class) {
  VALUE super = RCLASS_SUPER(internal_class);
  if (!super)
    return Qnil;
  return super;
}

.internal_undefined_instance_methods(klass) ⇒ Object

Return the list of undefined instance methods (as Symbols) of the given internal class.



129
130
131
132
133
# File 'ext/looksee/looksee.c', line 129

VALUE Looksee_internal_undefined_instance_methods(VALUE self, VALUE klass) {
  VALUE names = rb_ary_new();
  st_foreach(RCLASS_M_TBL(klass), add_method_if_undefined, (st_data_t)&names);
  return names;
}

.lookup_modules(object) ⇒ Object

Return the chain of classes and modules which comprise the object’s method lookup path.



137
138
139
140
141
142
143
144
145
# File 'lib/looksee.rb', line 137

def lookup_modules(object)
  modules = []
  klass = Looksee.internal_class(object)
  while klass
    modules << Looksee.internal_class_to_module(klass)
    klass = Looksee.internal_superclass(klass)
  end
  modules
end

.lookup_path(object, *options) ⇒ Object

Return a collection of methods that object responds to, according to the options given. The following options are recognized:

  • :public - include public methods

  • :protected - include protected methods

  • :private - include private methods

  • :undefined - include undefined methods (see Module#undef_method)

  • :overridden - include methods overridden by subclasses

The default (if options is nil or omitted) is given by #default_lookup_path_options.



79
80
81
82
83
84
85
86
87
# File 'lib/looksee.rb', line 79

def lookup_path(object, *options)
  normalized_options = Looksee.default_lookup_path_options.dup
  hash_options = options.last.is_a?(Hash) ? options.pop : {}
  options.each do |option|
    normalized_options[option] = true
  end
  normalized_options.update(hash_options)
  LookupPath.for(object, normalized_options)
end