One of the standard themes of Ruby beginner articles is "Differences between puts, print, and p ".
However,
puts outputs with line breaksprint prints without line breaksp displays the object as isp is displayed with""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.
inspect methodNext, about print.
to_s methodFinally about puts.
to_sputs 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