LoginSignup
1
1

More than 5 years have passed since last update.

Ruby の puts は to_s より先に to_ary が実行される (配列と文字列を除く)

Last updated at Posted at 2017-10-11

https://docs.ruby-lang.org/ja/latest/method/Kernel/m/puts.html より抜粋

配列や文字列以外のオブジェクトが引数として与えられた場合には、 当該オブジェクトを最初に to_ary により配列へ、 次に to_s メソッドにより文字列へ変換を試みます。

class Integer
  def to_ary
    ['to_ary']
  end

  def to_s
    '10'
  end
end

puts 1

出力

to_ary

参考サイト

1
1
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
1
1