How to debug ruby code - ruby-on-rails

I want to debug my Ruby code using the Ruby Mine IDE. In the drop down list Select Run/Debug Configuration, I chose My Project - development, then pressed Shift + F9. The Debugger started well, but did not stop at my break points. Why?

Ensure your Gemfile contains this:
gem 'linecache19', '>= 0.5.13', :git => 'https://github.com/robmathews/linecache19 0.5.13.git'
gem 'ruby-debug-base19x', '>= 0.11.30.pre12'
gem 'ruby-debug-ide', '>= 0.4.17.beta17'
maybe for details see that answer

Try using command line debugger: https://github.com/cldwalker/debugger
See also
http://railscasts.com/episodes/54-debugging-with-ruby-debug
http://railscasts.com/episodes/54-debugging-ruby-revised

pry is an interactive Ruby shell (or "REPL"), which can be used as a debugger (best combined with the pry-nav and pry-stack_explorer gems). It has a lot of features which make it much nicer to use than the debugger gem. For general exploration and experimentation with Ruby code, it's also nicer to use than irb.
For more info:
http://pryrepl.org/
https://github.com/pry/pry
After adding pry to your Gemfile and bundling, you can add a "breakpoint" with the following Ruby code: binding.pry

Related

Breakpoint on a different folder in a Rails application doesn't work well

I'm debugging a Rails application that uses gems from my local environment. Is there a way to place a breakpoint in one of the gem's file and have it trigger when I debug my app ?
How can I see the source code of a gem installed on my machine? should help you
If you need to edit the source code of say Devise, you would run:
bundle open devise
This will open the gem in your default text editor, allowing you to put breakpoints wherever needed.
Alternatively to see where it's stored on your machine try:
bundle show devise
You can use gem 'pry-rescue' and gem 'pry-rails' in your gemfile to debbug your code. Just put in your group development like this:
group :development do
gem 'pry-rails'
gem 'pry-rescue'
end
in your code you can put
binding.pry
and after that just run your application and your request will stop where you put the binding.pry

How to I start a rails console with pry turned off?

Sometimes I have reason to want to start the rails console as an irb repl rather than pry (as awesome as pry is). It will default to pry because pry has in the Gemfile. Hows is that done nowadays?
I think there used to be a --irb option when running rails console but that seems to be gone now. I get a deprecation error message when I try it.
More details
If I just run "rails console" it takes me to pry.
If I run "rails console -irb=irb":
$ rails c -irb=irb
--irb option is no longer supported. Invoke `/your/choice/of/ruby script/rails console` instead
Relevent lines from my Gemfile:
gem 'rails', '3.2.18'
gem 'pry-rails'
gem 'pry-plus'
Launching pry when calling rails console or rails c is set up by the pry-rails gem. If you look in the pry-rails issues there is one that describes a solution.
Define the environment variable DISABLE_PRY_RAILS as 1.
So you can call rails console without pry with:
DISABLE_PRY_RAILS=1 rails c
Works in Rails 4: In your application.rb, inside your Application class, drop this puppy in.
# Use the IRB console instead of the Pry one
console do
require 'irb'
config.console = IRB
end
I couldn't take the Pry console anymore. It kept putting my cursor in odd places at unpredictable times. I can't even describe it but if you know what I'm talking about and know the solution, please let me know.
Inspired by the answers above, I added the following to the class definition in application.rb so that Pry is toggleable from the console:
console do
if ENV['IRB']
require 'irb'
config.console = IRB
end
end
You can then run rails c to get a Pry console, and IRB=true rails c to get an IRB console. This is easily modified if you want the inverse. Works in Rails 4 and 5.
For the benefit of anyone who runs into the same problem, this is my (crappy) workaround.
I wrapped the pry gems in Gemfile with this:
...
unless ENV['NOPRY']
gem 'pry-rails'
gem 'pry-plus'
end
...
Then run this from the unix terminal:
NOPRY=true bundle install
NOPRY=true rails console
Not pretty, but gets the job done...
You can also do it once console has already been started via IRB.start method.

Error launching Rails server: undefined method 'configure'

I'm new to rails and working through Hartl's tutorial. Everything was fine until I tried to do the tutorial a second time and created another project trying to use the latest version of rails. When I try to load the rails server from the app folder I get the following error.
$ rails s
=> Booting WEBrick
=> Rails 4.0.4 application starting in development on
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
Exiting
/Users/sierra/Desktop/sample_app_2/config/environments/development.rb:1:in
`<top (required)>': undefined method `configure' for
#<SampleApp2::Application:0x00000101a74610> (NoMethodError)
My Gemfile is directly from the Hartl tutorial:
source 'https://rubygems.org'
ruby '2.1.0'
#ruby-gemset=railstutorial_rails_4_0
gem 'rails', '4.0.4'
group :development, :test do
gem 'sqlite3', '1.3.8'
gem 'rspec-rails', '2.13.1'
end
group :test do
gem 'selenium-webdriver', '2.35.1'
gem 'capybara', '2.1.0'
end
gem 'sass-rails', '4.0.1'
gem 'uglifier', '2.1.1'
gem 'coffee-rails', '4.0.1'
gem 'jquery-rails', '3.0.4'
gem 'turbolinks', '1.1.1'
gem 'jbuilder', '1.0.2'
group :doc do
gem 'sdoc', '0.3.20', require: false
end
group :production do
gem 'rails_12factor', '0.0.2'
end
I resolved it by doing following step.
Step 1: go to Project_Root_Directory/config/environment/development.rb
Change this line
Rails.application.configure do
To
Your_Rails_Application_Folder_name::Application.configure do
For example my rails project folder name is 'Spree_demo' so Your_Rails_Application_Folder_name in the following line:
Your_Rails_Application_Folder_name::Application.configure do
will be replaced as
SpreeDemo::Application.configure do
Note: See underscore in your application folder name it gets removed.
Hope it works for you guys.
First set Ruby version before Rails new
I had the same problem and I tried the answer given and it had no impact.
I even tried changing the name to get rid of the underscore, and it had no impact.
The problem is that you did this:
$ rails new app_name
But your ruby version was probably 2.1.1 or something else. You want to do:
$ rvm 2.0.0
BEFORE you run the new app, and then when you set 2.0.0 in your Gemfile (as Hartl recommends) it falls into place.
I don't know WHY this works, and I hope someone will shed light on it, but I can tell you that this worked better than the answer that is currently in the lead.
That happened to me too. The problem was that I used one version of Rails to create the project. Then I changed the Gemfile to use another version of Rails and the system was using it to scaffold or run the server. Newbie problem!
Using the same version consistently should solve the problem. :-)
I posted a (probably way too long) answer in a similar question: rails - NoMethodError: undefined method `configure' for FirstApp. This thread actually started me on the way to my eventual solution, so I thought I'd post here as well just in case it's helpful to anyone else.
From what I can tell, the problem occurs when the app/config/initializers/development.rb (and production.rb) files are generated for a new project using some newer versions of Rails (I'm not sure in which version it started, I only tested Rails 4.1.4). Mr. Hartl uses Rails 4.0.8 for his tutorial, and that's the highest version I tested in which the new syntax doesn't occur.
In Rails 4.1.4, and maybe some other versions after 4.0.8, the first line in those files is generated as Rails.application.configure.do rather than, using a project called sample_app as an example, SampleApp::Application.configure.do as in 4.0.8.
I'm new to Rails so I don't know why this syntax changed in newer versions. I'm assuming it's intentional and somehow better than the old way. Most likely, Mr. Hartl will take it in to account in future editions of his tutorial that are updated for versions of Rails which include this change.
Until then, see my other answer in the question I mentioned above for a more thorough explanation of how I got around it on Windows 7, but the tl;dr of it is:
Make sure you're using the version of Rails specified for the tutorial (4.0.8) in your local repository/root development directory before you create your new project. Updating your Gemfile after creating the project is still important, but it won't solve this problem if the files themselves were generated with a newer version of Rails. You'll have to go in and edit that line manually in that case, as other users have suggested.
I had this issue when I messed around with my Gemfile. For example I have created the app using rails 4.0.2 or something like that then due to some errors I changed it to 4.1.1 that change cause this exact same problem in both development and production

Which gems do I need to debug a Rails application in RubyMine?

I use Ruby Mine 6.3, ruby 2.1 and Rails 4.0.3.
Which gems need to debug application with Ruby Mine, I tried use these gems:
gem 'debugger'
gem 'debugger-xml'
But application crashed with exit code 127 at breakpoint, and print this:
.rvm/gems/ruby-2.1.0/extensions/x86_64-linux/2.1.0/debugger-1.6.6/ruby_debug.so:
undefined symbol: rb_vm_get_sourceline
UPDATE
I updated RubyMine to 6.3.1, dropped 'debugger', 'debugger-xml' (in Gemfile only 'ruby-debug-ide') and Debugging works!
I advise bunch of "better_errors" and "binding_of_caller".
You can read about this here: https://github.com/charliesome/better_errors
I'm usually use this gems:
https://gist.github.com/MrEmelianenko/11078561
To enhance your console, use pry-rails, better with pry-doc.
To debug in program, like using breakpoint, step in/over, you can use pry-byebug or pry-debugger.
p.s. this is my way to debug rails app in terminal, I'm not sure if it is ok with Ruby Mine. Hope this can help.
I always liked pry. And its very useful with easy to learn screencast
This is my debugging stack for my Ruby 2 & Rails 3/4 apps ;)
group :development do
gem 'pry'
gem 'pry-nav'
gem 'pry-rescue'
gem 'pry-stack_explorer'
gem 'pry-doc'
end
Hope it helps
As you mentioned, you need ruby-debug-ide, but not debugger or debugger-xml.
For Rubymine/IDea + Ruby plugin with Ruby 2.0/2.1, also try the debase gem along with ruby-debug-ide to speed up debugging. Earlier versions had some trouble, but it works well, as of 2014-04-21.
You already found your answer but I'd like to add small notes
Rubymine has an issue with the debugger gem, or probably it's the gems clashing(maybe), rubymine uses fast-debugger (ruby-debug-ide) and I think it doesn't like debugger, old versions suggested adding debugger-xml but from my trials that didn't really work so well, and current version kinda tolerates debugger a bit, but sometimes it freezes during breakpoints.
My teammates use debugger, so it's pushed to the repo, what I do is whenever I need to debug I just delete the debugger line from my Gemfile and wait for rubymine to detect that Gemfile change (about a second or two) and start my debugging session, I only put it back when I check the diff before pushing.

Rails colour highlighting for the Test::Unit/rake command?

When running test/unit using the rake test command from the terminal within a rails 3 project directory, the test result output is not coloured. Hence, it cannot be interpreted at a glance.
Is there a way of getting colourised output for the results, as you can get in rspec?
>rspec --colour
I discovered that redgreen was abandoned years ago, and found this solution which works well and requires no script hacking. The output, however, shows which test is being run in real time. So it is a lot longer than built in test output. It does have nice colors.
http://rubygems.org/gems/turn
In my Gemfile:
group :test do
gem 'turn'
end
Then run:
$ bundle install
$ rake test
The gem 'turn' works great. The caveat is that it doesn't seem to work with Mocha, due to monkey-patching issues. If you are using Mocha, you can use the redgreen gem. See instructions above in the approved answer for this question.
Yes, you can use the redgreen gem. Include it in your gemfile:
group :development, :test do
gem 'redgreen'
end
And that's all you need for ruby 1.8. If you're using 1.9, there's a workaround. add the test-unit gem:
group :development, :test do
gem 'redgreen'
gem 'test-unit', '1.2.3
end
It's not perfect with 1.9 - test-unit seems to run an empty test suite after every rake task or generator call, which is harmless but annoying.
I am working on Rails 5.1 / minitest and I was also searching for a solution to make the reporting color. None of these test::unit solutions are working, so I googled and saw this solution. Just add the following:
# Gemfile
gem 'minitest-reporters'
# test/test_helper.rb
require "minitest/reporters"
Minitest::Reporters.use!
Github: minitest-reporters

Resources