Practical Ruby Projects

My Ruby book Practical Ruby Projects: Ideas for the Eclectic Programmer is in stores!


(Practical Ruby Projects)

I've been writing for the past year, and I'm crazy about the result. This is the sort of book that I love to read, and hopefully that shines through.

Practical Ruby Projects is based on a few ideas:

  • "Hands on" is always better.
  • You can cover more if you trust your reader to know the basics.
  • Programming can be creative and exciting.
  • Getting started is hard. Anything that makes that easy is good.

This book covers a lot of projects (all done in Ruby, of course!):

  • Animation
  • Music
  • Simulation
  • Turn Based Startegy Games
  • RubyCocoa
  • Genetic Algorithms
  • Implementing Lisp
  • Parsing

PS- All the source code is available under the MIT license!

posted on: 01/04/2008 | path: /tech

Startup School in Sunny CA

Some background. My friend Adam was selected as one of this year's Y-Combinator Winter Founders. For the past few months he and his partner Josh have been building a startup company named "Tsumobi."

Thanks to Adam, I flew out this past weekend to attend Y-Combinator's Startup School. All in all, it was a totally unique experience.


(The auditorium was packed.)

The speakers were really good. Almost everyone had some useful insight. My favorites were Mitch Kapor (founder of Lotus, and no slouch since then either) and Greg McAdoo Partner (of Sequoia Capita). And as I understand, Paul Graham's presentation was something of a novelty because he used slides.

The audience was a very cool collection of geeks. Unfortunately, there were a few individuals that would mob each speaker as he or she tried to leave the stage. It wasn't clear to me if they were looking for investors or new best friends, but some of the speakers got a little wild eyed trying to escape.


(Thankfully RoboCop was there to escort the speakers off the premise.)

That said, I meet some really sharp other attendees, including several folks I'd previously only met electronically. I even ran into my friend Davy who was down from Washington for the weekend.


(Trendy Nightclub or Geek Social?)

I managed to pick up a sore throat on the plane and by Saturday night I was so exhausted that I feel asleep on the couch in the middle of a networking event. Not my finest moment. But it was definitely worth it.


(Heck of a view looking down from the hills toward the city.)

Mountain View itself was kind of awful, but the warm breezes of California were amazing. You know, I'm not sure if I'll end up with my own startup company at some point, but today it actually feels doable.

I'd say that's the real takeaway from Startup School.

posted on: 03/29/2007 | path: /tech

Building Rubinius

Have I mentioned how cool I think Rubinius is? It's totally awesome. Here's how I got it building on my Mac:

# Make sure ~/bin is in your path before doing this
cd ~/bin
wget http://www.opensource.apple.com/darwinsource/Current/ruby-22.2.2/ruby/mdoc2man.rb
chmod a+x mdoc2man.rb

# You'll need DarwinPorts/MacPorts installed

sudo port install subversion

sudo port install readline
sudo port install ruby

sudo port install pkgconfig
sudo port install glib2

# Now use Ruby Gems

# Had to download package manually for some reason
sudo gem install rake
sudo gem install RubyInline
sudo gem install rspec
sudo gem install ruby2ruby (needed by RSpec)

# Check out Rubinius
svn co http://code.fallingsnow.net/svn/rubinius/trunk

cd trunk

cd externals/syd-parser
rake package
cd pkg
sudo gem install sydparse-1.2.2.gem
cd ../../..

rake build:all

I'm pretty sure that's what I did anyways. =)

posted on: 02/06/2007 | path: /tech

Digging the Paste Way

I'm using Python and Pylons for a project at work, and I deployed my application for the first time today.

I've mostly enjoyed using Pylons. Although it does suffer from Too Many Files Syndrome (which I'll try to talk about in a later post) and the javascript/AJAX stuff feels cobbled together and undocumented compared to other frameworks like Rails or Seaside.

It's basically just another MVC web framework, but it works and I like its philosophy of loosely coupled best of breed libraries. In particular, it's been great how easy it was to use SQLAlchemy instead of SQLObject.

SQLAlchemy is insanely powerful (though not as user friendly as I'd hoped) and is a great fit for our databases which don't necessarily conform to the schemas demanded by ActiveRecord or SQLObject. And If you take the time to build your model classes carefully, you can hide most of the complexity.

While I'm at it, I also out to give a shout ought to jQuery which I've been using alongside the Prototype Javascript Library that ships with Pylons. jQuery is totally awesome. I love it's markup oriented behavior concept. It's really a great way to think about writing Javascript.

Anyways, all that was just so I could mention how frickin cool Paste based web deployment is (Pylons uses Paste). Your entire web app gets packed up as a Python egg, which can then be installed on any machine (multiple versions can be installed too, and library version dependencies are handled by the Egg system so no need to worry about upgraded versions preventing rollbacks).

In order to launch the web app, all you need to do is have the Egg installed, put together a quick config file (specifying things like port, but also application specific things like database connection or data directories), and use the 'paster' command to launch it. I'm pretty sure this is "good stuff."

Update: Ian Bicking, Python web super hero, does a quick comparison between Turbo Gears and Pylons.

posted on: 02/06/2007 | path: /tech

One Step Back

Does anyone know why the recently released Core Doc for Ruby 1.8.5 contains a flood of classes from the Standard Library (which is also documented separately)?

It's resulted in a serious loss of usability. Now when I need documentation on the core classes that really matter (classes like Array, Class, Enumerable, File, Hash, IO, Regexp, String, and Thread), I have to wade through a mess of DRb, Generators, IXMLDOM, Net, RDoc, REXML, RI, RSS, Resolv, Rinda, RubyToken, SM, SOAP, Test, WEBrick, WSDL, XSD, YAML, and Zlib.

All of which are great (except SOAP and WSDL -- ugh ;-). I just liked it better when they were documented separately in the Standard Library docs.

Some kind of fancy AJAX search interface to the RDocs might take the edge off here, but I'd still like to be able to browse just the most important classes. I hope we don't lose that ability forever.

For some perspective, here's the number of classes (+/- one or two) documented in the Core RDoc for the past three releases of Ruby.

1.8.3: 167
1.8.4: 181
1.8.5: 1325

Yikes!

posted on: 12/11/2006 | path: /tech

Mongrel Comet Update

If you've been looking to do Comet work using Ruby and Mongrel, I might have some good news.

I'd previously reported that my code to add Comet support to Mongrel required a seperate URL handler at a different mount point for each persistent connection because of some mysterious threading issue.

Looks like I may have been misunderstanding what was going on. In fact, Joseph McDonald reports that it works just fine... provided you remember to respect your browsers allowed number of connections. I'm not sure why I'd forgotten about this during testing. From Wikipedia:

Browsers pose a further limitation: section 8.1.4 of the HTTP 1.1 spec states that "A single-user client SHOULD NOT maintain more than 2 connections with any server or proxy", and this recommendation is in fact followed by the most common browsers, including Internet Explorer and Firefox. Holding one connection open for HTTP streaming tends to create performance issues for AJAX applications, such as inability to kick off a new user-initiated data request while a series of images are being loaded. This is often worked around by creating a distinct hostname for push connections (even with a single physical server).

posted on: 12/10/2006 | path: /tech

Two Down

The second chapter of my upcoming Ruby book is done! Man it feels good. But now I have to decide which chapter to write next.

I haven't had as much time to blog as of late, but I'm going to take this opportunity to give a shout out to two great Ruby tools I've been using at work lately.

  • Syntax - This module apparently does a lot, but I've mostly been subclassing it's Syntax::Tokenizer. With a little glue code these tokenizers can be easily used along side...
  • RACC - In the style of YACC, RACC is LALR(1) parser generator, but you get to use Ruby (and it generates Ruby) instead of C.

I'm always amazed at how many low profile, but very cool libraries there are for Ruby.

posted on: 09/26/2006 | path: /tech

Making Ruby into PHP

Sometimes a web framework is just over kill. Maybe it's a one off dynamic page, maybe you don't want the memory footprint of a whole framework running for only a small component of your whole site.

mod_ruby and eruby can get you a lot of what PHP gives you if you're into that and don't mind the setup. There's the added perk that you won't have to write PHP.

But what if you want something similar, but quick and simple and you're willing to use CGI? Here's a neat little trick.

First save this text into a file called "rubyhp.rb" in your cgi-bin directory.

require 'erb'
require 'cgi'

cgi = CGI.new
print "Content-type: text/html\n\n"
print ERB.new(DATA.read).result(binding)

Leave this file unexecutable, so your web server won't serve it. Then create your PHP style Ruby file like this.

#!/usr/bin/ruby                                                                 
require 'rubyhp'
__END__
<html>
 <body>
  <% cgi.params.each do |key, value| %>
   <%= key %>: <%= value %><br />
  <% end %>
  <% if cgi.params.empty? %>
   Sorry, please enter some cgi parameters. How about "?foo=baz"?
  <% end %>
 </body>
</html>

Name this file "test.rb" and save it in your same cgi-bin directory. Make it executable and you're done. You can write whatever erb you want after the __END__ line, without needing to worry about setting up anything fancy. And you have access to a parsed CGI object through the variable 'cgi'.

Does CGI still have a place in modern web work? I'm don't know. But I do still use it for quick dynamic pages on cyll.org like the Computer Science Bandname Generator. This little trick makes life a little easier for me when I do.

posted on: 08/26/2006 | path: /tech