Module: ShortInspect

Defined in:
lib/short_inspect.rb,
lib/short_inspect/version.rb

Defined Under Namespace

Modules: Minimal

Constant Summary collapse

VERSION =
"0.0.2"

Class Method Summary collapse

Class Method Details

.apply_minimal_to(*klasses) ⇒ Object



38
39
40
41
42
# File 'lib/short_inspect.rb', line 38

def self.apply_minimal_to *klasses
  Array(klasses).each do |klass|
    klass.send(:include, ShortInspect::Minimal)
  end
end

.apply_to(*klasses) ⇒ Object



32
33
34
35
36
# File 'lib/short_inspect.rb', line 32

def self.apply_to *klasses
  Array(klasses).each do |klass|
    klass.send(:include, ShortInspect)
  end
end

.basic_inspect(obj) ⇒ Object



25
26
27
28
29
30
# File 'lib/short_inspect.rb', line 25

def self.basic_inspect obj
  case obj
  when String, Fixnum, Float, Symbol, Bignum then obj.inspect
  else "#<%s:0x%x>" % [ obj.class.name, obj.object_id << 1 ]
  end
end

.included(base) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/short_inspect.rb', line 6

def self.included(base)
  base.class_eval do
    alias inspect_without_shortening inspect

    def inspect
      "#<%s:0x%x%s>" % [ self.class.name, object_id << 1, instance_variables_for_short_inspect ]
    end

    def instance_variables_for_short_inspect
      s = instance_variables.map do |name|
        "#{name}=#{ShortInspect.basic_inspect(instance_variable_get(name))}"
      end.join(", ")
      s = " #{s}" if s.length != 0
      s
    end

  end
end

.remove_from(*klasses) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/short_inspect.rb', line 44

def self.remove_from *klasses
  Array(klasses).each do |klass|
    klass.class_eval do
      alias inspect inspect_without_shortening
      remove_method :inspect_without_shortening
    end
  end
end