p vs puts in Ruby
p foo
prints foo.inspect
followed by a newline, i.e. it prints the value of inspect
instead of to_s
, which is more suitable for debugging (because you can e.g. tell the difference between 1
, "1"
and "2\b1"
, which you can't when printing without inspect
).
What is difference between p and pp?
p
is used to inspect a variable as a debug aide. It works printing the output of the method #inspect
. For example p foo
will output the content of foo.inspect
.
Sometimes you need to debug complex variables or nested variables. In this case p
will output a long line that is hard to understand. Instead, pp
will put try to arrange the content of the variable so that it is easier to understand, for example indenting nested arrays or using one line for each instance variable of a complex object. pp
does this calling the #pretty_inspect
method (the pp
library adds #pretty_inspect
methods to many classes such as String
, Array
or Struct
).
To remember: p
= print, pp
= pretty print.
What is the difference between print and puts?
puts
adds a new line to the end of each argument if there is not one already.
print
does not add a new line.
For example:
puts [[1,2,3], [4,5,nil]]
Would return:
1
2
3
4
5
Whereas print [[1,2,3], [4,5,nil]]
would return:
[[1,2,3], [4,5,nil]]
Notice how puts does not output the nil value whereas print does.
Difference between p in a rails view and puts
puts
calls the method to_s
p
calls the method inspect
class Foo
def to_s
"In #to_s"
end
def inspect
"In #inspect"
def
def
Semantically, to_s is meant to output the representation of the object to the user, and inspect to hint about the internal properties of the object (kinda like python's repr
), but that's just a convention.
If you want to inspect something in your HTML use <%= debug "something" %>
Difference between p and puts
p object
is equal to puts object.inspect
That's the main difference.
Object#inspect
returns a string containing a human-readable representation of object.
What is p in Ruby?
p()
is a Kernel method
It writes obj.inspect
to the standard output.
Because Object mixes in the Kernel module, the p()
method is available everywhere.
It's common, btw, to use it in poetry mode, meaning that the parens are dropped. The CSV snippet can be written like...
CSV.open 'data.csv', 'r' do |row|
p row
end
It's documented here with the rest of the Kernel module.
In Ruby, which is the object where the method puts is applied?
The OP is correct in thinking that, when the explicit receiver is omitted, the receiver becomes self
. And since puts
works without an explicit receiver, it is not that bad of an idea to try self.puts "hello"
in the same environment. Indeed puts
is defined on main
, or whatever object self
points to in the given environment.
The issue here is that the methods that can be called with an explicit receiver are public methods, whereas the method puts
is a private method, which rejects explicit receivers.
A standard way to detour this restriction is to use the method send
as follows:
self.send(:puts, "hello")
How to display multiple items using puts?
@order_items
is a hash and what has printed out if the string representation of such a hash. When you want to format the output differently, then you have to implement that on your own – for example like this:
def display
puts "Thank you for coming!"
@order_items.each do |name, quantity|
puts "#{quantity} #{name}"
end
end
Related Topics
How to Match the Last Occurrence of a Pattern
Does Ruby Have Real Multithreading
Is 'Eval' Supposed to Be Nasty
How to "Pretty" Format Json Output in Ruby on Rails
Converting a Nested Hash into a Flat Hash
Error to Install Nokogiri on Osx 10.9 Maverick
What Does 'Monkey Patching' Exactly Mean in Ruby
Ruby - Print the Variable Name and Then Its Value
When Is 'Eval' in Ruby Justified
Weird Backslash Substitution in Ruby
How to Get the Current Absolute Url in Ruby on Rails
What Is Ruby'S Double-Colon '::'
Difference Between Include and Extend in Ruby
Best Practices With Stdin in Ruby
Difference Between Collection Route and Member Route in Ruby on Rails
How to Create an Average from a Ruby Array