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.
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
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]
1.Hello
2. hello world
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"
40
"I'm old."
You can see that the character string is enclosed in double quotation marks and the numbers are output as they are.
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