Ruby 1.9.3 リファレンスマニュアル > ライブラリ一覧 > 組み込みライブラリ > Objectクラス > to_s

instance method Object#to_s

to_s -> String[permalink][rdoc]

オブジェクトの文字列表現を返します。

Kernel.#printKernel.#sprintf は文字列以外の オブジェクトが引数に渡された場合このメソッドを使って文字列に変換し ます。

class Foo
  def initialize num
    @num = num
  end
end
it = Foo.new(40)

puts it #=> #<Foo:0x2b69110>

class Foo
 def to_s
   "Class:Foo Number:#{@num}"
 end
end

puts it #=> Class:Foo Number:40

[SEE_ALSO] Object#to_str,Kernel.#String