module SelectiveInspect

Constants

VERSION

Public Class Methods

included(base_class) click to toggle source

Add to classes that include this module some convenient class mathods to blacklist or whitelist methods by default.

# File lib/selective_inspect.rb, line 74
def self.included(base_class)
  base_class.class_eval do
    alias_method :default_inspect, :inspect
  end
  base_class.extend ClassMethods
  base_class.include InstanceMethods
end
perform_inspect(target, *whitelist) click to toggle source

Public: Inspects the given object in a customizable way.

target - The Object to be inspected. whitelist - The names of the instance variables to output.

Examples

SelectiveInspect.inspect(player, :id, :name)
# =>

Returns the String that describes the objects and its internals.

# File lib/selective_inspect.rb, line 49
def self.perform_inspect(target, *whitelist)
  klass = target.class
  return target.default_inspect if !klass.include?(self) && whitelist.size == 0


  whitelist = klass.get_inspectionable_vars.map { |name| '@' + name.to_s }
  if whitelist.size == 0
    whitelist = target.instance_variables - klass.get_uninspectionable_vars.map { |name| '@' + name.to_s }
  end

  fields = whitelist.map do |var_name|
    var_content = target.instance_variable_get(var_name)
    "#{var_name}=#{var_content.inspect}"
  end

  string = "#<#{klass.name}:0x#{target.object_id} "
  string + fields.join(", ") + ">"
end