Month: February 2011

  • For a personal project I’m working on, I need to find out the smallest time period with more than 5 records. I essentially wrote this code:

    period = [1.week, 1.month, 1.year].select_first do |period|
      Record.where("published_at >= ?", period.ago).count >= 5
    end
    

    only to find out that the select_first method doesn’t exist. So I wrote it:

    module Enumerable
      def select_first(&predicate)
        self.each do |item|
          if yield(item)
            return item
          end
        end
        return nil
      end
    end
    

    and then of course, I tested it:

    require "test_helper"
    
    require "enumerable_extensions"
    
    class EnumerableTest  2 }
      end
    
      should "select_first the first one" do
        assert_equal 1, [1, 2, 3, 4].select_first { |i| i >= 1 }
      end
    
      should "select_first the last one" do
        assert_equal 4, [1, 2, 3, 4].select_first { |i| i >= 4 }
      end
    
      should "select_first none" do
        assert_equal nil, [1, 2, 3, 4].select_first { |i| i >= 100 }
      end
    end
    
  • I’ve just released another gem, this one extends Hash to contain another method called hmap. This solves a problem I face ofter: how to run a map in a hash that returns another hash, for example:

    {:a => 1, :b => 2, :c => 3}
    

    being converted into

    {:a => 2, :b => 3, :c => 4}
    

    With hmap it’s easy:

    hash.hmap { |a,b| {a => b + 1} }
    

    It also works with arrays, but you must make sure the array you return always contains two and only two elements:

    hash.hmap { |a,b| [a, b + 1] }
    

    And that’s all, quite a simple piece of code, but now it’s re-usable and well tested.

  • Some time ago I had an idea for a web application. That idea was essentially Gist. I couldn’t convince people to go after it, thankfully because I wouldn’t like to compete with a Brad Feld backed company. It’s nice to see that the idea was good, as Gist got bought by RIM. Congratulations Gist!

  • I think this partnership with Microsoft was a mistake for Nokia. It was great for Microsoft though.

    Every time you show an old Nokia phone you get the same comment: “Oh, those phones were built to last, I went through 3 iPhones and the Nokia still works…” or “That is the only phone that will not blend”.

    But Nokia’s obsolete software is killing it. They need to provide truly smartphones. They had three options:

    • build their own
    • use Android
    • use Windows

    I think they already tried to build their own and failed. That’s very hard. A giant like Microsoft tried to build their own and failed. It’s very hard to build a software platform.

    Apple did it by being the first ones and providing the coolest product ever. Google did it by doing it for free. They are both a success because they have thousands and thousands of applications on their platform by now.

    Microsoft haven’t done it. As powerful as Microsoft is, they still haven’t cracked the smartphone market, and it’s very likely they’ll never do. They are up against Apple and Google and both of them have years of advantage now (previous efforts by Microsoft are useless today).

    Now Microsoft has a chance to do it because of the deal with Nokia. Nokia is likely going to put Windows 7 Phone whatever on the hands of many people. Those people will get use to Windows, but not to Nokia and may switch to HTC or another provider in a blink of an eye. Microsoft wins, Nokia loses.

    I think Nokia should have done with Android. I know it’s hard to differentiate yourself with Android (what’s the different between a Samsung and a Sony/Ericson phone these days? They both run Angry Birds), but Nokia could have done it by making a tough phone. There’s a lot of people today not using smartphones because they won’t last in their pockets. Nokia could build a smartphone for them.

    Maybe Nokia decided against Android because of their past mistakes with Open Source projects and companies. At any rate, I think they are making a mistake right now.