inquisitive
inquisitive

Reputation: 3974

How to debug using rails console and puts in application

I want to get some lines printed in irb opened through rails console. I have seen a lot of SO questions of how to achieve it. But I get nothing in the irb.

below is the code--

def show
puts 'in show method'
@post = Feed.find_by_id params[:id]
puts @post.inspect
redirect_to root_path unless @post.present? 
end

now I have opened server by command rails server. Also, In another terminal I gave the command rails console, it opened the irb prompt. when in browser I run localhost:3000/posts/82 it gives the correct post, but nothing is shown in the console. What step am I missing? I want to print something in the console when a particular method is called.

Upvotes: 3

Views: 27463

Answers (6)

Victor Cordeiro Costa
Victor Cordeiro Costa

Reputation: 2194

The irb version 1.6 has been released and allow debugging via binding.irb. In order to use it, you need to have ruby >= 3.2.

It allows several debugging commands, like show_cmds, show_doc, edit, debug, break, catch, next, delete, step, continue, finish, backtrace, info. All of them can be seen here:

However, still, at least for now, I prefer to recommend binding.pry instead of binding.irb for the debugging process. Using particularly the pry-byebug gem, which is a gem that enhances the binding.pry debugger with byebug capabilities, adding debugging commands like break, step, next, finish, continue, backtrace, up, down, frame, whereami, ls, cd, !!!, help, help alias, help whereami, w, @, $, @ 5, h , $ some_method, show-source method_name, find-method to_a, play -l line_number, self, pp(obj), cd SomeModule, ls -m, cd .., b, s, c, n, and much more...

This debugger provides an also robust list of commands, but more importantly: with less bugs and more intuitive interface at the same time. I will explain these points in more detail in the next section.

binding.irb X binding.pry?

Which one is better at debugging in rails?

Comparison

We can start a comparison by a very simple example via rails c.

  • binding.irb example
def sum(a, b)
  binding.irb
  puts a
  puts b
  a + b
end
  • binding.pry example
def sum(a, b)
  binding.pry
  puts a
  puts b
  a + b
end

Outputs

binding.irb

binding.irb

binding.pry

binding.pry

Conclusion

While binding.irb has a very interesting promise, like serving as the main debugger in rails, it comes with bugs and drawbacks that simply don't exist when we using binding.pry for the debugging process.

1. Where am I ?!

binding.pry looks more clear for the debugging process, particularly because binding.pry outputs to the user the code where binding.pry is located as soon as the code hits this debugger, while binding.irb doesn't output anything to the user when the code hits this debugger (even if we call whereami sometimes it doesn't output anything (😱), which is very confusing in terms of usability).

2. What are the values ?!

In addition, there are bugs in binding.irb related to not outputting the value of a variable when we call it! We can check it by looking at the above pictures and noticing the a variable value was not shown to the user here:

irb(main):004> a

On the other hand, binding.pry doesn't have this bug and always output the value of a variable when we call it and press enter.

As a last note... Is there a way to fix all of these bugs in binding.irb? In case not, I would recommend to keep using binding.pry for the debugging process to avoid headaches.

Upvotes: 0

Fer
Fer

Reputation: 3347

Best way to debug is to use the debugger command.

If you are using ruby 2.0 or above, you have to use the gem 'byebug' and if you are using 1.9 or below, then gem ruby-debug

then, when you run your server in development mode, your server will stop when it reaches the debugger allowing you to see your objects' state and modify them (much better than simply using puts

The program will stop in the same window that your server runs.

Some basic commands:

  • c continues the execution until next debugger is found
  • n runs the next command. If it is a function executes the function
  • s step into the next command. If it is a function, you will get into the function and see the variables
  • display expression on every step display the result of the expression you write (very useful when debugging loops)
  • undisplay expression_number stops displaying the expresion
  • display shows all the expressions being displayed
  • list Displays the source code being executed
  • help shows the available commands help command_name shows detailed info about a command

More info about debugging: http://guides.rubyonrails.org/debugging_rails_applications.html

Upvotes: 11

konole
konole

Reputation: 766

  1. Use Rails.logger.debug "in show method" etc.
  2. In the second tab in terminal tail log/development.log like this

    $ cd rails_app_root
    $ tail -f log/development.log
    

    or

    $ cd rails_app_root
    $ less +F log/development.log
    

There you will find all the output from the console.

Upvotes: 1

tokhi
tokhi

Reputation: 21626

I am a big fan of puts debugging. e.g;

def index
  method = Kernel.instance_method(:method)
  p method.bind(request).call(:headers).source_location
  @users = User.all
end

The above snippet helps you find where the method is implemented:

Processing by UsersController#index as */*
["/Users/aaron/git/rails/actionpack/lib/action_dispatch/http/request.rb", 201]

You can find more cool puts debugging here.

Upvotes: 0

VDN
VDN

Reputation: 516

Try to use Rails.logger

Rails.logger.info "Some debugging info I want to see in my development log.------#{@post.inspect}"

It will print @post value in log file.

Upvotes: 0

Bibek Shrestha
Bibek Shrestha

Reputation: 35054

The puts 'in show method' in line 2 won't show the output in rails console. Instead it shows the output in the same terminal where you did rails server. They might be lost with so much of output, so try to find it there itself.

Upvotes: 2

Related Questions