Debug Printing Method

デバッグ用にオブジェクトの情報をプログラマに提供するためには、inspectメソッドをオーバーライドしましょう。

Rubyでは、オブジェクトを表示可能な文字列に変換するための仕組みとしてinspectメソッドを用意しています。デバッグ用にオブジェクトの情報をプログラマに提供したい場合は、このinspectメソッドをオーバーライドしてデバッグ用の情報を生成しましょう。

collection = ["one", "two", "three"]
p collection # => ["one", "two", "three"]

def collection.inspect
  print "[first] " + self.first + ", "
  print "[last] " + self.last + ", "
  print "[size] " + self.size.to_s + "\n"
end
p collection # => [first] one, [last] three, [size] 3