What is "p" in Ruby?

Viewed 21080

I'm sure it's a silly question to those who know, but I can't find an explanation of what it does or what it is.

CSV.open('data.csv', 'r') do |row|
  p row
end

What does "p row" do?

6 Answers

To understand the difference between p and puts, you must understand difference between to_s() and instance() methods.

to_s is used to get string representation of an object while instance is a more developer friendly version of to_s which gives contents of the objects as well.

class Dog
        def initialize(name, breed)
            @name = name
            @breed = breed
        end
        def to_s
            puts  "#@name's breed is #@breed."
        end
end

terra=Dog.new("Terra","Husky")
puts terra #Terra's breed is Husky.
p terra    #<Dog:0x00007fbde0932a88 @name="Terra", @breed="Husky">  
Related