Interactive Shell for Ruby on Rails - ruby-on-rails

When working on a web app in python/flask, I am able to import pdb at the top of a file, and then call pdb.set_trace() somewhere in my code to "pause" the web app and open an interactive console in my terminal for debugging. I am looking for something similar in Ruby/Rails. What exists for this purpose, and how do I use it?

Pry (and pry-remote) might be what you're looking for. I have no experience with it, but this is what it's supposed to let you do.
https://github.com/mon-ouie/pry-remote

I have found a solution.
The gem debugger is created for this purpose. To start, run
gem install debugger
After, add the line debugger into your code to "pause" the code at that point. For example:
def hello
#users = Users.all()
debugger
end
Then, when starting the server, call it with:
rails server --debugger

Related

Is there a reliable tool for stepping through code while serving in foreman

I'm looking for some suggestions on a tool similar to using binding.pry.
I'm serving a rails app locally with foreman and would like to accomplish the same ability to put "debuggers" in my code and when the process hits that, freeze and open a console environment in that section of the code.
I've tried using pry with foreman to no avail and was curious if there are other known tools I'm not finding through google?
Thanks for the suggestions.
Try pry-remote, it seems to be the answer of running Pry with Foreman.
Checklist:
Make sure you're using binding.remote_pry instead of binding.pry.
Make sure to run your program making binding.remote_pry first and then run in console remote-pry, otherwise the pry server won't be running.
If nothing helps, try adding require 'pry-remote' at the top of your .rb file.
Check also this issue.

Debugging in rails 3

i want to debug ROR without going through the effort of putting inspect method for every relevant object in the controller as well in the model.is there a better way as we have in Java (Run time debugger using eclipse).i know that I can Use Rails.logger and also make use of rails Console(irb`).i am even aware of debugging/inspecting elements in erb/rb file.Still is there a better,quick and reliable way to debug a Rails app.
There is much better, see this railscats.
It presents two great gems, especially Better Errors
Otherwise, you could use pry with rails, see this railscast.
you can also use pry-rails, pry-debugger and then use binding.pry method in your code and then while using your app you have Rails console available in rails server
Add this lines to your application's Gemfile
group :development do
gem 'ruby-debug19'
end
then run cammand
bundle install
add debugger within your controller or model method, stop the rails server and restart again. Whenever rails found word debugger it stops control at that point. You can easily debug your value or object.
Hope this will helps you.

Rails Debugging - Exit controller prematurely

I was wondering how to exit a controller in rails and get the output up to that point.
In PHP I often used the "exit" when debugging to get only the data processed to that point. I haven't found a sollution to this in rails.
If you get a error further down in the code the view is locked from displaying <%= debug %> information.
Some would suggest console or rescue, and I know about these. But isn't there a simpler solution?
In development mode, I often just use puts or awesome_print to print something to the screen that I ran rails server from. That works pretty well for the simple cases.
For anything more complex than that, I use ruby-debug or pry to drop down into an interactive console when it hits the right point.
I have some editor shortcuts to print one of these two snippets:
require 'pry'; binding.pry
require 'ruby-debug'; debugger
Drop these in your code and you can use IRB to inspect (and manipulate) the state of your program.
I highly recommend you give pry a shot. Check it out here:
http://pryrepl.org/
http://railscasts.com/episodes/280-pry-with-rails
There is also the older ruby-debug:
http://railscasts.com/episodes/54-debugging-with-ruby-debug

Ruby / Rails debugging strategy

Please can you share your approach / methodology to debugging in Ruby / Rails.
I'm busy with the Rails tutorial, and am getting the error:
NoMethodError in UsersController#show
undefined method `microposts' for #<User:0x83b43e8>
And that got me thinking about debugging strategies. Does anyone have advice for a new Rails user (and new MVC user) on strategies to approach debugging. What path do you follow? Is there a generally accepted approach? Is there a way to step through the code?
Right now I am using unit testing as a kind of "lint" checker, but that only goes so far.
Although I want to solve it, the actual error I am getting right now is not the main thrust of this question.
(PS: The problem is not a duplicated "show" as documented in elsewhere on Stackoverflow
I haven't seen this mentioned yet but another option is to put a debugger statement in your code. Like this
def some_method
something = 3
debugger
# ... more code
end
If this is in a rails app when the code reaches debugger it will cause the terminal window running the web server to jump into something that looks like an irb session (I'm not exactly sure what it is). From there you can do puts something to see what the value is for example. You can even puts params to see what all the params values are. You can also step through the code, jump to a specific line, etc.
Pry seems to be a better way to go about this but it's how I used to debug before I knew about pry.
Note: You might need to do require 'ruby-debug' if you're doing this outside of rails.
i use a combination of irb, print statements, logging and pry bindings. (pry is a great gem)
irb is a great way to just play around with your ruby or rails app in the console. You could just enter the code from your controller (or similar) and see if it breaks in console for faster feedback loop. But remember you have to do reload! if you change anything in your class/module.
print statements are easy if you're running tests and just want it to output something a different points in your test. But if your testing in a browser I would recommend writing to the logger: Rails.logger.debug "...". But remember to set your logging level in your configuration to DEBUG -or- you can just do Rails.logger.info instead which should show up by default. Then you can just tail or view the logs in my_app/logs/development.rb.
My favorite method for really tricky bugs is that if the error is happening in a test you can just place binding.pry in the preceding line and then it will pause your test at that line and drop you into a console. I recommend watching the rails casts for more in-depth info: http://railscasts.com/episodes/280-pry-with-rails
I do not start Rails project without 'pry' gem.
Add gem to Genfile:
group :development, :test do
gem 'pry'
end
and stop request execution anywhere in your project, just put
binding.pry
to your model, controller, tests ..., or
<% binding.pry %>
in your view's, templates, partials.
Then you can check what ever you want objects, params, variables ...
Type exit to leave pry environment, and request will continue.
The Ruby on Rails Guide would be a great place to start, but there's plenty more.
I always have a rails console session or at minimum an irb session to play with to see if things do what I think they do.
I also use RubyMine which has an excellent integrated debugger http://www.jetbrains.com/ruby/
Beside pry gem, another option would be byebug. This gem enables you to temporarily stop code execution at a breakpoint, which is marked with keyword byebug inside the code. When execution reaches the breakpoint, a marker will be pointing to the current line, and you are able to type in commands.
This Gem offers a huge set of commands, the most commonly used ones are:
next - this command enables you to go to next line
step - goes into each invoked method step by step
break - it stops the execution of code
continue - continues code execution
This is a great article to check for debugging in rails.

How can I clear the rails console history

When I run rails c and press the up key when irb starts up, I can see the last commands I entered when my app dropped to irb after encountering a debugger command for the ruby-debug gem. I would not only like to clear these commands out, but I would like it if rails c would pull the last commands I issued during my last rails console session. I think it used to do this but I'm not sure what has changed. I'm on ruby 1.8.7 and rails 3.0.3 on Mac OS 10.6.5 if that helps.
Update
Ray's answer helped me out in the interim. Recently I did a bit more digging to find out more and realized that there are a number of conflicting issues.
IRB checks if you have a ~/.irbrc and if not falls back to /etc/irbrc as Ray mentioned. However, if you are using rvm there is another file to consider ~/.rvm/scripts/irbrc which just loads up ~/.rvm/scripts/irbrc.rb (note the .rb) if you have rvm_path set in your ENV (you should if using rvm).
Interestingly while ~/.rvm/scripts/irbrc.rb was based off of /etc/irbrc they are not the same and differ in a few ways. The most obvious way and easiest way to detect which one is being used on your system is their history file's name. If /etc/irbrc is being used your history file will be ~/.irb_history where as rvm's is ~/.irb-history (Note: _ vs -).
Hopefully this additional information will help you determine what you need to setup your system as you would like.
Pry Concerns
I've since stopped using debugger and have moved to pry-byebug which includes the pry gem. Pry is an alternative to IRB but can also be used along side and within it. The reason I was able to provide the above update is because I was trying to figure out how to keep their respective histories separate. For more information please see my answer to the SO question on "why does pry history keep cloberring irb history?". I've included links there to the known Github issue for Pry as well as my attempt to fix it.
I interpret you question as asking how to turn history on in the Rails Console and off in the Ruby debugger. If this isn't true, please clarify.
IRB, and by extension, the Rails Console, read from ~/.irbrc, or if that doesn't exist, /etc/irbrc, to startup and configure irb. Your history is typically written to ~/.irb_history, but that is dictated by the contents of your irbrc file. The /etc/irbrc on my Mac OS X is set up to write the history from irb, so perhaps you've created a local .irbrc that doesn't have history, or perhaps you have a syntax error in that file.
The debugger reads a file called .rdebugrc on startup. You can turn off history in debug by adding this line to ~/.rdebugrc:
set history save off
Turn it back on with:
set history save on
You could also set your debug output to go to a different file than irb reads from with the command:
set history filename
These also work from the debug prompt, but aren't persistent.
There are a number of tools to help improve the irb experience. Bond and hirb are promising.
Here is Comprehensive list of Irb Tools and some tips on directly editing the .irbrc file.
Hope this help!
Although a very old question I got here by google.
Turns out RVM slightly changed over time.
Currently my IRB history (using rvm) is stored here:
user#host:~$ ls ~/.rvm/rubies/ruby-2.4.2/.irbrc*
/home/user/.rvm/rubies/ruby-2.4.2/.irbrc
/home/user/.rvm/rubies/ruby-2.4.2/.irbrc_history

Resources