Ruby 2.4.0 リファレンスマニュアル > ライブラリ一覧 > 組み込みライブラリ > Objectクラス > to_s
to_s -> String
[permalink][rdoc]オブジェクトの文字列表現を返します。
Kernel.#print や Kernel.#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