How to debug the code to be tested - ruby

When I run an rspec test, I want to get details of the variables in the code that is tested. I tried to use rubymine, but it is complicated to configure. Sublime text 2 is a ruby friendly editor, so I wish someone can recommend some plugins to me.

Unfortunately, I don't know of any Sublime plugins that will do this for you, but...
Check out the pry gem. This will allow you to peek into your code, view instance variables, modify settings, etc. within the middle of an Rspec test.

There is no similar package for Sublime. What you are looking for is a debugger (in fact, RubuyMine integrates the Ruby debugger).
The most common Ruby debugger is the debugger gem. Simply install it, then place a debugger statement where you want to debug the execution and you'll be able to jump in the middle of the running spec execution.

Related

Adding blackboxed libraries to Pry debugging

Is it possible to "blackbox" libraries when using Pry?
I like to walk the stack when I hit a breakpoint, but I really don't care about active-record transactions management and such. In fact, it even makes walking the stack completely useless sometimes.
In javascript, it's easy to add scripts to an ignore list and they are just skipped during debugging. Is there a way to achieve a similar behavior with Pry?
Additionnal details
What i mean by stack walk, is using "up" to move to the calling source code line which moves the source code marker to the previous stack frame.
I want to ignore frames that are outside my own code, like ActiveRecord and most third-party gems. I don't mind using either a blacklist or a whitelist.
Bundle gives me these gem versions:
Using byebug 9.0.6
Using pry 0.12.2
Using pry-byebug 3.4.3
The requested functionality does not exist in pry-byebug. You can add your +1 (or write some code) on the following GitHub issue for pry-byebug:
My question/suggestion is that if there's a way to filter or skip over external library, or a setting to step into the next line that belongs to a script within the current application. For example, step into a method call will skip over any Rails's internal script or any currently-used gem and stop at the next line of the file that's inside the application.
As well as this GitHub issue:
I think it would be super useful to have a command that lets you run until you hit the next line of non-Rails/non-gem code.
As the original issue has been open for nearly six years, I think your best bet will be to help build the feature vs. adding a +1 on the existing issues.
The author of pry-byebug also offers this workaround in another SO answer to a very similar question:
you need to manually set breakpoints to jump from controller to view and the other way around

In IntelliJ IDEA, can I re-order watch expressions?

After doing a quick search, I only found the main documentation that tells me how to add, edit, and remove them, but not how to move them around. I have tried dragging them around, but that doesn't seem to change anything.
I want to do this so I can see related data together but more efficient debugging. I just don't necessarily think or see what I need to debug all at once.
P.S. I'm using this with a Ruby on Rails project, with the Ruby plug-in installed, and RVM Ruby-Rails Gemset chosen, and a Rails module added to the project, running a Test Unit test.
Just found the Up and Down icons at the bottom of the window. I guess I just wanted the dragging to work =/.

How to traverse through Source code only using debugger - Rails 3.2.13

In my Rails app, using Ruby 2.0.0-p0 and Rails 3.2.13, I also use the gem debugger for debugging purpose. If I have to debug an area in my source code, for e.g. a controller method, I will put debugger inbetween and when I execute the action, it will go to the source code and then travserse through the framework code a lot and that really mess-up the normal debugging. I can see many commands through help, but don't know how to handle with that.
I just want to traverse through the source code only..
Please help me to have a solution. Thanks :)-
Use byebug instead. It was created to overcome debugger's problems with ruby-2.0.0, like the one that makes it "step" when you use command "next", unexpectedly leading you to framework sources.
Disclaimer: I'm byebug's author

mirroring Terminal with gets Ruby

There may be a good gem out there for this, but I've run out of Google.
I'm creating a command line interface with a basic gets each inputed command. I also want to have basic terminal features like hitting the up arrow and having it cycle the history. Are there gems which mimic this or is there a simple way to implement it?
Not to self-promote, but my friend and I wrote a project that is mostly functional with this goal. Check it out here: https://github.com/jamez01/arsh
It's also a packaged gem that you can install.
Some time ago I did a "smart telnet" using "Readline". I even enabled autocompletion.
It has all the cool features like up down arrow, autocompletion, ...
Take a look at it

How to incorporate Interactive Ruby into my development process?

I am trying to find a better way to integrate IRB with my normal ruby devleopment. Currently I rarely use IRB with my code. I only use it to verify syntax or to try something small.
I know I can load my own code into ruby as a
require 'mycode'
but this usually doesn't mesh with my programming style. Sometimes the variables I want to examine are out of scope or inside of a loop. Is there an easy way to fire up my script and freeze at a certain point inside of IRB? I guess I'm looking for an easier way to debug my ruby code without breaking my F5(compile) key.
Maybe a more experienced ruby developer can share with me a more streamlined method of development.
Install the ruby-debug gem. Of course, require it inside your app (only in development/test mode). Now you can write 'debugger' where you want to stop execution.
Once your app stop at your breakpoint, you can type 'help' to know about all commands. One of them is 'irb'. It starts an IRB session in which you have access to all methods in your current context.
I personally mostly use p (print), eval, v i (instance vars) and v l (local vars). Of course, n for next and c for continue.
The command to step out of a given block/method never worked for me though. I never investigated why :-)
I don't tend to use irb directly that frequently, as I tend to be inside rails and so use script/console a bunch, but I do like using the ruby debugger (Ruby Debug gem). It lets you set a breakpoint basically and then step through your code line by line.
Here's a screencast about it that I haven't actually watched, but a quick search pulled it up, and it could be useful:
http://brian.maybeyoureinsane.net/blog/2007/05/07/ruby-debug-basics-screencast/
For Ruby development in Eclipse: there's a much improved version of RDT (ruby development tools) available now. To install it directly in Eclipse, click Help > Software Updates > Find and Install > Search for new features radio button > next > new remote Site > Name = Ruby and URL = http://update.aptana.com/update/rdt/3.2/
Another Ruby plugin is the shiny new Eclipse DLTK (dynamic languages toolkit).
DLTK stable release 1.0.M5 just came out a few days ago.
Here are some useful installation tips.
I just use rdebug to debug any of my ruby or RoR code.
If you're willing to use an IDE for debugging, I know Eclipse (via the Ruby Development Tools) has a relatively straightforward interface. If you're doing rails then there's a specific build of eclipse called RadRails which may also help (though I haven't used it for debugging)

Resources