1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Ruby】puts,p,printメソッドの違い

Posted at

puts、p、printメソッドについて、違いが分からなかったので調べてみました。


putsメソッド

-改行あり
-引数のオブジェクトを文字列に変換して出力

putsメソッド

puts "あけまして、そらじろう"
puts 20200405
ターミナル
あけましてそらじろう
20200405

pメソッド
-改行あり
-引数と引数の**文字型(文字列や数値)**も出力

pメソッド
p "あけまして、そらじろう"
p 20200405
ターミナル
"あけまして、そらじろう"
20200405

printメソッド
-改行なし
-数のオブジェクトを文字列に変換し出力

printメソッド
print "あけまして、そらじろう"
print 20200405
ターミナル
あけましてそらじろう20200405

追記
putsメソッドとpメソッドの違いは、文字列として識別できるかどうか
printメソッドは、改行のないputsメソッド

ちなみにpメソッドは、デバッグでよく使うみたいです。

<https://gihyo.jp/book/2018/978-4-297-10123-7
<https://rurema.clear-code.com/

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?