Skip to content

Latest commit

 

History

History
32 lines (22 loc) · 1.21 KB

2016-02-21-puts_vs_p.md

File metadata and controls

32 lines (22 loc) · 1.21 KB
title tip-number tip-username tip-username-profile tip-description
Difference between puts and p
10
Logesh
Both puts and p are used for printing but then what the difference is covered here.

puts

When you use puts for printing, then puts internally uses to_s on objects before processing inputs, while p uses inspect on objects before processing inputs.

E.g. Again considering same example,

user = User.first
puts user
#<User:0x007fefb0c690b0>

This just has converted object's reference using to_s

p

p user
#<User id: 1, email: "[email protected]", encrypted_password: "$2a$10$D57y73Q9HUXG9Hym3bLl8.MizOdTRxd6NQH6snHi4Q....", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: "2014-10-15 11:19:09", sign_in_count: 13, current_sign_in_at: "2014-10-21 20:10:18", last_sign_in_at: "2014-10-20 17:37:27", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", created_at: "2014-06-30 17:41:06", updated_at: "2014-10-21 20:10:18">

Thus, you may prefer using p instead of to_s if you are looking purely for logging/ debugging purposes. Otherwise use of these methods purely depends on the required functionality though.