The difference between puts and print in Ruby is not just the presence or absence of line breaks

One of the standard themes of Ruby beginner articles is "Differences between puts, print, and p ". However,

It is a pity that there are many descriptions such as (only those that remain). """ With" is probably about String objects. What does "as is" mean? ?? ??

It's a shame that many articles don't have links, although you can clearly see the difference by looking at the official reference. (I'll also give you the pp link)

Here are just a few points I would like you to keep in mind. The object must be stringified in order to be output, but how to stringify it is important.

First, about p.

Next, about print.

Finally about puts.

puts is a bit complicated. There is also a process that "objects that react to to_ary are arrayed with this first ", but classes with to_ary (not to be confused with to_a) are rare, so even if you do not know at first I think it's okay.

Anyway, it's important to know about inspect and to_s to understand these methods.

The link above is a link to each method of the Object class, but inspect and to_s are overridden in many classes, in which case the method that is actually overridden is the one that is actually used. So, for example, to see what an array looks like with the p method, you have to look at Array # inspect.

Recommended Posts

The difference between puts and print in Ruby is not just the presence or absence of line breaks
The presence or absence of! In Ruby method names does not mean destructive / non-destructive
[Ruby] Difference between print, puts and p
About the difference between classes and instances in Ruby
In fact, Ruby distinguishes between line breaks and whitespace.
Difference between puts and print
About the difference between "(double quotation)" and "single quotation" in Ruby
What is the difference between the responsibilities of the domain layer and the application layer in the onion architecture [DDD]
About the difference between gets and gets.chomp (other than line breaks)
Note: Difference between Ruby "p" and "puts"
Difference between "|| =" and "instance_variable_defined?" In Ruby memoization
[Rails] Difference in behavior between delegate and has_many-through in the case of one-to-one-to-many
[Rails] Regarding the presence or absence of parentheses in the argument of the render method
What is the difference between SimpleDateFormat and DateTimeFormatter? ??
[Ruby] Difference between methods with and without self in the class. About class methods and instance methods.
[Ruby] Difference between puts and return, output and return value
Understand in 3 minutes! A very rough explanation of the difference between session and cookie
[Ruby] I thought about the difference between each_with_index and each.with_index
What is the difference between a class and a struct? ?? ??
What is the difference between System Spec and Feature Spec?
[Rails] What is the difference between redirect and render?
What is the difference between skip and pending? [RSpec]
What is the difference between Java EE and Jakarta EE?
[For Ruby beginners] What is the difference between puts print p? Actually, there is a clear way to use it properly!
Now in the third year, the misunderstanding that I noticed is the difference between the equals method and ==
[Rails] What is the difference between bundle install and bundle update?
Determine that the value is a multiple of 〇 in Ruby
Jersey --What is Difference Between bind and bindAsContract in HK2?
What is the difference between an action and an instance method?
The difference between programming with Ruby classes and programming without it
Difference between Not Null constraint and model validation (presence: true)
Handling of line beginning and line ending in regular expressions in Ruby
Is short-circuit evaluation really fast? Difference between && and & in Java
[Ruby] Difference between symbol variables and character string variables. About the difference between [: a] and ['a'].
Output the difference between each field of two objects in Java
What is @Override or @SuppressWarnings ("SleepWhileInLoop") in front of the function? ?? ??
Difference between byCharWrapping and byWordWrapping of UI Label in Japanese display
[Java] What is the difference between form, entity and dto? [Bean]
Easy to understand the difference between Ruby instance method and class method.
[Ruby] Difference between get and post
[Ruby] Difference between is_a? And instance_of?
Understand the difference between each_with_index and each.with_index
catch (Exception e) or catch (IOException e) is not required in the try-with-resources syntax
The content of the return value of executeBatch is different between 11g and 12c
"" Application.js "is not present in the asset pipeline" error in Ruby on Rails
[Ruby basics] About the role of true and break in the while statement
Understand the difference between int and Integer and BigInteger in java and float and double