Difference between p in a rails view and puts

I am a newbie in Rails. I created a controller and a action. In the corresponding view I used <%= puts "asd" %> once and <%= p "asd" %> the other time.

In case id puts it shows on the console and in case of p it is rendered as HTML. What is the possible reason?

Answers


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" %>


Need Your Help

Javascript Dynamic HREF Creation from XML Element Attribute Values

javascript xml href

I want to use javascript to create dynamic HTML links from the attributes of an xml file.

Basic C Server: Connection Refused Error

c sockets networking tcp

I have a program that should accept a connection at port 62085 and sends back a test message. The code hangs at accept() and never returns even if the client tries to connect. Why does the server