Ruby print puts p printf output method


It will be a memo for learning.

Ruby has a print method as a method to output the result of some processing to the log or console in a visible form.

And in Ruby, there are various output methods other than the print method.

print method

The print method can be used only by `specifying the value to be output with a space between them, as shown in the sample code below.

The feature of the print method is that it prints the value specified in the argument without inserting a line break.

1.print 'Hello'
2.print 'hello world'

[Execution result]

1.Hello hello world

puts method

The puts method can also be used by specifying the output value after a half-width space, but the difference from the print method is that it is output with a line break at the end.

1.print 'Hello'
2.print 'hello world'

[Execution result]

2. hello world

p method

The p method outputs type information (string, numeric type, etc.) together with the output value.

p 'My age'
p 40
p 'I'm old.'

[Execution result]

"My age"
"I'm old."

You can see that the character string is enclosed in double quotation marks and the numbers are output as they are.

printf method

The printf method is a method for specifying the format and printing.

Let's check a concrete sample first.

printf('Product information:Category%s model number%d', 'bag', 10111)

[Execution result]

Product information:Category bag Model number 10111

Unlike the previous output methods, the output contents are enclosed in parentheses.

In addition,% s and% d are assigned the values of the arguments separated by commas after that, and the specification of% s and% d is used properly according to the type of the argument (character string or numeric type).


There are various other methods, so I will update them daily.

Recommended Posts

Ruby print puts p printf output method
[Ruby] Difference between print, puts and p
Learning Ruby with AtCoder 12 How to use standard output properly (p / puts / print)
String output method memo in Ruby
puts method
Note: Difference between Ruby "p" and "puts"
Ruby input / output
Ruby to_s method
[ruby] drill output
[ruby] drill output
[ruby] drill output
[Ruby] slice method
Ruby standard output
[Ruby] end_with? method
[Ruby] Method memorandum
[ruby] drill output
[ruby] drill output
[ruby] drill output
[ruby] drill output
[Ruby] Difference between puts and return, output and return value
[Ruby] initialize method
Ruby build method
Ruby accessor method
ruby map method
Output about the method # 2
Ruby Learning # 30 Initialize Method
Ruby Thread # [] = method notes
Ruby study notes (puts)
definition of ruby method
Ruby calorie calculation output
Output triangle in Ruby
[Ruby] Method definition summary