Quick debugger tip in Ruby

The ruby-debug gem is great for debugging but here’s something you might have missed: you can call it with a condition. That way it only breaks execution when a given condition is met.

[3, 2, 15, nil, 5].collect do |id|

  debugger if id.nil?



That way, it will only stop if one of the array items is nil. This is a huge time-saver. Use it frequently.

Netbeans 6.5 Code folding and test files

I don’t know how many people will find this useful, but it made me lose some time, so that’s why I’m sharing.
I use Netbeans code folding and the Shoulda plugin by Thoughtbot. Today I ran into this strange situation where Netbeans wasn’t showing the folds for one of my files, but it did for the others.
The reason why no folds were appearing in one of the files was that its name didn’t end in ‘_test.rb’. I changed it and the ‘context’ and ‘should’ code folds appeared.
That was pretty annoying, but good thing I found out.

5 Mind-blowing firebug secrets

I spent my whole workday using Firebug, so discovering these was a huge productivity improvement for my workflow. Enjoy:

The power of Ctrl-Z
If you use Firebug console with the larger command line and you change tabs, the content of what you had typed gets erased. In the smaller command line you get what you previously entered by hitting the up arraw. In the larger command line this doesn’t work, but if you hit Ctrl-Z you’ll be able to go back through the history of what you typed. Pretty Awesome. You can even close the tab or open the console in a new one and it will still remember.
Programmatic Debugger
Being able to place breakpoints using the debugger in the script tab is pretty awesome, but if you need more flexibility (say, stop everytime a loop is run) you can use the debugger keyword inside your JS code and it will break every time it’s called, then you only have to hit play to resume until the next breakpoint
Legibility in the Console
If, for example, you have a long chunk of HTML code that you are appending to a node, it might be easier to indent it, but because it’s wraped in quotes you’ll get an “unterminated string literal” error. A solution to this is using backslashes (“\”) to indicate a line break. Just be sure to not leave any spaces to the right of the backslash.

Remember also, that you can use regular JS comments (“//”, “/**/”) in the console. This can be useful for quick toggling of certain lines


Docking to the Right
There is a great Firefox addon called Widerbug which allows you to dock Firebug to the right of your screen. I know it’s been around for some time, but I only found about it today, so that’s why I’m including it. The only problem I have with it is that once you select the Firefox tab where Widerbug is running it squishes the other tabs to the left, and I find it kind of annoying, but it’s a great way to use your widescreen monitor’s real estate
Messages From Your Code
Do yourself a favor and stop using the alert() call to debug your app, if you still are. A much more legible way is using console.info(). There are other varieties like console.log(), console.error(), etc., but I like the blue “i” this one shows.

Also, remember you can insert as many things as you want in a single message, so instead of doing this

console.info("My array has " + arr.length + " items")//notice the spaces

You should be doing this:

console.info("My array has", arr.length, "items", "look:", arr)


Happy coding, and tell me if you’ve found these useful.

Note: As of this writing I’m using Firebug 1.3.0b7

Bonus tip: Install the Monaco font, it’s so much nicer and Courier New.

jQuery (QUnit): sharing variables between tests in different modules

I went crazy for a few days trying to find out how to share variables between my tests in QUnit. If anybody knows of a better way, please share. This is an example of how to share the variable $phrases between two tests in two different modules:

$(document).ready(function () {
  //variables you want to share.
  var $phrases = $("#phrases li")

  test("trying out text():", function() {
    same($phrases.eq(0).text(), "myText");
  test("trying out contents()", function() {
    same($phrases.eq(2).contents(), "<#text></#text>")

jQuery: $.extend() and $.fn.extend() confusion

There is a significant difference between using .extend() with one argument and doing it with two or more:

When .extend() receives a single object, it adds the methods defined in it to either the jQuery or the jQuery.fn (also called jQuery.prototype and $.fn) objects.

As a general rule, you should extend the jQuery object for functions and the jQuery.fn object for methods. A function, as opposed to a method, is not accessed directly from the DOM.

Notice the different way of calling a method when extending the jQuery.fn or jQuery objects. The receiver (what’s to left of the period) changes.

myMethod: function(){...}

myMethod2: function(){...}

When .extend() receives two or more objects, it takes the first object and adds to it the methods and variables defined in the other objects. See an example:

defaults = { size: 3 };
options = { height: 6 };
var opts = $.extend(defaults, options)
// 'defaults' receives the methods and variables defined in 'options'
// opts == defaults == { size: 3, height: 6 }
// options == { height: 6 };

If the first object is empty, it will add the methods and variables in a new object. This is useful when we want to group the methods defined in several objects without modifying any of them:

<pre>var opts = $.extend( {}, defaults, options)
// 'opts' gets all methods and variables defined in 'defaults' and 'options',
// neither of them get modified.
// opts == { size: 3, height: 6 }
// defaults == { size: 3 };
// options == { height: 6 };

Attribute_fu: has_many_without_association_option’: stack level too deep

If you’re getthing this error, it’s probably because you have a /stable directory that got created while you were trying to install the plugin. So check that you only have /vendor/plugins/attribute_fu and not vendor/plugins/stable.

Also, if you want to install attribute_fu but using piston doesn’t work, try this:

svn checkout http://svn.jamesgolick.com/attribute_fu/tags/stable vendor/plugins/attribute_fu

Happy nesting!

RESOLVED: ActiveRecord::StatementInvalid: SQlite3::SQLException: SQL logic error or missing database

1) Delete any fixtures located in test/fixtures.
2) Solved.