A cute little trick with Ruby hashes and .try()

Recently I encountered a situation where incoming JSON was converted to a hash, and certain keys were not always present. Naturally I want to safely grab the values that were present without barfing on the nils.

    hsh = {foo: bar: [2]}
    bad_hsh = {foo: nil}
    should_be_2 = hsh[:foo][:bar].first # works
    uhoh = fail[:foo][:bar].first # NilClass error

At first began looking for a way to use .try() to get at nested hash values (since we’re not on Ruby 2.3 yet and can’t use the lonely operator). But the normal try syntax didn’t work:

    uhoh = fail[:foo].try([:bar]).try(:first) # does not work

But then I remembered that [] is actually the name of the method, and what’s in it is the first parameter, so I tried this:

    win = fail[:foo].try([], :bar).try(:first) # It works!

This is not fantastic code, I know there are better ways to do this, and I can’t wait to upgrade to Ruby 2.3, but I do find this interesting.

WordPress Multisite Tips

You can’t start a multi-site install from scratch, you have to install the base site first, then turn on multi-site. If you turn it on right out of the box, you’ll get stuck with a database error and probably spend an hour messing with MySQL permissions before you figure it out. Don’t ask how I know 😉

Also, for running WordPress locally, it took a while to get auto-update working (it’s easier to use than than update manually on the local machine).

To do that I had to enable FTP on this machine, which I kind of hate to do because it’s a security hole. But to do that, execute this command:

sudo launchctl load -w /System/Library/LaunchDaemons/ftp.plist

When wordpress asks for FTP info, you can use ‘localhost’ as the hostname

permissions updates may have been a factor, but I’m not sure, but mostly it just worked

The big revelation is the steps for multi-site setup. The wordpress docs are weak in this area. I really wish they would create two different documents, one for subdomain, one for subfolder, because it’s a lot of work to mentally filter out one or the other as you’re reading the document, and it’s easy to miss details.

Of particular note is that first, you turn on wp_allow_multisite, then  the Network settings item appears in your admin, then you enable the network, THEN you add the code it recommentds, includeing the MULTISITE=true item

More than I ever wanted to know about cookies

Here is a list of things I learned working on a project a while back, I wrote them down because they’re so screwy I know I’d force myself to forget them:

  • Cookie size causes both the browser and the server to barf. Total size, not just individual cookie.
  • Deleting a cookie is tricky, sometimes IE won’t delete session cookie without a restart.
  • When deleting, use ‘0’ as expiration, the full “Thu, 01 Jan 1970 00:00:00 GMT” fails in IE, it sets it to 2070 for some reason.
  • If you have two cookies, one with a subdomain, one without, the one without will “win”.
  • it can be hard to mess with a higher scoped cookie from a subdomain. Avoid it if you can
  • And here’s the kicker: Sometimes the buffer space on your load balancer can be too small for the total size of cookies that your browser is sending, even if Apache can handle larger cookies. In that situation you’ll get an ugly 500 error page. This buffer space may only become an issue if there are rules on the LB that require it to inspect the headers.

Just the other day I saw this article on a related note: Let’s Break The Internet

Image Uploads to AWS S3 using Rails 3, Paperclip and PLUpload

For the Invest Your Heart project, I’ve been working on getting image uploads perfected. That’s easy enough with the Paperclip gem. But storing uploads on your Heroku dyno isn’t a good idea, so I wanted to use S3. Oh and throw in upload previews, resizing and a progress bar.

I managed to get it all working, though it took a lot because there are so many moving parts. I’m using the PLUpload code to provide uploads and progress var interface and Paperclip for resizing. And I’m uploading directly to S3, to avoid timeouts on Heroku when large files are uploaded. After the initial upload, Paperclip actually downloads the image, processes it, and uploads the different resized versions back to S3.

I couldn’t have done it without these two incredibly helpful sites: The Rails-S3-Plupload demo project, and Swarut’s post. Thanks guys! I would never have gotten it working without you.

Swarut also turned me on to the Gon gem which provides a simple way to send data from your controllers and JS variables. Very handy for when your app doesn’t need something like Backbone.js

All aboard the StartupBus

I’ve decided to break out of some ruts and join the StartupBus (@startupbusfl), a 3 day moving hackathon on a bus to Austin, TX for South by Southwest Interactive (SxSWi).

I’ll be posting a whole lot more about it as I go along, so I’ll start here with just the list of things that I’m packing to bring along (aside form the obvious tech):

  1. Sketchpad
  2. Dry-erase markers
  3. Post-its
  4. Headphones
  5. My Ideas To Pitch
  6. Luna Bars
  7. MyPressi Portable Espresso Machine
  8. Upgraded Coffee
  9. 100GB of music
  10. Pre-built Rails application skeleton (I’ll tell you about that in another post. soon!)



(photo credit teddy.is)

Mini Upgrade

image by 'smee' at Apple InsiderThanks to this tutorial, I was able to upgrade my Mac Mini media server to 3GB of RAM. That allowed me to upgrade it to Mac OS X Lion as well (it only had 1GB before, and Lion requires 2GB). That’s made quite an improvement in file sharing responsiveness. If you recall, even on Snow Leopard, it could take 30 seconds to connect to a server. Initially, I had tried to use the DIMMs left over form my MacBook Pro upgrade (to 8GB), but they were too advanced. Luckily, I had an old white MacBook in the closet with a bad motherboard which happened to have matching DIMMs, so I was able to complete the upgrade.

Pow and WordPress on the same machine

While developing the InnerLightTools application on my local machine, I’m using the Pow web/rack server. It’s a great way to run rails apps locally. At the same time I’m working on WordPress development locally as well, and for that I need Apache. By default Pow runs on port 80, just like Apache, so I couldn’t do both at the same time. Initially I set Apache to run on a different port, but discovered the WordPress Multisite won’t run on ports other than 80 and 443. So, I found the Pow configuration page contained information about setting Pow to run on an alternate port. It’s pretty simple, just add

export POW_DST_PORT=<any port num>

to your ~/.powconfig file and restart. My Pow is finicky, so I often have to reinstall it to get things to work properly. Fortunatley that’s trivial with the Powder gem.

Lion, Rails 3.1 and WordPress Clients

I haven’t gotten much done on Inner Light Tools on the last few months. My day job had taken over all of my free time, at the same time, I was waiting for Rails 3.1 to finish up. I’d begun converting the code over with one of the early Release Candidates, but when I updated my MacBook to Lion, things broke. At the same time I’d tried renaming the Xcode directory, and then make and gcc stopped working. Even when I moved the folder back they wouldn’t work.

Because I couldn’t compile any native code, the upgrade to rails 3.1 final would’t work. I couldn’t get Xcode to reinstall from the App Store either. So I reinstalled it on another machine, and made a copy of the installer before it was finished (when it gets deleted). With that I was able to reinstall, and everything started working again, thankfully.

Now that I had a final version of Rails. I could get on with the code. Over the weekend I spent some time deleting parts of the app. Originally I’d planned to allow users to build customized intake forms, but dealing with all the complexity that brought was driving me nuts, and taking forever to code. So I’ve cut it out, and it’s not just a simple model.

I may go back to that idea in the future, if my users actually want that kind of thing. I’ll ask them first.

Also, I’m trying out WordPress clients, trying to decide between MarsEdit and MacJournal. I’ll let you know which one wins.


I’m disappointed in Drupal. It looked like they were doing great things with the 7.0 upgrade, and I was looking forward to it. I have 7 different sites running Drupal 6 right now. As soon as 7 was release I upgraded this site to it. That process was not as seamless as I would have hoped.

for the 8 months prior to 7.0, it seemed like every plugin author has added a compatibility pledge to their listing, saying that they’d have a working version on the day 7.0 was released. This made me even more eager to jump in. Unfortunately not everyone was onboard.

Prior to the upgrade there was no easy way to test whether your plugin has a 7.0 compatible version available. I could have visited the nodes for ever one of my plugins, but there is no easy way to find links to those pages, even with the module installed. So would have had to go to the drupal site and do a search for every one of the 20 or so modules. And the names are fairly generic, so sometimes it’s hard to find the right plugin, or nodes discussing the plugin flood the results and you have to spend a while digging through to find the right node.

So I just went for it and upgraded, and what a mess it was. half my plugins broke, so I had to install alpha or beta versions of those. If they were available, many times there wasn’t even that. So, I lost a lot of functionality including core things like MetaWeblog API support. For months I’d check on the nodes for those modules, hoping for an update. Few came.

I have a lot of time and knowledge invested in Drupal, but things were handled so poorly that I’ve given up. Usability even went downhill in 7.0, with some setting taking me months to find. This site is now running wordpress.

What a difference! Whereas the Drupal experience is clunky, disorganized and rough, WordPress is a joy to use and install. the UI polish is an order of magnitude greater, and really makes it feel like the developers care about your experience. Plugins actually have ratings, so you can compare them easily. Installation is trivial, and the number of classy professional themes available is staggering.

In short, I’m pissed that I waited so long.