Kernel#p
p(object) -> object Returns:
Object · Updated March 13, 2026 · Kernel Methods output debugging inspection printing
The p method outputs objects to STDOUT using their inspect method, making it ideal for debugging. Unlike puts which calls to_s, p shows the actual structure of objects.
Basic Usage
# Simple values
p "hello" # => "hello"
p 42 # => 42
p [1, 2, 3] # => [1, 2, 3]
# Returns the object
result = p [1, 2, 3]
# Prints: [1, 2, 3]
# Returns: [1, 2, 3]
p vs puts vs print
# puts calls to_s
puts "hello" # hello (no quotes)
puts [1, 2] # 1
# 2
# p calls inspect
p "hello" # "hello" (with quotes)
p [1, 2] # [1, 2]
# print is like puts but no newline
print "hi" # hi (no newline)
Practical Examples
Debugging Arrays
data = [1, "hello", {key: "value"}]
p data
# Output: [1, "hello", {:key=>"value"}]
# (shows the actual structure)
Debugging Hashes
config = { debug: true, port: 8080 }
p config
# Output: {:debug=>true, :port=>8080}
Multiple Arguments
# p accepts multiple arguments
x = 10
p x, x*2, x*3
# Output:
# 10
# 20
# 30
In Methods
def process_data(input)
p input # Debug print
# ... process
p input # Debug print after
end
With Objects
class User
attr_accessor :name, :age
def initialize(name, age)
@name = name
@age = age
end
end
user = User.new("Alice", 30)
p user
# Output: #<User:0x00007f8a2c003a80 @name="Alice", @age=30>
Chaining
# p returns its argument, so it can be chained
result = p p [1, 2, 3]
# Prints:
# [1, 2, 3]
# [1, 2, 3]
# Returns: [1, 2, 3]
IRB/Console Debugging
# In IRB or Rails console
p variables
p params
p ENV["PATH"]
Use Cases
- Debugging variable values
- Inspecting complex data structures
- Quick logging during development
- Seeing exact object types