Use a regular expression to match a string up to a given character

Often I run into a string where all I want is to match everything up to a given character. So given the following test string:

This is my string with a pipe | and everything after it is stupid.

Using Ruby as my reference language, I might do something like this in an interactive ruby session (aka: IRB) or on

str = 'This is my string with a pipe | and everything after it is stupid.'

Which would produce the following in Ruby:

"This is my string with a pipe "

Lets break down the different parts of the expression ^.*(?=\|) and what they're doing:

  • ^ start at the beginning of the string
  • .* match anything from the current position
  • (?=\|) only if followed by a pipe character

The last part of the expression is what's known as a positive look-ahead (?=). Using a look-ahead is like attaching a condition to an existing regular expression. I'm telling my expression to return the match only if it's followed by characters matched by an additional expression.

For a more comprehensive dive into look-around functions, head on over to Also feel free to play with my example on

As always, use regular expressions responsibly.

November 20, 2015  ·  Permalink

Clean up empty upload directories created by Carrierwave

Carrierwave doesn't clean up empty directories when you delete a record with an existing upload. It deletes the file, but not the containing directory. That is a task you can handle with an ActiveRecord callback.

Let say there is a model called 'Post' that you can attach an image to. The code would look like the following:


class Post < ActiveRecord::Base
  mount_uploadeder :image, ImageUploader

  after_destroy :delete_empty_upload_directory

    def delete_empty_upload_directory
      FileUtils.rm_rf(Rails.root.join('path', 'to', 'your', 'uploads', if self.image

Simple, nice, and easy.

November 11, 2015  ·  Permalink

Screenr is shutting down

In an email from Screenr, a web-based app for recording your screen:

As you might already know, Screenr’s recording capability is based on Java RE (Runtime Environment), which is rapidly becoming antiquated. For example, Google Chrome doesn’t support Java RE anymore. So Screenr can’t run there. And on other browsers, Java support is also limited, causing constant problems for users.

Because we at Screenr pride ourselves on providing a superior customer experience, these issues are simply unacceptable. And there’s no way around them.

Before Droplr implemented screen recording in their app, I used Screenr. It was useful and their free tier allowed some quick and dirty screen recording with minimal friction.

Admitting defeat is hard. But completely ignoring the competitive field for apps like this is the real reason why Screenr is shutting down. I guess blaming Java platform and quitting is easier than adapting your software to use better, newer technology.

October 28, 2015  ·  Permalink

Programming sucks.

Peter Welch via Mashable:

Every programmer occasionally, when nobody's home, turns off the lights, pours a glass of scotch, puts on some light German electronica, and opens up a file on their computer. It's a different file for every programmer. Sometimes they wrote it, sometimes they found it and knew they had to save it. They read over the lines, and weep at their beauty, then the tears turn bitter as they remember the rest of the files and the inevitable collapse of all that is good and true in the world.

The truth of this is both sobering and hilarious.

June 06, 2015  ·  Source  ·  Permalink

iTunes and Spotify are aligning to compete

Apple and Spotify are on a war path to dominate the digital music space. Both are very fortified in their own successes but each have something to gain from their competitors' respective strongholds.

In the red corner...

iTunes, a juggernaut of the music industry also does a great job of delivering podcasts, music videos, movies, and television. Still, music has always been the essence of iTunes and its ability to remain king has been in question thanks to the onslaught of music streaming services.

Apple is working on expanding iTunes with a music streaming component through its Beats acquisition. Maybe at last I can stop feeling guilty about not using my huge expensive library of iTunes-purchased music.

In the blue corner...

Spotify, a one category service whose popularity is driven by people leaving behind the pay-per-song and pay-per-album model of music consumption. They have been growing veraciously and they've recently expanded their horizons into fitness, videos and podcasts.

Being a long-time Spotify user, I'm thrilled they're adding podcasts to the mix. Despite Marco Arment's superb Overcast app driving my podcast listening, I think Spotify has a lot to gain from it.

What not to expect

Both companies are certainly stretching outside their comfort zones and it's exciting to see. Undoubtedly users will be switching to and fro and how each company manages retention will be extremely critical. Music is laughably cheap to acquire now and merely offering more of it isn't enough.

With that said, I don't expect artists on music streaming services to start jumping ship like Taylor Swift did not so long ago. Spotify is in a very good position as a distribution platform and any artist would be stupid to ignore that.

Apple's strategy is to show up late. They let the market mature and then launch a product that is all the best parts. I expect Apple to be a very aggressive competitor. But I don't expect them dominate in the short term. Spotify will continue to be a reigning champion of music streaming for the foreseeable future.

As always, time will tell. But it sure is exciting to see the fruits of a competitive market.

May 25, 2015  ·  Permalink

Cucumber step definitions with optional arguments

When I am writing cucumber step definitions that pass arguments, not all of those arguments are needed. And I don't want to write a separate step with or without those arguments. The best way to do this is with a non-capturing regex.

Take the following example (in Ruby):

Given /I am logged in as (.+)/ do |user|

What if I don't want to define a user in every scenario? A default user would be great when none are passed. Since the verbiage works without this portion: as (.+), I'm going to do non-capturing regex around it, which looks like the following:

Given /I am logged in(?: as (.+))?/ do |user|

There are 2 capture groups. The first captures the entire string and the second captures the argument I'm trying to pass. I add ?: which defines the first group as non-capturing. The second group remains the same. How I make it optional is by adding a ? (question mark) to the end of the first group.

Now I can hit two birds with one stone like so:

Given I am logged in
Given I am logged in as some_user

Both will match a single step definition. Don't repeat yourself.

May 15, 2015  ·  Permalink

Should you get the new Macbook (2015)?

Answering such a question means understanding why this Macbook exists. The new Macbook, simply put, is filling the gap between the best iPad and slowest Macbook Pro.

UPDATE: I completely glossed over the 11-inch Macbook Air for $899. Which is an even better value if you're upgrading from an iPad. It's faster, supports more storage and is less than half a pound heavier. While it lacks a Retina display, I don't see any reason to get the new Macbook over the similar sized 11-inch Macbook Air.

It's all about upgrade path

It's rocking an Intel Core M processor so you're not getting a huge leg up over anything younger than 2 years. Andrew Cunningham of Arstechnica had this to say:

If you’ve got a 2013 or 2015 MacBook Air, it will be a step down. If you have a 2012 MacBook Air, it’s a step sideways at best.

An upgrade from an iPad makes the most sense. Especially if you've been using one for a few years. If you wanted something as cheap as the best iPad but not as expensive as the latest Macbook Air or Pro, the new Macbook is the way to go. Buying it for any other reason will leave you mostly disappointed.

Should you get one?

No. The 2015 Macbook is the slowest Macbook in the line up by a large margin despite it's Haswell heart. While it does bring you the lightest most portable Macbook with a retina display, you're still better off with a Macbook Air or the entry-level 13-inch Macbook Pro.

March 13, 2015  ·  Permalink

File Uploads with Rails 4, Carrierwave and Google Cloud Storage

You're developing a rails app and you want to upload files. You, of course, are fancy and want to use Google Cloud Storage instead of Amazon's Simple Storage service (AWS) and Carrierwave instead of Paperclip.

Instead of boring with you with prose, I'm just going to send you over to the Stack Overflow answer that gives you the TL;DR on how to accomplish multi-file uploads with Rails 4, Carrierwave and Google Cloud Storage! The answer also includes a link to the rails app demo source code on Github.

More at Stack Overflow

December 14, 2014  ·  Source  ·  Permalink