0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

inspect

Posted at

inspectの内容が文字列となって出力される

オブジェクトを人間が読める形式に変換した文字列を返します。

組み込み関数 Kernel.#p は、このメソッドの結果を使用してオブジェクトを表示します。

[ 1, 2, 3..4, 'five' ].inspect   # => "[1, 2, 3..4, \"five\"]"
Time.new.inspect                 # => "2008-03-08 19:43:39 +0900"
[ 1, 2, 3..4, 'five' ].inspect   # => "[1, 2, 3..4, \"five\"]"
Time.new.inspect                 # => "2008-03-08 19:43:39 +0900"

inspect メソッドをオーバーライドしなかった場合、クラス名とインスタンス変数の名前、値の組を元にした文字列を返します。

class Foo
end
Foo.new.inspect                  # => "#<Foo:0x0300c868>"

class Bar
  def initialize
    @bar = 1
  end
end
Bar.new.inspect                  # => "#<Bar:0x0300c868 @bar=1>"
class Foo
end
Foo.new.inspect                  # => "#<Foo:0x0300c868>"

class Bar
  def initialize
    @bar = 1
  end
end
Bar.new.inspect                  # => "#<Bar:0x0300c868 @bar=1>"

inspectあるなしで実験してみた

irb(main):057* class Hi
irb(main):058*   def inspect
irb(main):059*     "hi hi!"
irb(main):060*   end
irb(main):061> end
=> :inspect
irb(main):062> Hi.new
=> hi hi!
irb(main):001* class Hi
irb(main):002*   def dance
irb(main):003*     "hi hi dance!"
irb(main):004*   end
irb(main):005> end
=> :dance
irb(main):006> Hi.new
=> #<Hi:0x000000010911fee0>
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?