Brian Ewing

Prying into Rails

Pry is an amazing, light-weight alternative to IRB packed with nifty features.

I wanted to be able to run a rails console, but with Pry instead of IRB. I tried initially hacking at the “rails console” command, but it’s not nicely feasible as it calls IRB.start with no room for customisation without overriding the whole Rails::Console#start method (see the source code, cerca line 47)

So, a rake task will have to do. Here’s what I came up with:

1
2
3
4
5
6
7
8
9
10
task :pry => :environment do
  begin
    Pry.start
  rescue NameError
    STDERR.puts "Pry isn't installed. Place this in the development group in your Gemfile"
  end
end

task :console => :pry
task :c => :pry

This works great! Running “rake pry” drops into a Pry shell with my app at the ready. Nice :)

Ruby Fixnum#power_of?

This is my quick solution:

Gotta love Ruby. I’ve seen others solve this iteratively/recursively dividing by i until less than or equal to i. Less than meaning not a power, and equal to meaning it is a power. This is an expensive operation, whereas this uses the magic of logarithms.

One problem is with the check at the end (whether the exponent of i is a whole number by checking if it’s exactly divisible by 1) failing. Floating point precision means that there will be a remainder of ~0.000000001, and it’ll give a false negative. This happens above 2^29, unfortunately. This could be ""fix’d"" by checking if the remainder is <= 0.000000001, but that’s a bit of a hack.

This is the best implementation I can come up with right now though. I’ve seen a great implementation for powers of two, like this:

1
2
3
def power_of_two?(i)
  i >= 0 and i & (i - 1) == 0
end

Which, unfortunately, won’t work for bases other than 2. Feel free to improve my Gist above!

we7: A thoughtful review

Following the backlash over Spotify’s forced new Facebook integration, I’ve been in search of a new streaming service, settling on we7. This will be a long post, as with any luck some of the guys at we7 will pick it up.

The Good

Firstly, what’s done right. I immediately loved the we7 radio. I said I wanted to listen to ‘Snow Patrol’, and it played a few of my favourite Snow Patrol songs, and intertwined them with songs from other artists that were well within my music tastes. I was genuinely surprised at how much I enjoyed the music it threw up, new and old, known and unknown. It’s now my preferred way to listen to background music.

In terms of “discovering new music”, it’s done a far better job already than Spotify has done in the two and half years I’ve been using it, with this being one of Spotify’s purported selling points. I love the idea of it being your personal DJ and you putting in ‘requests’ for artists, genres, songs, albums, etc you like. It works well because, well, it works well.

It makes great use Chrome notifications to tell you which song’s playing, and logging in with my OpenID is pretty nifty.

Papercuts

It’s a real pity that the UI has so many papercuts. For me, there’s a lot of work to be done to make for a seamless experience.

When you buy a Premium(+) subscription, it takes a few moments to realise that, no, you haven’t been tricked into buying something: you have to visit a URL nested in your welcome email to get to the secret ‘subscription’ part of the site, with an entirely different UI. This isn’t too bad in itself, and I presume it’s a bug that it wasn’t linked to on the homepage until I logged in again. Should be easy to fix.

The most annoying thing, probably the most in need of attention, is how awkward it is to find and play specific songs. I’m not saying it’s a major hassle, but compared to other services it’s a lot more work. The search is far from perfect, and doesn’t seem to have any relevant ordering based on popularity or anything.

To play “Milk” by Kings of Leon, I have to click on a link for more search results, then go to the second page, then click on the link for that song, taking me to a page just for that song. I then have to hit ‘Play’ there. It’s a lot of work compared to the single double-click it takes in Spotify after searching for “Milk” (which is in the first few results there.

It seems to actively prefer songs that are less popular than popular songs with the same name.

The song results from a search are another papercut. For me, song results are often below the fold, whereas albums and artists matching the search term aren’t. I have to search, then scroll the page to see if it’s in the first page (which it usually isn’t). In general, the search is pretty awkward and really affects the experience.

It’s also difficult to find a song by a particular artist. Searching for ‘milk kings of leon’ yields zero results. You have to scour the song results or scour the artist’s general song listings to find a song by artist.

The search really needs to be improved before I can use this as my daily player. I want to be able to flick over to the we7 tab, type a song name (with the artist if necessary) and have it just play, in one or two key presses or clicks, then go about my business. Spending a 30 second to a minute finding a specific song just feels like a chore.

Customer Service

When I originally signed up, there was a hiccup with my payment and for some reason it didn’t go through, while the funds had been authorised from my account. I was able to phone them and get through to a real human being. They handled this extremely well. I felt valued as a customer, and the rep saved me as a customer. I think she was genuinely sorry that my first experience with them as a company was negative. I phoned again with a quick query and happened to get through to her again, and she seemed genuinely happy that my second attempt at a payment went through fine. In short, they have great customer service and you can get through to a human if things go wrong.

Result

I’d say go for the subscription, if only the ‘Radio+’ subscription to escape the ads from the regular radio player and have an unlimited request limit.

The on-demand player hasn’t really sparked my interest, as the papercuts above just make it that little bit too awkward to use, in my opinion. That said, if what I’ve noted above can be improved, I’ll be quite happy to make a full switch from Spotify. I find my self firing up Spotify for specific songs, and listening to we7’s personal radio as background music the rest of the time.

PS. we7 has just started playing another song I’d forgotten I loved. This is awesome.

Relayer::start!

Relayer is a dead simple, high-performance, event-driven IRC library written in Ruby.

It uses IO#select to achieve asynchronous IO, and has been tested to handle many hundreds of concurrent IRC connections from one instance.

Wrote this a few weeks ago when I couldn’t find a nice multi-connection IRC library for Ruby, just getting round to releasing it now ;)

Sample usage:

1
2
3
4
5
6
7
echo = Relayer::IRCClient.new(:hostname => 'irc.esper.net', :nick => 'EchoBot', :channels => ['#echo'])

echo.events.channel_msg do |irc, event|
  irc.message event[:channel], event[:message]
end

Relayer::start! echo

Fork it on Github

Hello world

First blog post. :)