January 2007
Monthly Archive
Thu 25 Jan 2007
Ruby is a great language. Really. How many other languages let you extend other classes? Modify their behavior? Overload operators? With great power comes great opportunity, and in this post I’ll cover one of my recent extensions to Ruby that was recently accepted into Rails’ core.
Date and Time
Managing dates and times is not exactly fun in any language. There are time zones, leap years, two meridians, twenty-four hours, 365 days (sometimes), and a mess of inconsistently long months. Those of us who use Ruby frequently are familiar with how to get objects corresponding to the current time and the current day:
>> Time.now
=> Thu Jan 25 20:12:50 -0800 2007
>> Date.today
=> #<Date: 4908251/2,0,2299161T>
Sexy methods…
Rails has these really cool extensions to Numeric that allow computing lengths of time in a more human-readable way. For example, if I wanted to figure out how long 12 hours is, I could just call 12.hours and get back the number of seconds in 12 hours, which could then be conveniently added to an instance of Time, like so:
>> t = Time.now
=> Thu Jan 25 19:31:17 -0800 2007
>> t + 12.hours
=> Fri Jan 26 07:31:17 -0800 2007
Sweet! What’s even cooler though is that, in addition to Numeric with seconds, minutes, hours, days, weeks, fortnights, months, and years, we also get from_now, ago, since, and until:
>> 1.day.from_now
=> Fri Jan 26 19:34:20 -0800 2007
>> 2.weeks.ago
=> Thu Jan 11 19:34:35 -0800 2007
but kinda dumb.
Nice! These are so easy to read. These were great for simple things, but since they returned numbers, they couldn’t take into account the current date and how many days were in a month, leap year, etc. Notice that this causes problems when the month doesn’t have 30 days in it:
>> Time.now
=> Thu Jan 25 21:01:31 -0800 2007
>> 1.month.from_now
=> Sun Feb 24 21:01:34 -0800 2007
So it’s good for approximations, but not much else. Well what now?
A smart method
To compensate for the lack of accuracy in the above sexy methods, Time#advance was added to make it easy to do smart addition to Time instances. Here’s some of the above using advance:
>> t = Time.now
=> Thu Jan 25 21:11:32 -0800 2007
>> t.advance(:hours => 12)
=> Thu Jan 25 21:11:32 -0800 2007
>> t.advance(:months => 1)
=> Sun Feb 25 21:11:32 -0800 2007
Excellent! Not quite as sexy, but definitely smart. A method suitable for Serious Business.
Smart n’ Sexy
I welcomed Time#advance, like everyone else, but I didn’t want to give up the sexy methods. Fortunately, Ruby is great for letting objects masquerade as other objects (the whole duck typing thing), and the de-facto way to do this in Rails is by using Builder::BlankSlate, whose instance undefine almost all of their methods, allowing you to easily proxy all or some methods to something else. The two best examples of its use in Rails are AssociationProxy, which is used by Active Record, and JavaScriptGenerator, which is used by RJS.
To solve the problem, all we need to do is make the duration methods on Numeric return a proxy for the number they used to return which acts accordingly around Time and Date objects. This new class is located in ActiveSupport::Duration, and simply accumulates lengths of time for when it is used around a Time or Date, and a number for use around things that expect it to act like a number (i.e. for backward compatibility). Here’s the new, smart and sexy, methods on Numeric:
>> t = Time.now
=> Thu Jan 25 21:21:44 -0800 2007
>> t + 1.month
=> Sun Feb 25 21:21:44 -0800 2007
>> t + 1.week
=> Thu Feb 01 21:21:44 -0800 2007
>> 1.year.from_now
=> Fri Jan 25 21:22:00 -0800 2008
>> 4.years.from_now
=> Tue Jan 25 21:22:12 -0800 2011
>> 3.weeks
=> 21 days
How’s that last one for overriding inspect? To try it out all you have to do is freeze edge in a Rails project near you!
$ rake rails:freeze:edge
Update: I should add that this recent change also corrected handling around Date objects. I implied that above, but I should be explicit. Before Duration, adding Date.today and 1.day didn’t yield the expected results at all:
>> (Date.today + 1.day).to_time
ArgumentError: time out of range
Now it behaves as expected:
>> (Date.today + 1.day).to_time
=> Sat Jan 27 00:00:00 -0800 2007
Sat 20 Jan 2007
Earlier I mentioned the contest that CD Baby is putting on in cooperation with WorkingWithRails. I’d hoped that my Duration patch (which was accepted) would be enough to keep me in the running until the 22nd. By chance I also ran into a problem with the PostgreSQL adapter while working, and took some time to fix it. That was actually accepted just a few hours after I wrote it and made it into 1.2, which was recently released.
I think it’s great how much activity this contest has spurred. There are many, many documentation patches now. People have been using Kevin Clark’s Heckle to find gaps in Rails’ test suite and writing patches to fill those gaps. People’s little pet peeves are now being submitted as patches (mine included). The downside is that the core guys now have a lot more work to do, but now that Rails 1.2 is out they’ve got a little more time.
So thanks to everyone on this list - those who have contributed to Rails within the last 20 days or so. Rails has a pretty amazing community and it’s great to work with such talented, devoted people. I’m sure the activity will die down a little after the 22nd, but at least for me it’s helped me feel a bit more a part of the community, and I bet others feel the same. Hope to see you all at RailsConf!
Sun 14 Jan 2007
In the usual style of a stream of consciousness I’m writing this immediately after watching Garden State (thanks Kip!), which I just saw was written, directed, and lead by Zach Braff! My guess as to how it’d go is that they’d end up apart either because a) she had some terminal illness (oh wait, that’s A Walk to Remember) or that they were just meant to be friends that changed each others’ lives by giving the other perspective (uh, oops. Lost in Translation). Actually they end up together in the end but in a way that feels real and leaves much of the usual cliché behind.
So some people use drugs prescribed by a doctor to change their mood (Zoloft is the new popular one, right?). Some use drugs not prescribed by a doctor. Some use alcohol to deal with things in their life that they either can’t or don’t know how to deal with. I use movies. And music. And I know I’m not alone in this. How many of the three of you have that favorite movie that you like to watch on a rainy day or when you’re feeling just a little down? The funny thing is that people know this but it’s really just a perspective shift, like the Necker Cube.
Favorite message from the movie: when he’s talking to his dad, he said that they should try just being okay that they are who they are. Fricin’ obvious, no? But we don’t do it. Oh perhaps you do, reader #3, but I know the others don’t. And I don’t. I have these Bose headphones which I just noticed have a pretty low rating on Amazon. I’m not an audio snob, but I bought them because I liked them when I tried them out and they were on sale at a Good Guy’s closing. I stopped when the right side’s plastic part broke, that is until I took some initiative and visited my local Wallgreen’s to buy some Krazy Glue - about a year later. I even managed to avoid gluing my fingers to my eyelid! I use them to listen to music and language lessons on BART, since my iPod headphones don’t block out enough sound to allow me to hear what I’m supposed to repeat back in Japanese to the people staring ahead on BART who can’t hear me anyway. As a consequence of this I take them off when I’m walking, but it’s too much trouble to put them away, so I put them around my neck, and I realized that I must look a bit like Rob in High Fidelity with my leather jacket and messenger bag.
But the thing is, it’s really a costume, and it feels like a costume. I’m playing the role of That Guy who wears headphones while walking around and has a tape deck at home and prefers vinyl to CDs and makes lists of his top five whatever a couple times a day. It’s amusing to me that people might envision this whole other fictional part of my life based solely on my wanting to carry headphones that actually block sound. When I was in Santa Barbara I biked to work, and I became an environmentalist - at least to other people. I’m just a cheap bastard who is afraid of buying anything that’d tie him down is really all it boils down to, but that’s not what others think. Do I recycle? Yeah. Did I go watch An Inconvenient Truth and get freaked out by it? Sure. But I’m not an environmentalist.
The other point I liked from the movie, which was similar to the first, is shared by Fight Club and many others, just phrased differently. In Garden State it was that it doesn’t make sense to break off relationships with people in order to work out your “issues”, whatever they might be (exceptions might be made for those with homicidal tendencies, but I digress) and that you’ve just gotta live your life and deal (Kip, you should be nodding your head about now). The way Brad Pitt put it in Fight Club was, “This is your pain. This is your burning hand. It’s right here.” The extent to which this was what I did with Sarah is unclear to me, but it played a part. For that I’m sorry (as if it matters, nearly a year later).
Movies also provide for me some sense of anchoring. When I first moved to Bakersfield and didn’t know anyone, just starting high school, Kelly and I would get home from school and watch Hook. We watched that movie so many times. When I first moved to Santa Barbara I did the same thing, just with a different set of movies. I’m doing it now, and in my rotation are Pride and Prejudice, Superman Returns, Lost in Translation, and newcomer The Illusionist and perhaps Clerks II.
As a developer, this is sort of a natural thing to have happened. I spend a lot of time on the computer, and watching these movies is something I can do while I work without distracting me too much. It’s like an IV. I suspect, however, that there are better drugs out there. I enjoy spending time with friends, there are just not enough of them in San Francisco yet. Zazen is another that I’m determined to try. Wandering aimlessly around the city has proven to be beneficial. Today I discovered Glen Canyon, which doesn’t quite have the view Bernal Hill does, but it’s a lot more green and pretty.
I’ll close this weird post with a question: if you dropped everything right now and went traveling, how long could you go? Would you stop because of financial reasons, or homesickness, or something else? My answer: about a year, stopping for financial reasons.
Fri 12 Jan 2007
At the Ruby meetup Josh Susser mentioned that CD Baby, a Portland-based Rails shop, has purchased 20 tickets to RailsConf ‘07 as well as 0 hotel rooms in the area (Portland). These will go to the top 20 contributors from Jan 1 to Jan 22. I checked the list and found that, as of yesterday, I’m in the top 20. Hopefully my Duration patch will be accepted and I’ll stay in the top 20 until the 22nd. Any suggestions?
Tue 9 Jan 2007
Posted by brian under
GeneralNo Comments
So it’s real. And it looks like a wet dream of a phone. Let’s see, how does it compare to my previous list of must-haves and would-like-to-haves?
Bluetooth + WiFi
Yep, it’s got this. And 2.0 EDR at that. To top it off, Apple will also offer a Bluetooth headset that, at first blush, is quite small and sexy. This is a must-have that is quickly becoming standard on most cell phones. It has WiFi too! Gah!
Sync Support
Well, duh. Apple would be insane not to do this, and though Apple is many things, it is not insane. I expect full support for Address Book, iCal, iTunes, and iPhoto.
Google Maps
Yep, and it seems to even have GPS support! Maybe Apple got Cingular to pull its head out of its ass and support this feature. From what I understand GSM networks have technical issues with this, but it is technically possible to do it on GSM. Google Maps on the iPhone looks even sexier than the one on Google’s website.
Email
POP (Gmail, etc) and IMAP (corporate, .edu) support. Will we see push email like Blackberry? I bet we will.
Web Browsing
As a web developer I would like to have frequent access to the internet, and the iPhone will do this extremely well. It has Safari with zooming via two-finger pinching.
The Rest
It’s also a video iPod, so you can play movies, tv shows, music, etc. It looks truly amazing. So it sounds awesome, but guess how much it costs! It’s only $499 for 4GB! Wow, that’s… uh.. expensive. Damn it!
Sun 7 Jan 2007
As much as I’d like to have this be a project that I do, if someone has already done it or sees this post and runs wild, so be it - I’d rather that the service exists.
What I really want is a website or service that can change my address with all of my other services. The problem is that, if this is a website, this service would have to know my login information for all of these things, which is not so good. The other way to do it is to create a client application. A third, and I believe better, alternative is the model Flickr uses for their API: you don’t give the other services your login info, but they request permission to do something, which you can then grant. They are then authorized to do just that thing. In this case it’d be to update your address information. I recently made a list of the places I’d have to change my address when the time comes to move again:
- usps.gov
- cingular.com
- wellsfargo.com
- americanexpress.com
- amazon.com
- paypal.com
- providian.com
- ingdirect.com
- lendmonkey.com
There may be more, but I can tell you that this list has expanded since my last move, and is likely to expand even more as time goes on. If each one took me an average of 10 minutes to do it’d take me an hour and a half to complete. That’s not much fun. Where would you have to change your address? I’ve filed this project under someday/maybe, so if you post it here it’s got a better chance of actually happening someday.
Fri 5 Jan 2007
Posted by brian under
Movies ,
LoveNo Comments
My roommates’ latest NetFlix envelope contained Pride and Prejudice, based on the Jane Austin novel. I’ve not read this one, but I have read Persuasion, which seems to remind me of this one but with various English names swapped for various others. It could simply be Keira Knightley, but I think it more likely that I have a soft spot for these types of movies, especially when I find myself not in the midst of a love affair of Georgian magnitude - in this, I suspect I am not alone.
Having only just watched the movie, I am well aware of its effect on my English. You know that strange desire you get to imitate an Outback Steakhouse advert commercial whenever an Aussie is around - it’s of the same nature. What really lets the sappiness sink in is the commonalities I choose for myself from the protagonists (incidentally a funny thing about Pride and Prejudice is that it is almost completely devoid of antagonists).
Elizabeth: Do you dance Mr. Darcy? / Mr. Darcy: Not if I can help it.
Ah yes, how frequently that very response has come to my mind.
Mr. Darcy: Would you allow me to see you back to the town? / Elizabeth: No, I’m rather fond of walking.
Me TOO, she and I would be perfect for each other. /sarcasm
What a roundabout way of saying that I miss being in a relationship, eh? Ah, but I’ve come up with an outline of how to remedy that. And no, it doesn’t involve dating.
Tue 2 Jan 2007
Posted by brian under
Music ,
iTunes ,
iLikeNo Comments
I decided to try out a music recommendation service called iLike, using Google to get around their home page’s Application Error (Rails) (whoops!) to download the app.
It seems like a so-so thing. You get to listen to previews of other songs on iTunes that are similar to the one you’re currently listening to. That’s enough to make it interesting, but not enough to keep it around. The one added thing that pushed me into keeping it is that, in addition to the songs on iTunes, it has recommendations of songs that are free (where they come from, I know not), and will even download them into iTunes for you. Sweet!
I recently downloaded one that had a title that is similar to a journal entry I was thinking of writing: “Song For My Future Wife”, where the future wife is, of course, an unknown person.
Tue 2 Jan 2007
I’ve been looking for a new cell phone over the last month or so. My current phone, the T616, has Bluetooth and syncs well with my MacBook via iSync. It turns out I can even use it as a Bluetooth modem for my computer! Why I didn’t learn about that several years ago when I bought the phone is beyond me. A few holes in functionality have popped up though, especially with the move to SF and the new job:
- Google Maps: exploring SF is great, but it’d be nice to be able to look up where things are without having to cart my laptop around and finding a Starbucks (yes, I’ve done that)
- Email: sometimes (like yesterday) when I’m expecting an email (which, incidentally, never came!) it’d be nice to know that I’ll know when it comes and I don’t have to keep wondering whether it’s been sent. also communicating quick things to my co-workers (answers to Ruby questions, perhaps?)
Some other nice-to-haves come to mind:
- Real web browsing, with a decent browser
- SSH access to various things
I don’t have a data plan at the moment, so there’s going to be an increased cost to all of this. That hasn’t been the biggest problem though. Here’s a rundown of the phones I’ve been considering:
Nokia E61/2
This phone is a PDA, and it runs Symbian OS. I’d never heard of it but it seems like a great alternative to Palm OS and Windows Mobile since it’s open and has lots of developer backing. The included browser is KHTML-based, and looks quite a bit like Safari (duh). It supports push email via Blackberry Connect and even works with iSync via a plugin. The screen is awesome and the size isn’t bad, since it’s actually a hair thinner than my T616. It’s GSM and runs on Cingular. The E61 variant even has Wifi. The downside? No 3G support, so it’s stuck at EDGE speeds. $99 w/2yr contract on Cingular.
Blackberry Pearl (8100)
This is closer to the candybar style that I’m used to, but features a scrunched QWERTY keyboard that puts two letters on one key. Maybe it’s just that I’m not used to it, but typing this way was a serious mind-tweak. I know I’m conditioned by every other cell phone to expect the three-letters-per-number setup, but I didn’t have any such problem on actual QWERTY keyboards like the N62. I think I’d get used to it after a little while. This phone is QUAD-band, like the N62, which would be great for travel abroad. The built-in browser is just a piece of WAP crap, but supposedly it supports Opera Mini, which might be better. That’s just one of the nice-to-haves though. I put Google Maps on the one at the T-Mobile store, and it was SO SLOW. Really god-awful. The included Navigation app was faster, so maybe I could get used to it. And since it’s a Blackberry it of course supports the push email thing. $199 w/2yr contract on Cingular.
T-Mobile Dash
I include this one mainly for completeness, as I did look at it briefly. What I liked about this one is the way it fits in your hand. It’s got a good size and has a kind of rubber that makes handling it nice. I really couldn’t abide Windows Mobile though, so this one is out of the running.
Sony Ericsson Z610i
This phone looks so sexy. The external display is one of those OLED screens that you can’t see when it’s not on. Great for one of the things I love about the T616: being a watch. Unfortunately it is only sold in the UK and lacks the 850 band, which is pretty much necessary if you spend any time in the US outside of major cities. Even if it was available here I don’t know how well I’d handle the flip-phone thing. My between-the-T616s phone was a Motorola V551, a flip phone, and I found it hard to like.
Sony Ericsson M600i
Now here’s a Symbian OS phone that is 3G! Hallelujah! Except… wait a minute, it’s a touch-screen? The reviews I’ve read for this phone claim that it is not easy to operate with one hand. Also, there’s no 850 band, so no Cingular! It features the same keyboard type as the Blackberry Pearl. I’d really have to try this one out before passing judgement on it. It sounds good on paper, but who knows?
To top off the confusion, all of these phones are GSM and should work with either Cingular or T-Mobile. The problem is that nearly all the phones available on these networks are EDGE, not 3G. If I want EVDO I have to move over to a CDMA network like Verizon or Sprint. Besides having more phones available with that fast connection, the other advantage on that side of the fence is that nearly all those phones have GPS built-in. The Helio Drift that I reviewed last month had this, and it was great. Google Maps actually knew where I was! I didn’t quite appreciate how nice this feature was at the time. However, I still wouldn’t have kept the Drift because of its lack of GMail support. If it did have GMail support that might have been enough to get me to keep it, since I’d basically scratched the sync itch with a script. The problem with CDMA phones is the difficulty in swapping them around. I’d rather not buy my phones through my carrier, and I’d ideally rather not involve them at all. GSM lets me do this just by swapping SIM cards.
It seems that there is no phone/service combination on the market today that ideally suits my needs. If I were forced to pick a phone today, I’d probably pick the Blackberry Pearl with its small form factor, scroll nipple that reminds me of the Mighty Mouse, and push email capabilities. On T-Mobile it’d cost me $199 up front with a 2yr contract and $59.98/mo for unlimited calling to five people (300 anytime minutes) and unlimited internet and push email. Contrast that to Cingular with $199 up front with a 2yr contract and $79.98/mo for 450 anytime minutes and unlimited internet and push email. From what I hear, Cingular has a faster EDGE network than T-Mobile (160kbps vs. 120kbps peak). You can see why I really want to like the Sony Ericsson M600i: 3G + Symbian OS. Alas, it’s the wrong 3G. So the winner: Sony Ericsson T616.