2

I'm using the console in Rails to inspect some objects, like so:

dc = DependencyCondition.new
# => #<DependencyCondition id: nil, dependency_id: nil, rule_key: nil, question_id: nil, operator: nil, answer_id: nil, datetime_value: nil, integer_value: nil, float_value: nil, unit: nil, text_value: nil, string_value: nil, response_other: nil, created_at: nil, updated_at: nil>

I find this output difficult to read and inspect, especially with multiple objects. I would love if the output came out like this:

# => #<DependencyCondition id: nil, 
       dependency_id: nil, rule_key: nil, 
       question_id: nil, 
       operator: nil, 
       answer_id: nil, 
       datetime_value: nil, 
       integer_value: nil, 
       float_value: nil, 
       unit: nil, 
       text_value: nil, 
       string_value: nil, 
       response_other: nil, 
       created_at: nil, 
       updated_at: nil>

Is there an easy way to achieve this or something I should probably already know that I don't?

4 Answers 4

3

Install gem named hirb. You can install this gem by command:

gem install 'hirb'

Then launch your rails console and enter following commands,

> require 'hirb'

> Hirb.enable

Done. Your output will be in formatted way.

Sign up to request clarification or add additional context in comments.

Comments

1

You can use pry, a great alternative to irb, that has a lot of great features show in the RailsCast listed below.

RailsCast #280: Pry with Rails: Pry is an alternative to IRB and sports many great features. Here I show how to integrate it into a Rails app, and how it can aid in debugging.

The output will not be exactly what you are asking for, but I find it a lot more readable than the output in irb.

2 Comments

Pry looks really cool. I actually use to work with the guy, Josh Cheek, who did the introductory screencast for them. He's a great guy. I think I will invest the time to learn how to use it. Thanks.
This doesnt actually answer the question. pry is a nicer tool than irb, but specific features are required to get the output the OP is looking for. Simply doing exactly what he is doing in the question above would produce a big pile of difficult-to-read text.
0

Try yaml output:

y dc = DependencyCondition.new

You may also need to require 'yaml' if you get a NoMethodError on y

Comments

0

If using Pry - as recommended in the accepted answer - you can also choose your own output format .

Permanently (in a .pryrc file):

Pry.config.print = proc { |output, value| output.puts "=> #{value.inspect}" }

Temporarily (in a repl session):

_pry_.config.print = proc { |output,value| output.puts "=> #{value.inspect}" }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.