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

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

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

x.succ

Posted on March 3, 2006

x.succ

[ "H", "A", "L" ].collect {|x| puts x.succ }

Prints out:

HAL, plus one letter up, equals IBM. Pretty funny.

Filed under: Ruby No Comments

Dave Thomas on Rails

Posted on November 19, 2005

dave_thomas.jpg

A while ago I ordered a Ruby book entitled, "Programming Ruby by Dave Thomas" off amazon (bookpool was close on price). I read the first few chapters, got hung up on syntax and my interest was peaked.

One week later, a coworker says "Hey! Dave Thomas is speaking at NovaJUG!" (northern virginia java users group). It was uncanny and I had to go. To be fair, the opening act (as he called it) was on PMD Applied, by Tom Copeland. He has written a book on PMD. Essentially what I got out of it was a normalization process for code. No duplicate code, no logical duplication and a Java tool to help detect it all. I think Tom discredited himself a bit (understandably) by feeling like everyone was there for the Ruby bits. This was only mostly true for me. I wanted to see what the Java end of the JavaJUG was like. I enjoyed his presentation although I felt he could have been more confident even if he was just the 'opening band'. Tom knows his stuff for sure.

Dave Thomas got up and explained what Ruby on Rails is, what Ruby is and why he's even attempting to give his talk to a Java Users Group. He explained that his talks are becoming more and more popular at Java seminars. Why? Probably because it's not a 300lbs hammer like Java is. He had a good point about Programmers aren't Programmers anymore. They are "Java Enterprise Programmers with XYZ framework experience". A carpenter with only one tool.

Ok point taken. But onto the gritty details. Where does Ruby not work? Where does it break? How can it be perfect? He did a good job of answering all of these common questions and explaining that Rails works for an estimated 70% of projects and the remaining 30% it might work 50% of the time if you changed the requirements or pushed back on the customers.

A really depressing part of the talk was when he explained that this Java programmer he talked to rewrote an app in Rails at night and in his free time. The XML configuration of his Java app was 400+ lines, just the XML config. Took him 4 months for the Java piece. The same app in Ruby was less than 400 lines of Ruby code and he did it in 4 nights (spare time). Now, obviously if you rewrite something you already know requirements, how the app works, domain experience and all the other stuff you already did. So Dave said "double the time! why not! 8 days!".

I'm still not convinced. So he starts talking about JCP. Said something like, "Take a good idea and put it into a Java committee where a bunch of vendors all influence the direction of a new spec to make the most money. JSF was written so humans can't write it! So you'd have to buy a tool! Sun has killed Java. If you look at C# and Java5, it's a checkbox contest. Generics, check. Autoboxing, check."

Ruby has configuration files that are written in Ruby. No XML (unless you want to). Ruby makes a lot of assumptions to make your life easier. You can override the assumptions but could as easily take the defaults to make it quicker.

Dave then started coding on his mac laptop and we all watched. He used MySQL as a database, YourSQL as a MySQL frontend (to create tables quick), TextMate as a text editor (did Ruby syntax autocompletion a bit) and a standard OSX terminal. He created a database table and with one line of code 'scaffold' had the table on the web with 'show', 'edit', 'destroy' functionality per row. Basic functionality. He had run some scripts to generate the controller (comes with unit test). But what he hadn't done was the killer. He had URL rewriting (Rails does that), he had a simple database mapping (Rails I think) and he had a server running on port 3000 with no jboss or tomcat config deployment etc.

Everyone was excited. I was excited.

He talked about how you can freeze your Ruby interpreter to your vendor/ directory within your development directory. So if a new version of Ruby or Rails comes along you aren't screwed if you try out the new version.

He talked about a free ruby tool called SwitchTower that lets you control versions of Ruby apps (not Ruby only) on deployment. SwitchTower can roll back versions if a bug is found. Looks neat.

He continued on at the end by talking about the future of Ruby. He talked about JRuby. Ruby running a Java VM. You can call java objects from Ruby natively, way cleaner than that JNI crap. He talked about possible verions of Ruby with bytecode like Perl6. It's all very new.

It was a great talk, he graciously signed my book. Dave Thomas is excited, honest, charmingly English (he forced him to pronounce status like us yanks -- Stah-tuss vs Stay-tuss) and approachable. I could tell he's done lots of these talks, he's invested in Ruby, he's selling his book. But that's his job for now. Ruby speaks for itself upfront and I'm just going to have to explore the rest. Dave Thomas and others were also invested in Java. But they aren't throwing it away (maybe some are). His point is to not use Java for everything. Not to use Ruby for everything. Anyone who says one tool to bind them all is a salesman.

Don't use one tool for everything. If I want to quickly parse XML in Java, what do I have to do ... get Xerces, Xalan, a million jars with the right version. XML, Webservices, Unit Tests, AJAX, real object oriented stuff is just built into Ruby. It's new. It's improved. You might like it for a side tool project. Or a Java side-helper.

Don't drop Java he said. Don't force and sell this to your boss. Try it out. Make an app quick and when people ask, "how the hell did you do that?" you say, "oh it was nothing".

What I'm doing.
I'm reading his book. I'm going to try it out. I'm going to see if I can write something quickly and use all these neat features. So far the syntax is weird, I miss my curly braces. But even without a book the commands and scripts are fairly self explanatory. Emerging rails was a cinch under gentoo:

ACCEPT_KEYWORDS="~amd64 ~x86" emerge rails

And then I just mkdir "ruby" and "cd ruby" and "rails myproject". I ran "script/server" and I had a rails server running on port 3000. That was easy. Like 5 minutes.

The rest of it is the kicker I think.

Filed under: Ruby No Comments