juniorRuby

Explain the difference between 'puts', 'print', and 'p' in Ruby.

Updated May 17, 2026

Short answer

puts appends a new line and converts elements to strings via to_s. print prints content exactly as specified without a newline. p calls inspect on the object, displaying its internal state, making it ideal for debugging.

Deep explanation

puts processes arguments by converting them to strings via to_s. If an array is passed, it outputs each element on a new line. It always appends a trailing newline unless the string already ends with one. print acts similarly but does not append a newline and prints arrays inline. p is a debugging method; it returns the object itself (whereas puts and print return nil) and calls .inspect on the argument to show its exact literal or structural representation.

Real-world example

Developers use puts for standard console output formatting, while p or pp (pretty print) is used directly inside code or console sessions to quickly check if a value is an array, string, nil, or integer.

Common mistakes

  • Using `puts` to debug a variable that might be `nil` or an empty string. `puts nil` prints a blank line, making it impossible to distinguish between an explicit `nil` value and an empty string. Use `p` instead.

Follow-up questions

  • What does the `pp` library do?
  • What value do `puts` and `print` return?

More Ruby interview questions

View all →