MAR | MAY | JUN
| 20 |
2006 | 2007 | 2008

23 captures 3 Mar 06 - 31 May 09 |

Close Help

## iPhone humour

Posted by sjs Fri, 18 May 2007 18:34:00 GMT

Love it or hate it - even though it's not even out yet - the iPhone has spawned at least 2 good jokes.

- The other iPhone lawsuit [geekculture.com]

- A comment on slashdot:

  > "I'm waiting for the iPhone-shuffle, no display, just a button to call a random person on your contacts list."

Posted in amusement, apple | Tags apple, funny, iphone | no comments | no trackbacks

## Cheating at life in general

Posted by sjs Wed, 16 May 2007 16:46:00 GMT

NB: My definition of life is slightly skewed by my being somewhat of a geek

Luckily no one in the real world cares if you cheat. Most of life is open-book, but for the times when you just need to find something quick the answer, of course, is to cheat profusely.

I've only checked out a few of the cheat sheets but they are of high quality and their wiki-like nature means if they suck and you have time you can help out. I was very pleased to find that there are a number of zsh cheats already.

They certainly know the way to my heart! Ruby, Rails, TextMate*, vim*, zsh, screen. That'll do snake. That'll do.

* There are cheats for emacs, jEdit, and e too if TextMate and/or vim don't tickle your fancy

Tags cheat | no comments | no trackbacks

## Dumping objects to the browser in Rails

Posted by sjs Wed, 16 May 2007 03:38:00 GMT

Here's an easy way to solve a problem that may have nagged you as it did me. Simply using `foo.inspect` to dump out some object to the browser dumps one long string which is barely useful except for short strings and the like. The ideal output is already available using the `PrettyPrint` module so we just need to use it.

Unfortunately typing `<pre><%= PP.pp(@something, '') %></pre>` to quickly debug some possibly large object (or collection) can get old fast so we need a shortcut.

Taking the definition of `Object#pp_s` from the extensions project it's trivial to create a helper method to just dump out an object in a reasonable manner.

/app/helpers/application_helper.rb

    def dump(thing)
      s = StringIO.new
      PP.pp(thing, s)
      "<pre>#{s.string}</pre>"
    end

Alternatively you could do as the extensions folks do and actually define `Object#pp_s` so you can use it in your logs or anywhere else you may want to inspect an object. If you do this you probably want to change the `dump` helper method accordingly in case you decide to change `pp_s` in the future.

lib/local_support/core_ext/object.rb

    class Object
      def pp_s
        pps = StringIO.new
        PP.pp(self, pps)
        pps.string
      end
    end

Posted in rails | Tags rails | no comments | no trackbacks

## Good reading

Posted by sjs Wed, 16 May 2007 00:55:00 GMT

This is the best tutorial I have read on developing RESTful web apps using Rails. If you're remotely interested or curious about REST on Rails I highly recommend that as one of your first reads.

I have been messing around with REST and managed to piece together a basic app without much trouble. That tutorial really cleared up some things I wasn't too sure about, especially with respect to routing resources.

Check out the restful_authentication plugin to see how a session becomes a resource.

For many cases I can definitely see the benefits of REST and for the exceptions you just use Rails in the traditional way.

Posted in rails | Tags rails, rest | no comments | no trackbacks

## Enumerable#pluck and String#to_proc for Ruby

Posted by sjs Fri, 11 May 2007 06:14:00 GMT

I wanted a method analogous to Prototype's pluck and invoke in Rails for building lists for options_for_select. Yes, I know about options_from_collection_for_select.

I wanted something more general that I can use anywhere - not just in Rails - so I wrote one. In a second I'll introduce `Enumerable#pluck`, but first we need some other methods to help implement it nicely.

First you need `Symbol#to_proc`, which shouldn't need an introduction. If you're using Rails you have this already.

Symbol#to_proc

    class Symbol
      # Turns a symbol into a proc.
      #
      # Example:
      #   # The same as people.map { |p| p.birthdate }
      #   people.map(&:birthdate)
      #
      def to_proc
        Proc.new {|thing, *args| thing.send(self, *args)}
      end
    end

Next we define `String#to_proc`, which is nearly identical to the `Array#to_proc` method I previously wrote about.

String#to_proc

    class String
      # Turns a string into a proc.
      #
      # Example:
      #   # The same as people.map { |p| p.birthdate.year }
      #   people.map(&'birthdate.year')
      #
      def to_proc
        Proc.new do |*args|
          split('.').inject(args.shift) do |thing, msg|
            thing = thing.send(msg.to_sym, *args)
          end
        end
      end
    end

Finally there's `Enumerable#to_proc` which returns a proc that passes its parameter through each of its members and collects their results. It's easier to explain by example.

Enumerable#to_proc

    module Enumerable
      # Effectively treats itself as a list of transformations, and returns a proc
      # which maps values to a list of the results of applying each transformation
      # in that list to the value.
      #
      # Example:
      #   # The same as people.map { |p| [p.birthdate, p.email] }
      #   people.map(&[:birthdate, :email])
      #
      def to_proc
        @procs ||= map(&:to_proc)
        Proc.new do |thing, *args|
          @procs.map do |proc|
            proc.call(thing, *args)
          end
        end
      end
    end

Here's the cool part, `Enumerable#pluck` for Ruby in all its glory.

Enumerable#pluck

    module Enumerable
      # Use this to pluck values from objects, especially useful for ActiveRecord models.
      # This is analogous to Prototype's Enumerable.pluck method but more powerful.
      #
      # You can pluck values simply, like so:
      #   >> people.pluck(:last_name)  #=> ['Samhuri', 'Jones', ...]
      #
      # But with Symbol#to_proc defined this is effectively the same as:
      #   >> people.map(&:last_name)   #=> ['Samhuri', 'Jones', ...]
      #
      # Where pluck's power becomes evident is when you want to do something like:
      #   >> people.pluck(:name, :address, :phone)
      #        #=> [['Johnny Canuck', '123 Maple Lane', '416-555-124'], ...]
      #
      # Instead of:
      #   >> people.map { |p| [p.name, p.address, p.phone] }
      #
      #   # map each person to: [person.country.code, person.id]
      #   >> people.pluck('country.code', :id)
      #        #=> [['US', 1], ['CA', 2], ...]
      #
      def pluck(*args)
        # Thanks to Symbol#to_proc, Enumerable#to_proc and String#to_proc this Just Works(tm)
        map(&args)
      end
    end

I wrote another version without using the various `#to_proc` methods so as to work with a standard Ruby while only patching 1 module.

    module Enumerable
      # A version of pluck which doesn't require any to_proc methods.
      def pluck(*args)
        procs = args.map do |msgs|
          # always operate on lists of messages
          if String === msgs
            msgs = msgs.split('.').map {|a| a.to_sym} # allow 'country.code'
          elsif !(Enumerable === msgs)
            msgs = [msgs]
          end
          Proc.new do |orig|
            msgs.inject(orig) { |thing, msg| thing = thing.send(msg) }
          end
        end

        if procs.size == 1
          map(&procs.first)
        else
          map do |thing|
            procs.map { |proc| proc.call(thing) }
          end
        end
      end
    end

It's just icing on the cake considering Ruby's convenient block syntax, but there it is. Do with it what you will. You can change or extend any of these to support drilling down into hashes quite easily too.

Update #1: Fixed a potential performance issue in `Enumerable#to_proc` by saving the results of `to_proc` in `@procs`.

Posted in coding, ruby | Tags extensions, ruby | 2 comments | no trackbacks

## Rails plugins (link dump)

Posted by sjs Thu, 10 May 2007 07:22:00 GMT

Some Rails plugins I find useful:

- Simply Helpful
- Focus-fu
- Sexy Migrations
- TextMate Footnotes (just awesome)
- acts_as_taggable_on_steroids
- acts_as_textiled

Posted in rails | Tags rails | no comments | no trackbacks

## I can't wait to see what Matt Stone & Trey Parker do with this

Posted by sjs Thu, 10 May 2007 04:34:00 GMT

I'd just like to say, bwa ha ha ha!

Summary: Paris Hilton drove with a suspended license and is facing 45 days in jail. Now she's reaching out to lord knows who on her MySpace page to petition The Governator to pardon her. I might cry if I weren't pissing myself laughing.

Paris Hilton is out of her mind, living in some fantasy world separate from Earth. Pathetic doesn't begin to describe this plea for help from her own stupidity. She knowingly and willingly disobeyed the law, got busted, and then proceeded to claim that

> "She provides hope for young people all over the U.S. and the world. She provides beauty and excitement to (most of) our otherwise mundane lives."

I take this as a learning experience. For example, I learned that the US is no longer part of the world.

Flat out crazy, insane, not of sound mind. She is not any sort of hope, inspiration or role model for anyone.

Posted in amusement | Tags crazy, i_laughed_i_cried | no comments | no trackbacks

## dtrace + ruby = goodness for Sun

Posted by sjs Wed, 09 May 2007 22:45:00 GMT

Suddenly I feel the urge to try out Solaris for i386 again. Last time I gave it a shot was when it was first released, and all I ever got out of the CD was a white screen. It's been 2-3 years since then and it should be well-tested. I'll try to install it into a VM first using the ISO and potentially save myself a CD. (I don't even think I have blank CDs lying around anymore, only DVDs.)

The culprit.

Posted in ruby | Tags dtrace, ruby | no comments | no trackbacks

## A new way to look at networking

Posted by sjs Sun, 06 May 2007 06:10:00 GMT

Van Jacobson gave a Google Tech Talk on some of his ideas of how a modern, global network could work more effectively, and with more trust in the data which changes many hands on its journey to its final destination.

Watch the talk on Google's site

The man is very smart and his ideas are fascinating. He has the experience and knowledge to see the big picture and what can be done to solve some of the new problems we have. He starts with the beginning of the phone networks and then goes on to briefly explain the origins of the ARPAnet, which evolved into the modern Internet and TCP/IP and the many other protocols we use daily (often behind the scenes).

He explains the problems that were faced while using the phone networks for data, and how they were solved by realizing that a new problem had risen and needed a new, different solution. He then goes to explain how the Internet has changed significantly from the time it started off in research centres, schools, and government offices into what it is today (lots of identical bytes being redundantly pushed to many consumers, where broadcast would be more appropriate and efficient).

If you have some time I really suggest watching this talk. I would love to research some of the things he spoke about if I ever got the chance. I'm sure they'll be on my mind anyway and inevitably I'll end up playing around with layering crap onto IPV6 and what not. Deep down I love coding in C and I think developing a protocol would be pretty fun. I'd learn a lot in any case.

Posted in technology | Tags networking | no comments | no trackbacks

## Gotta love the ferry ride

Posted by sjs Sat, 05 May 2007 18:25:00 GMT

I lived in Victoria for over a year before I ever rode the ferry between Vancouver Island and Tsawwassen (ignoring the time I was in BC with my family about 16 years ago, that is). I always just flew in and out of Victoria directly. The ferry is awesome and the view is incredible, navigating through all those little islands. Last time I rode the ferry I snapped this shot. It's possibly the best picture I've taken on that trip.

Posted in life | Tags photo | no comments | no trackbacks

Older posts: 1 2 3 ... 5

# sjs

## 5d d9 2b e6 21 22 8d ad
7e bb 84 a9 ee cd 99 4b