Ruby Gets Possessive

Posted by code_monkey_steve on May 6, 2010 May 6

I debated whether this code snippet was significant enough to bother blogging about, but it is useful if for no other reason that as an example of one of the great features of Ruby: the ability to reopen any class, even a standard class, and add methods.

In this case, we needed a method to apply the English rules for the possessive apostrophy to a string, e.g. a person’s name. I looked-up the rules on Wikipedia and coded up the following three-liner:

{% highlight ruby }
class String
def possessive
str = self + “’”
str += ‘s’ unless %r{(s|se|z|ze|ce|x|xe)$}i.match(self)
endhighlight %}

Now in my site I can do things like:

{% highlight ruby }
“Steve”.possessive + " Profile" == “Steve’s Profile”
“Alex”.possessive + " Profile" == “Alex’ Profile”
endhighlight %}

(Cross-posted to the Conceivian Blog)

comments | Tags: ruby

Swinging from Tree to Tree

Posted by code_monkey_steve on Feb 26, 2010 Feb 26

Last time I updated this blog it was the night before starting a new job, and on the eve of starting yet-another new job, I thought it would be a good time to recap.

The Old

At the beginning of the year I started work at Sponsaurus, a little sports-sponsorship management start-up working out of the StartPad offices. The idea was good, the people were great, and building a site from scratch using the latest-and-greatest toys was addictively fun. Things that were “20 pounds of awesome in a 10 pound bag” :

  • CEO Bruce P. Henry, and co-developer and fellow Kashless alum John Postlethwait. Working with them was always great fun, and I would happily do so again anytime.
  • MongoDB, as provided by MongoHQ, and the MongoMapper ODM. I’ve raved about Mongo before, but it was the first time I’d used it in production.
  • The Warden and Devise authentication framework. It was especially useful in that it works well with Mongo, and allows user accounts to be scoped, when serving multiple virtual sites.
  • The Heroku Ruby cloud-hosting service. MongoMapper’s
    John Nunemaker puts it best in You’re an Idiot for Not Using Heroku.

To my great disappointment, Sponsaurus’ first round of funding fell-through, and they were forced to suspend development after just three weeks. So it was back to job hunting, but with the added wrinkle of having fallen madly in love with the start-up lifestyle. I was dreading having to take just any old code monkey job to pay the bills.

The New

It’s not at all lost on me that almost all of my coding gigs have come as recommendations from friends-of-friends. In the latest case, Bruce introduced me to Conceivian, a custom Rails shop in Redmond, and I’m now their Lead Developer. They specialize in building prototype websites for other start-up companies, which means an endless supply of green-fields coding projects to feed my start-up addiction. It’s also a small team, which means little-to-no politics (yuk). So here goes …


JSON and the Args (or Not)

Posted by code_monkey_steve on Dec 31, 2009 Dec 31

(Admittedly not my best work, but it’s hard coming up with JSON puns)

Playing with JSON Document Stores has led me down the path to a few other exciting JSON toys. The first is …

JSON Schema

Just like XML Schema, JSON Schema allows you to specify (in JSON form) the semantics of a particular JSON data structure. While in theory the schema is useful for validation, in practice validation just sucks up too much CPU time to be worth the trouble. Where things get really interesting is the possibility of automatically generating user interfaces that compose JSON-based messages behind the scenes. There’s just one thing missing …

Service Mapping Description

Ta-da! SMD allows you to describe all of the methods of a web service using JSON Schema to describe each method’s parameters. It supports a variety of transports and envelopes from simple GETs or POSTs up through RESTful resources and JSON-RPC.

The beauty of all of this is that it allows for services (web and otherwise) to advertise its functionality in user- and computer-readable ways. It has the potential to be a generic communication mechanism between disparate bits of software (potentially controlling hardware), allowing us to virtually wire-up appliance X to service Y through user interface Z, without any of them having prior knowledge of each other. Pretty sweet.

You Are 'Here':{}

One last bit of JSON goodness: GeoJSON. As you might expect, it’s a standard for representing geographic information in JSON. It’s already the de facto standard, and supported by just about everybody.

comments | Tags: json

Long Time, No Blog

Posted by code_monkey_steve on Nov 22, 2009 Nov 22

It seems to work out that when I’m most busy coding, I have the least time to blog (and vice-versa). That’s my official excuse for it being two months since my last update: work has been busy.

First, at the beginning of October, me and the Kashless Krew went to AlohaOnRails, my first Ruby/Rails conference in sunny (read: “sweltering”) O’ahu, Hawai’i:

The conference was a lot of fun: got to hear about some neat Rails tech and meet some of the rock stars in the Ruby world.

I’ve also been continuing my exploration of No-SQL databases. While I still love the design of CouchDB, I’ve been playing with MongoDB and MongoMapper. Mongo is a bit more mature, even if it does still have a whiff of the SQL smell about it. I’ll have more Useful Information about that later.

The other thing I’ve been learning is the RSpec testing framework. When I first looked into it a few years back, I found the RSpec syntax unwieldy compared with Test::Unit. But after having to do various hacky things to make Shoulda run decently (e.g. fast_context) and even contemplating writing my own framework (the now defunct Mustard), I’ve come to appreciate the RSpec Way and can’t see myself ever going back.


Safe Mailing with mail_safe

Posted by code_monkey_steve on Sep 20, 2009 Sep 20

Working on a production website can be a bit nerve-wracking, especially when it comes to testing features that send email as a side effect: one little bug could wind up spamming all of your precious users. Of course, Rails has the basic safety feature of simply disabling mail delivery in certain environments (i.e. test and development), but that’s no good because sometimes you do want to test that mail is actually delivered, just without having to worry that it’s delivered to live users.

Enter mail_safe, a handy little gem written by my co-worker Myron. Instead of disabling mail delivery environment-wide, mail_safe allows you to define one or more domains for which mail should be delivered, and a catch-all address for those that shouldn’t. This allows for testing with a real account (if it’s in the appropriate domain), while still keeping you secure against unintentional spam.

Once you’ve started using it, you’ll wonder how you ever slept soundly without it. It’s the sort of thing that should probably be included in the Rails core (IMNSHO).

comments | Tags: rails