Is There Equivalent for PHP's Print_R in Ruby/Rails

Is there equivalent for PHP's print_r in Ruby / Rails?

In Rails templates you can do

<%= debug an_object %>

and it will do nice HTML PRE output.

var_dump and die like php, In ruby on rails (debug in ruby on rails)

Rails will only output views to the browser. Any other output is sent to STD_OUT on the server.

Debugging from views is simple:

<%= debug @brand %>

But debugging from inside a controller or model requires you to either halt the execution with abort, which will output an error page:

abort @brand.inspect

Or you can write to the rails log with:

logger.debug(@brand.inspect)

You can read the log by using tail -f /logs/development.log from the your shell.

Are Ruby symbols the equivalent of PHP constants?

Symbols in ruby and constants in PHP are not really equivalent.

Most ruby implementations keep something called a symbol table that is internal to the interpreter. The symbol table stores identifiers used throughout a program, like method names, class names, and so on in said table. Lookups of said names are performed based on the integer position of said names in the symbol table.

Symbols themselves are objects, and there is a 1-to-1 correspondence between symbol names and Symbol objects. So when you have e.g. def hello ... and call the hello method somewhere in a program, this is referring to the exact same object as if you'd referred to :hello. The symbol table is basically a way to avoid doing a bunch of string comparisons when referring to commonly-used names throughout a program.

To illustrate, consider the following code:

class A
def hello
puts "the method name is #{__method__}"
puts "the class of the method name is #{__method__.class}"
puts "the object ID of the name hello is #{__method__.__id__}"
end
end

A.new.hello
puts "the object id of :hello is #{:hello.__id__}"

This outputs:

$ the method name is hello
the class of the method name is Symbol
the object ID of the name hello is 898268
the object id of :hello is 898268

So, the method name 'hello' and the symbol :hello refer to the same object. Storing symbols in this way reduces object allocations -- as you hinted at -- and by extension makes looking up commonly-used names in a program much faster.

Also, you can't assign values to symbols. e.g., :hello = "blah" won't work. Because :hello has a very concrete value according to the internal symbol table. So that's another obvious difference between ruby symbols and PHP constants.

In PHP, a constant is an identifier bound to a value where the value is immutable for the duration of the program's execution. The value can be any object or value. If you have two PHP constants that refer to the same value, e.g. define("A", "value") and define("B", "value"), then this is actually two separate identifiers pointing to two unique values.

So while A and B there point to the same semantic content, they do not point to a single identifiable object. And they do not reduce to a single identifiable object by referencing them. There's no internal store in PHP that keeps a map of common symbols-to-ids like there is for symbols in ruby. So I would not say that ruby symbols are the equivalent of PHP constants at all.

[1] - https://ruby-doc.org/core-2.2.0/Symbol.html

Is there a print_r or var_dump equivalent in Ruby / Ruby on Rails?

The .inspect method of any object should format is correctly for display, just do..

<%= theobject.inspect %>

The .methods method may also be of use:

<%= theobject.methods.inspect %>

It may help to put that in <pre> tags, depending on the data

Rails equivalent to php die()?

abort("Message goes here")

See: How to create an exit message

Rails-like console for PHP?

PHP has a basic interactive shell available by running php -a. It doesn't have the bells and whistles of a framework-based console though.

More info can be found in the docs: http://docs.php.net/commandline.interactive

How could you create a content_for equivalent in PHP?

  1. Never do include $_GET['p']. This opens a huge security hole in your site, as include accepts filenames and URLs, so anybody would be able to read any file on your site and also execute any code on your server. You may want to check and sanitize the value first.
  2. If you need something simple, you may put header and footer in separate files, execute your test.php which would set the variables, capture its output using output buffering, then include the header, output the middle part and include the footer. Example:

    <?php ob_start(); ?>
    <body>
    <?php include $filename.'.php'; ?>
    </body>
    <?php $content = ob_get_clean();
    include 'header.php';
    echo $content;
    include 'footer.php';
    ?>

How do I print out the contents of an object in Rails for easy debugging?

I generally first try .inspect, if that doesn't give me what I want, I'll switch to .to_yaml.

class User
attr_accessor :name, :age
end

user = User.new
user.name = "John Smith"
user.age = 30

puts user.inspect
#=> #<User:0x423270c @name="John Smith", @age=30>
puts user.to_yaml
#=> --- !ruby/object:User
#=> age: 30
#=> name: John Smith

Hope that helps.

Ruby equivalent of PHP's .= (dot equals) operator

irb(main):001:0> a = "ezcezc"
=> "ezcezc"
irb(main):002:0> a << "erer"
=> "ezcezcerer"

or

irb(main):003:0> a += "epruneiruv"
=> "ezcezcererepruneiruv"


Related Topics



Leave a reply



Submit