October 2006
Intermediate to advanced
888 pages
16h 55m
English
The purpose of the inspect method (and the p method that invokes it) is to show objects in human-readable form. In that sense, there is a connection with testing and debugging that justifies covering this here.
The only problem with p is that the object it prints out can be difficult to read. That is why we have the standard library pp, which adds a method of the same name.
Consider the following contrived object called my_obj:
class MyClass
attr_accessor :alpha, :beta, :gamma
def initialize(a,b,c)
@alpha, @beta, @gamma = a, b, c
end
end
x = MyClass.new(2, 3, 4)
y = MyClass.new(5, 6, 7)
z = MyClass.new(7, 8, 9)
my_obj = { x => y, z => [:p, :q] }
p my_objWhen we do the p, we get this output:
{#<MyClass:0xb7eed86c ...Read now
Unlock full access