SQUARISM addicted to pixels

Fixing IRB autocompletion on OSX

Posted on July 10, 2010

Using macports version of ruby (and irb), I noticed that the lovely and wonderful tab autocompletion wasn't working:

$ irb --simple-prompt
>> "foo".cap (The tab key! It does nothing!)

So you can see that I was hitting tab expecting to see .cap resolve to .capitalize but it did nothing. I'm not quite sure if this has been broken for a long time or forever. On Linux, irb seems to just work. So I found a fix out on the 'tubes.

sudo gem install bond
$ cat ~/.irbrc
require 'rubygems';
require 'bond';
Bond.start

And now the tab is delicious.

>> "foo".cap [tab]
>> "foo.capitalize
=> "Foo"

Now while you're at it, might as well get the color going with wirble:
sudo gem install wirble
Add some more lines to .irbrc. Now our .irbrc looks like this:
$ cat ~/.irbrc
require 'rubygems'
require 'wirble'
require 'bond'
Wirble.init
Wirble.colorize
Bond.start

Irb will have color and [tab] now which you can see in the screenshot right there. The colors can be customized in wirble but it seems to be limited to ANSI terminal colors. I wish the terminal would support RGB #html_codes, you could really go crazy and port some of the awesome Textmate themes over to the irb term.

Filed under: Ruby 1 Comment

IRB return trick

Posted on July 9, 2010

Irb, like ruby and many other languages, returns things to the left. When you say:

x = 5

5 gets sent to x and x gets sent to the console or to the bit bucket if nothing is watching for it. Irb with Ruby will tell you this explicitly which is great for debugging and developing.

>> x = 5
=> 5

But sometimes it's annoying. Like when you have a massive array of objects and you're iterating through them. A quick trick is to add another meaningless return on the end like this:

>> x = 5; 1
=> 1

If x=5 was a huge database result set or a large array, it'd be hard to read. But 1 is easy to ignore.

Filed under: Ruby No Comments

Watchr Unit Tests + Growl + Doomguy

Posted on May 23, 2010


I read this post about setting up autotest with growl. The little doomguy was a nice effect and got a literal lol out of me (llol?). However, the post was from 2007 and apparently autotest has some problems with 1.9. I'm not running 1.9 but on the eve of rails3, I probably will be. Ok, what's cooler than autotest? Apparently not much, three hits on google.

Install a gem

I found Watchr. It's not too hard to set up. sudo gem install watchr to get the gem. If you're on *nix, grab the filesystem event gem: sudo gem install rev. Now we have watchr up but we need a config.

Create the watchr config

The docs tell you to edit a config/watchr.rb file. Mine came from the examples on their wiki. It worked out of the box. It's posted here. It's a basic rails testing config that I haven't had to edit. It's very readable if you need to futz with it.

Create a test case

Ok so watchr just runs `rake test` which you can do in your rails app root. But perhaps you don't have any test cases created. The generators will create them for you. But assuming you don't have one, create a file named [rails app root]/test/functional/hello_test.rb.

require 'test_helper'
 
class HelloTest < ActiveSupport::TestCase
  def test_hello
    s = "Hello"
    assert_equal("Hello", s)
  end
 
end

Obviously this is a crappy assertion.

Get Growlnotify

Obviously, you'll need growl installed. But you also need the growlnotify binary. It's good to have anyway (I use it in Automator). I didn't have it by default on two of my machines so I assume that the normal Growl install doesn't install it. It's on the Growl 1.2 dmg under extras. You just have to copy it to /usr/local/bin. Download the growl dmg, open the dmg. You don't need to reinstall growl.
sudo cp /Volumes/Growl-1.2/Extras/growlnotify/growlnotify /usr/local/bin

You can test it with:
growlnotify -t "Title" -m "Message"

Make doomguy happen

Create a directory for the growl icons: mkdir ~/.watchr_images.
Copy these two doomguy faces to ~/.watchr_images/. On mac, hit Shift+Cmd+G and go to the hidden folder.

Now we should have two pngs in our hidden folder.

The watchr.rb file references these images. If you want to put it somewhere else (like maybe under ~/Pictures), you can change the .rb.

Ok Go!

Launch with: watchr config/watchr.rb from your rails app root. Watchr should be in your $PATH because you installed the gem. You should have a blank screen now where watchr is waiting.

When you save a file, watchr will fire. Depending on the file you saved, a different test will run. If you save a controller, for example, the functional test for that controller will fire. If you save a model object, a unit test for that model object will fire. If you want to force a whole test suite to run hit Ctrl+\ in the watchr window.

When things pass:

When things fail:

The growl display setting is "Music Video". I have it popping up on my 3rd monitor which is less distracting but still visible. However if you put it on your main monitor, the overlay won't interfere with mouse clicks, which is nice. It makes for a killer setup where I don't have to test my app with a browser or with contrived/non-automated tests. Now I just need to write more tests.

Filed under: Rails No Comments

iPhone Developer Activation Problem

Posted on May 10, 2010


I went to renew my membership on my iPhone developer account and although the membership is $99/year, my expiration date was only extended out for 3 months. I opened a ticket and it took about a month or two to get this thing resolved. I had to send screenshots, steps and evidence that I was doing the right thing (ok, fine, tier 1 support). Eventually I got a person that had a trick up their sleeve but didn't quite get it right. I'm blogging this in case this is happening to someone else.

First, the symptoms. Let's say your dev account is about to expire in January 2010. You want to renew for another year. You add a year subscription to your cart, pay for it. You'd expect your new expire date to be Jan-2011. But in your profile and in the "thanks for ordering" activation page it says March 2010 or something else wrong. Support send me activation links basically to the same page that wouldn't activate any differently. Each time I checked out, it would say March 2010.

The trick was to click on renew membership (as if paying again), click the checkbox to select which membership you want to buy ($99 year) and hit continue. Now you're on a "Review your purchase" page. Click continue here. Finally, it says "Proceed to your country's Apple Online Store to purchase" and has an add to cart button. Stop here.

Click the following link: http://developer.apple.com/iphone/enroll/activate.action. It should bring you to a page with a single textbox for your activation code (the one you've been trying to use all along). Put this in and hit continue. Your expire date should be correct now. If not, try using a link formatted like this:
http://developer.apple.com/iphone/enroll/activate.action?activationCode=YOURCODEHERE

Seems like they have some bug in the dev store. Please comment if this helped you so it's documented.

Filed under: Mac, ObjC No Comments

RMagic, ImageMagick, Snow Leopard and Ruby 1.9

Posted on March 8, 2010

I ran into a problem that kept me busy over the weekend. It seems that rmagick (which is requires for the awesome sparklines project will compile and run on Snow Leopard, however it has a critical bug when trying to use it. The script/server process will crash with a "Trace/BPT trap" error. The logs won't say anything and the hell if anything is going to appear on the browser. WTF.

Here's my stack on my Mac: (do not use this stack, this is just for informational purposes only)

  • ruby19 installed via macports: sudo install ruby19
  • regular rubygems installed
  • ImageMagick 6.6.0-0 installed via macports
  • A bunch of github gems installed but not firing on the controller method that crashes

So I started over on a CentOS 5.4 VM just to try out my stack and it worked. Except, I used ruby 1.8.7 compiled via source. It's a really lengthy process and it'll be part of my README if I ever get this project done. On CentOS you have to compile and install ImageMagick yourself because yum has a really old version. There's a ton of devel dependencies too. If anyone needs the instructions, leave a comment and I'll copy/paste from my README.

So I suspected that ruby 1.9 was causing problems my mac laptop and that turned out to be true. I did a sudo port uninstall ruby19 and then a sudo port install ruby. I had to reinstall all the gems again but it wasn't too bad. If port gives you a ln error, just exit your shell and open a new one.

So RMagick, ImageMagick and ruby 1.9 do not play together.

Filed under: Mac, Ruby No Comments

Arduino command protocol

Posted on February 28, 2010

Here's my IRC Arduino Bot. It uses a regular Arduino 328 and an Ethernet Shield both from sparkfun. As for software, I'm using the Ethernet2 library (see my previous post about this), the WString library and a homerolled IRC protocol parser. The breadboard's power is connected to arbitrary pin 5 and some resistors to keep the LED from burning out.

arduino_irc_light

Basically, my bot joins an IRC channel and then listens for PRIVMSG commands starting with a password. It takes those commands and controls an LED. For example, I'd send this privately to the Arduino:
command password LEDON

And then the red LED comes on. I tell it "LEDOFF" and it turns off. Ok, it's not a new RFC spec worthy of IEEE recognition and international adoption. But it got the job done in a human-readable manner. Previously on my facebook status light project, I had done much of the processing on my laptop and only send hex codes to the Arduino to light up LEDs. The difference now is that the Arduino is doing the processing and no computer is needed.

While I was working on this little project, I had the bot join the channel and announce itself.
irc_log

At one point, I was working on code and then my bot would disconnect. I checked the serial monitor and the server seemed to drop me after a few minutes. The channel would say that I timed out. I realized that I wasn't responding to the PING from the server. So I threw in some code that checks for anything from the server that starts with "PING :". I then respond with "PONG". I remember seeing PING?/PONG! messages in mIRC back in the day. Now it makes sense why mIRC was doing that in the console window.

It works great and I was excited about how much this little board could do in 14KB. And then I kept testing it. After about 7 or 8 "turn on" and "turn off" commands, the Arduino wouldn't do anything anymore. It's like it just froze. If I typed 5 commands, it'd stay connected for a long time. But every time I'd send it 7 to 8 commands, it would lock up. And by lock up, I mean the commands wouldn't work anymore and it would time out from the server. WTF. So close!

So I figure that it's something to do with pointers and memory. I really don't have a solid grasp on pointers and C. I got a lot of this working by iterative experimentation over many days. So I was looking for a better way to send human readable commands to my bot. By human readable I mean something that works like a unix command "command arg1 arg2". Of course this human readable bit introduces strings which is tricky enough in C (for me) and even worse on the Arduino. I figured this was a problem that someone smarter than me had solved.

I found a library called Messenger. It's pretty simple to install, just throw it in your ~/Documents/Arduino/libraries folder on Mac and um ... the equivalent on Windows? There are examples in the Messenger folder that you can checkout. HOWEVER the whole point of me posting this big long thing is the following.

The example checkString really threw me for a loop. It did exactly what I need it do to in a much cleaner way. I uploaded to the Arduino and then broke out to a shell.

$ screen /dev/tty.usbserial-A9005bCr 115200

Substitute your virtual usb device for the /dev/tty path. Note that the sketch uses 115k serial speed. You won't see anything when you type but if you hit "enter" (to clear the buffer) "on[enter]" in screen LED 13 will turn on. Type "off[enter]" ([enter] means the enter key) and it will turn off. Great! Exactly what I need. But then I tried typing "on" then "off" then "muffins" then "on" and the light stayed off. Any garbage gets the Arduino stuck like my sketch. Ok, is what I'm trying to do impossible or is this just coincidence?

I modified the checkString example to look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// This example demonstrates Messenger's checkString method
// It turns on the LED attached to pin 13 if it receives "on"
// It turns it off if it receives "off"
 
 
#include <messenger .h>
 
 
// Instantiate Messenger object with the message function and the default separator 
// (the space character)
Messenger message = Messenger(); 
 
 
// Define messenger function
void messageCompleted() {
  // This loop will echo each element of the message separately
  while ( message.available() ) {
    if ( message.checkString("on") ) {
      digitalWrite(13,HIGH);
    } else if ( message.checkString("off") ) {
      digitalWrite(13,LOW);
    } else {       // ADD THIS
      break;     // ADD THIS
    }                // ADD THIS
  }
 
 
}
 
void setup() {
  // Initiate Serial Communication
  Serial.begin(115200); 
  message.attach(messageCompleted);
 
  pinMode(13,OUTPUT);
 
}
 
void loop() {
 
  // The following line is the most effective way of 
  // feeding the serial data to Messenger
  while ( Serial.available() ) message.process( Serial.read() );
 
 
}
</messenger>

I added the break and it's able to deal with garbage. I tested more than 20 commands with banging on the keyboard in between and it seems pretty solid. Now I just need to integrate this with my IRC bot and I might have something that can stay online for a while.

By the way, after you use screen to connect to a serial port, hit "Ctrl+A, k" to kill the window and break out of screen.

Update: People have asked for the code. It's posted after the break.

Dataloading ftw

Posted on November 12, 2009

Man. I need to finish this ruby project I started. I've had this RSS loader running on a box since 7/10/2009 and:

mysql> select count(*) from jobs;
+----------+
|  1414595 |
+----------+

mysql> select count(*) from entries;
+----------+
|   184149 |
+----------+

mysql> select count(*) from batches;
+----------+
|    35170 |
+----------+

1.4 millions jobs with 184k RSS posts. Eep. I need to finish the analyzer part and turn this off. It's only a 200mb DB right now but my procrastination could cause /var to fill up. Heh.

Filed under: Ruby 1 Comment

Rails on Leopard via davesouth.org

Posted on July 18, 2009

Started a side project on rails, many guides on how to upgrade a Mac. Every OS has it's weirdness. Ubuntu wants to control ruby and the gems. OSX has an older version included. Macports sometimes doesn't add the PATH. It was all really muddy.

Then I found a page by Dave South. The article's post date is old but at the bottom you'll see he keeps it updated. Thanks Dave!

Filed under: Ruby No Comments

Stanford iPhone class on iTunes U

Posted on May 2, 2009

cs193p
Going through the CS193P Stanford iTunes U class as if I'm taking it. It was done in April but I'm going to use the assignment due dates as deadlines for May. If it's due on April 9, then it's due on May 9 for me. So far the content is good. The introductory stuff I got from the book&fail method but it's nice hearing someone explain it.

Assignment 1B is a good API doc researching exercise.

Filed under: ObjC No Comments

Synergy2 mouse centering

Posted on October 4, 2007

Synergy is a great tool for sharing multiple screens (reverse KVM) but for some reason lately I've noticed that if I'm physically on the center screen, then the mouse recenters occasionally. It's pretty annoying.

I turned on debug log and looked in ~/Library/Logs/synergyd.log. I found this:

2007-10-04 22:12:11.894 Synergyd[337] DEBUG: dropped bogus motion -840,-526

I searched sourceforge for that string and found code that essentially centers the mouse when it detects motion on the primary screen. There doesn't seem to be an option to control the behavior, so short of me making a patch, I don't see a solution.

UPDATE: I found a workaround, simply move your mouse over to the display that it being buggy and the if() condition doesn't fire off. So in my case, my mac connects to my PC's keyboard and mouse. I move the PC's mouse over to the Mac screen and the Mac stops acting weird (the centering problem).

Filed under: Development, Mac No Comments