p are String output functions. Let’s see what difference and how to use them.
puts is a basic output function. You can use it like below.
puts 'Hello World!' # Hello World!
You can also use it with parentheses.
puts ('Hello World!') # Hello World!
You can print multiple strings like below.
puts 'Hello', 'World!' # Hello # World!
puts function prints the string and adds a line break at the last.
In Ruby, the
puts function, but not add a line break.
print 'Hello World!' # Hello World!
You can see a difference when you use it like below.
print 'Hello', ' World!' # Hello World!
If you want to break a line, you should add the line break character(
print "Hello\nWorld!" # Hello # World!
Note, when you use the Escape Sequences like the line break(
\n) in Ruby, you should use the double quotation(
") to print them well.
In Ruby, you can print strings by itself via
p '100' # '100'
It is useful to distinguish numbers and strings like below.
p '100' p 100 # '100' # 100
We’ve seen what difference and how to use String Output Function in Ruby. There are many functions. Normally,
puts is used, so just don’t forget
puts function to print the string in Ruby.
Was my blog helpful? Please leave a comment at the bottom. it will be a great help to me!
Dekucreated the applications with Flutter.
If you have interested, please try to download them for free.