module Cabin::Inspectable

Public Instance Methods

inspect() click to toggle source

Provide a saner inspect method that’s easier to configure.

By default, will inspect all instance variables. You can tune this by setting @inspectables to an array of ivar symbols, like:

[ :@hello, :@world ]

class Foo
  include Cabin::Inspectable

  def initialize
    @inspectables = [:@foo, :@bar]
    @foo = 123
    @bar = "hello"
    @baz = "ok"
  end
end

foo = Foo.new
foo.inspect == '<Foo(1) @foo=123 @bar="hello" >'
# File lib/cabin/inspectable.rb, line 24
def inspect
  if instance_variable_defined?(:@inspectables)
    ivars = @inspectables
  else
    ivars = instance_variables
  end
  str = "<#{self.class.name}(@#{self.object_id}) "
  ivars.each do |ivar|
    str << "#{ivar}=#{instance_variable_get(ivar).inspect} "
  end
  str << ">"
  return str
end