Those pesky Xcode windows

When working on an Xcode project, it’s really easy to end up with a dozen windows open.  You did know that you can close all of them and leave just the main project window open, right?

Just option-click in the close “box” (that’s what we used to call it when it was square, is it the “close circle” now?) of any editor window, just not the main project window.  Et voilà — all windows closed.

MySQL CLI: Edit previous query

One of the very useful features of the mysql command line utility is the ability to edit the query you just executed and then execute it again.

Here is a query:

mysql> select now();
| now()               |
| 2010-04-23 15:57:02 |
1 row in set (0.00 sec)

I can now edit this by entering the command “edit” at the mysql prompt:

mysql> edit

The query is opened in an editor (vi for me) and I can edit and save the new query. When I exit the editor, it does NOT display the query. Instead, it just says “edit” and I must now append a final semi-colon before pressing return to execute the new query:

mysql> edit
    -> ;
| The time is: | now()               |
| The time is: | 2010-04-23 16:00:14 |
1 row in set (0.00 sec)

How did I get here?

Sometimes it’s useful to know the stack trace at a certain point in your Ruby code without using the debugger (or throwing an exception). Here’s one way to do it:

puts Kernel.caller.join("\n")

Ruby tip: while becomes any?

In implementing a new feature in a Rails project, I found myself needing to iterate through an array of patterns looking for a match against a particular string.  In a C-based language or Java, you would probably do something like this:

class LoopDemo
  public static void main(String[] args)
    String matchMe = "diamonds";
    String suits[] = {"spades", "clubs", "diamonds", "hearts"};

    boolean found = false;
    int suitIndex = 0;
    while ((!found) && (suitIndex < suits.length))
      found = matchMe.equals(suits[suitIndex++]);

    System.out.println(found ? "Match found" : "Match not found");

Ruby, of course, provides a much more compact way of accomplishing exactly the same thing:

match_me = "diamonds"
suits = ["spades", "clubs", "diamonds", "hearts"]

found = suits.any? {|suit| suit == match_me}

puts found ? "Match found" : "Match not found"

Yes, any? stops when it reaches the first returned true value.

The example is contrived — of course, there are easier ways with Ruby to find out if an array contains a particular string. But imagine that your criteria for “finding” or “matching” something is more complex. You can use this approach with an arbitrary amount of complexity in the block being passed into any? as long as the return value from the block evaluates to true or false.

When upgrading to Rails 3…

Be sure you delete config/initializers/new_rails_defaults.rb. It is no longer necessary (per its own comments) and in fact causes a weird exception if you don’t remove it:

/Users/mharper/Development/Rails/mileage/config/initializers/new_rails_defaults.rb:14: undefined method `generate_best_match=' for ActionDispatch::Routing:Module (NoMethodError)

Recursive grep

I don’t do this often enough to remember, so I figured I’d write it down.  For example, to look for all the places where you include stdio.h in .cpp files below the current directory:

$ grep -r -n --include "*.cpp" '#include <stdio.h>' .

Toll-free Bridged and NULL v. nil

I always wondered if NULL got toll-free bridged to nil.  I figured that NULL is a pointer value of all zeroes and nil is actually a pointer to a special object.  So my hypothesis was they weren’t interchangeable.  I was wrong.

  // Check to see what happens to a toll-free bridged ptr when it gets set to NULL.
  CFStringRef stringRef = NULL;
  NSString *str = (NSString *) stringRef;
  if (str == nil)
    NSLog(@"str is nil");
    NSLog(@"str is NOT nil!!!");

  NSLog(@"The object value of nil is %@", nil);
  NSLog(@"The numeric value of nil is %d", nil);

Yields the following output:

2009-12-04 10:51:58.532 TollFreeBridging[18053:a0f] str is nil
2009-12-04 10:51:58.535 TollFreeBridging[18053:a0f] The object value of nil is (null)
2009-12-04 10:51:58.536 TollFreeBridging[18053:a0f] The numeric value of nil is 0

And then there’s NSNull….

Cheap Mac OS X Tea Timer

Typically, tea needs to brew from 4-5 minutes.  You’re probably smart enough to figure out the rest of what’s going on here:

$ sleep 240; say "Tea's Done"

Edit 25-Nov-2009: I guess there’s no reason I couldn’t make a MacRuby app out of this, huh?

Running installer from cruisecontrol.rb

In the current Mac OS X / Cocoa project I’m working on, we need to be able to run an installer from code.  Naturally, I’ve written tests for this, which work fine on my own machine.  However, when I checked in the code, the test would literally hang at the point where it should be executing the “installer” command.

The continuous integration system we’re using is cruisecontrol.rb which is started at boot time by launchd under a non-root user running on a Mac Mini.  Consequently, when the test is run that fires up the installer command, it’s doing so as a non-root user.

This no workie.

I’m sure there’s some sort of interesting and obscure way to circumvent this, but the general size of things is that you can’t run the installer command unless you’re using sudo or you’re root.  Since running sudo in the above context is kinda obtuse, and since we’re not going to run cc.rb as root, it looks like these particular tests are now out of the automated test suite and into the manual test suite.  At least there’s an explanation now!