Ruby Typed Accessors

Pat Ladd started IMing me a week ago looking for a simple Ruby solution to the following problem:

  • He was building objects of a SOAP web service
  • SOAP in Ruby returned everything as strings
  • He wanted his objects to have numbers be numbers, not strings

You could do a lot of manual conversion, but it really seemed like there should be a simple way to say "this is an int, do the conversion for me".  While ruby is dynamically typed, it is also strongly typed, so String + Int causes an exception, typically in a place you weren't expecting.

He and I googled around for a while and found that no one seemed to have tackled this yet.  Later that day Pat sent me the first version of this, all done with some fun meta programming.  He and I both realized that this was probably useful enough that we should share, so I signed up to help package it as a gem.

Announcing Typed Accessors Gem

A quick example of how this works is below:

class Foo
float_accessor :float
date_accessor :date
end

>> f = Foo.new
=> #
>> f.float = "1.4"
=> "1.4"
>> f.float
=> 1.4
>> f.float = "1"
=> "1"
>> f.float
=> 1.0
>> f.date = "2009-10-30"
=> "2009-10-30"
>> f.date
=> #
>> f.date.to_s
=> "2009-10-30"

The full documentation can be found on rubyforge, and the source tree on github.  It's even got unit tests as of this weekend (which already found and fixed one bug).  This is released under MIT license, so do with it what you will.

If you want to use typed accessors just:

gem install typed_accessors

And require it in your environment.  It works as a mixin on Class, so very seemlessly fits in your environment.

The Daily Show: ending the war on science, the only war we were winning

Or directly linked here.

Adding alternating table row colors dynamically with prototype

Alternative table row colors (also known as zebra tables) are very handy.  Recently I ran into an issue with redmine where it generates tables in a way that prevents you from tagging the rows as "even/odd" when you create them.

You can fix this after the fact with the following prototype function (redmine is a rails app, so I used prototype, it should be pretty easy to do in jquery as well).

function colorTable() {
    var EvenOddCount = 0;
    $$('.splitcontentleft tr').each(function(element) {
            if(EvenOddCount % 2 == 0){
                element.className = "even";
            }else{
                element.className = "odd";
            }
            EvenOddCount++;
        });
}

I use your own css selector where appropriate in the $$ function.  Then you can set tr.even and tr.odd in your css file and give it your color scheme.  Just set onload="colorTable();" in your body declaration, and off you go.

That's the Futurama I remember

Susan and I finally watched Into the Wild Green Yonder last night.  Typically I had watched these on release day, but after Bender's Game, it didn't feel like it had the same sense of urgency.  I love Futurama, have bought everything in the past, but the 3 previous movies left me wanting.  One of the brilliances of Futurama was the sheer density of funny, and trying to get that with a 90 minute plot is tough.  Each movie previously had seemed a little worse than the one before, which made me sad.

But, by the time they got to Into the Wild Green Yonder, they seem to have figured it out.  It felt more like the original Futurama than any of the other movies to me.  Maybe it's the fact that the Futurama gang could always do environmentalist humor well.  At any rate, it was good.  And for anyone that enjoyed the show, I'd highly recommend buying that DVD (if you haven't already).  The possibility for future Futurama is all about DVD sales at this point.  So if you like this stuff, don't download it, don't netflix... buy it.

Integer Latitude / Longitude

I was very happy to recently realize that you can put latitude and longitude directly into google maps, and it will give you a map with that marker.  This is really useful if the address gives you a location that isn't right, as you can manually adjust the latitude and longitude to put the marker in the right place.

My parents almost live on the 44 degree lat line (I think the GPS coords for our wedding were 43.9998), so it occurred to me to see where the closest integer Lat / Long was to them, which isn't that far away, though it's close to a mile off any of the local roads. 


View Larger Map

I was obviously curious where the closed one was to me, which I've actually been past before. It's also only a couple hundred feet of a road, so easy to get to.


View Larger Map

And that made me wonder. What an interesting experiment that would be, to get a photo from ever integer lat / long on the planet, and display it on a website. I've got too many sticks in the fire, so no time for that, but if anyone decides to do it, let me know. I'll contribute a few pictures from the ones I can get to.

Updated: apparently this already exists, my search terms were just wrong the first time.  It's called the Degree Confluence Project.

Fixing Github with Greasemonkey

Github is great, I really can't say enough good things about them.  What's not so great is their css, as it breaks badly if you change you dpi.  I finally found the root cause of this which is the search field gets squeezed vertically, and collides with the div under it, forcing that to the right, and forcing the right sidebar down.  I sent in some feedback, but am not sure if/when it might get applied.

So, in the short term I added the following greasemonkey script for github.com

function addGlobalStyle(css) {
    var head, style;
    head = document.getElementsByTagName('head')[0];
    if (!head) { return; }
    style = document.createElement('style');
    style.type = 'text/css';
    style.innerHTML = css;
    head.appendChild(style);
}

addGlobalStyle('#header .topsearch {width: 25em}');

And voila, the site is readable again.

Why I love Ruby on Rails... impement a new feature in about an hour

Yes, I'm a fanboy, but with good reasons.

Tonight I decided that I'd really like to add openid support to the MHVLUG Event Calendar.  Accounts aren't used that much in it, because you have to be in the admin role to notice any difference.  However, it's just one less field for me to have to fill in.

So, the 6 million dollar question, what was the duration of time between deciding I'd like to do this (though having no idea how), and having a working implementation...

65 minutes.

Which included getting up to get dinner in the oven.

In blow by blow format:

  • A quick bit of googling got me to the openid-authentication railscast, which I watched all the way through.
  • I fixed a couple of his refactoring bits to work with newer restful_authentication.
  • I also installed an old open_id_authentication plugin the first time, which gave me an exception.  Make sure to get the latest on github.
  • I modified my user to have my identity_url using the rails console
  • I used the rails debugger to figure out that I needed an extra slash on the end of my identity_url, which is why my login with openid wasn't working

Which is a pretty serious amount of bouncing around because I had no idea what I was doing when I got started.  That will turn subsequent openid enablement into a 15 minute exercise.

Until one is committed, there is hesitancy

Until one is committed, there is hesitancy, the chance to draw back, always ineffectiveness. Concerning all acts of initiative (and creation), there is one elementary truth the ignorance of which kills countless ideas and splendid plans: that the moment one definitely commits oneself, the providence moves too. A whole stream of events issues from the decision, raising in one’s favor all manner of unforeseen incidents, meetings and material assistance, which no man could have dreamt would have come his way. I learned a deep respect for one of Goethe’s couplets: “Whatever you can do or dream you can, begin it. Boldness has genius, power and magic in it!”

W. H. Murray [thx SM]

via the Signal vs. Noise blog (yes, you should go and subscribe to that immediately)