43
31

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】p, puts, printメソッドの違い

Last updated at Posted at 2018-10-04

Ruby初学者です。
今回はp,puts,printメソッドの違いについて記載します。


pメソッド

  • 引数と引数の型情報(文字列や数値)を一緒に出力。
    (数値はそのまま、文字列は""も表示。)
  • 改行あり。
ruby
p "これはpメソッドです。"
p 20181004
結果
"これはpメソッドです。"
20181004

putsメソッド - 引数(文字列)を出力。 - 改行あり。
ruby
puts "これはputsメソッドです。"
puts 20181004
結果
これはputsメソッドです。
20181004

printメソッド - 引数(文字列)を出力。 - 改行なし。
ruby
print "これはprintメソッドです。"
print 20181004
結果
これはprintメソッドです。20181004

参考URL
https://www.sejuku.net/blog/16119
https://tech.teshiblog.com/2017/07/30/post-13/
https://qiita.com/Chinats/items/c464c3a88c263f15224b
http://d.hatena.ne.jp/japanrock_pg/20080613/1213314138

43
31
1

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
43
31

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?