Monday, August 22, 2016

PHP print_r Equivalent in rails

In PHP you can print the human-readable information about a variable. In the views, the Rails equivalent is:


<%= debug obj %>
You can also use pretty print 'pp', in IRB:

require 'pp'

pp obj

In Rails console, you don't have to require 'pp'. The print method is used to inspect a variable. It will call the to_s method of an object. Doing:

print obj

is equivalent to:

obj.to_s




Example:
class Demo
  def to_s
    "This is a demo"
  end
end

d = Demo.new

print d
This will print: "This is demo"








No comments:

Post a Comment