the anarchy of ruby

Posted by crayz

It seems the zeitgeist in ruby-land is looking for a solution to the ‘do this if I can’ patterns that continually crop up in code. In the python community you’d wait for the problem to be solved by Guido the benevolent dictator. With PHP there’d be three near-identical functions added to the global namespace in a minor point release. With Java you’d wait a couple years for a JSR, a couple more for the next release of the language, and a couple more for enterprisey vendor support. In the meantime you’d copy & paste around a “design pattern” solution to make yourself feel better

In Ruby, a bunch of people just go out and write their own, and then everyone else votes with their code:

Each solution has its own set of trade offs. For my money, #andand is the clear winner. It’s got the cleanest syntax(no symbols) and is closely tailored to the case of wanting to call a method if the receiver isn’t nil. #try is interesting, and might be worth rewriting to work the way #andand does. It gives you the same benefits of calling methods on possible nils, although it would behave in a way that might seem unexpected for methods that nil actually has, since doesn’t undef those the way #andand does. However #try also stops you from sending any message the receiver can’t respond to, which strikes me as overkill for most situations

And then #do_or_do_not, which just sends the damn message and rescues any exceptions. This I think is a huge mistake. When I first started coding ruby I abused the inline rescue exactly this way, just haphazardly rescuing any lines of code that might fail. It quickly became a nightmare for maintenance and bug hunting, because you wind up unintentionally swallowing ‘real’ exceptions when all you meant to do was stop the code from blowing up calling a method on nil. #do_or_do_not will be a little safer because the swallowed exceptions will be limited to the method called rather than an entire line of code. Still, this seems far more dangerous than it’s worth. Frequently throwing exceptions also causing performance issues when you’re neck-deep in the stack (just ask Twitter)

Biggest 'WTF' in PHP 6

Posted by crayz

From the unofficial PHP 6 changelog:

With ternary operator, the "true" expression is no longer required - this can be done: $a = $s ?: 'b';

How this is still a ternary operation, I have no idea

puts "Can they get anything right?" if php.still_sucks?

OS X Ruby Alarm Clock

Posted by crayz

No real error checking, but it should be pretty obvious whether it works when you run it.

%w(rubygems rbosa chronic).each{ |dep| require dep }

itunes = OSA.app 'iTunes'
wake_at = Chronic.parse ARGV.first
time_until_alarm = wake_at - Time.now

puts "I'll wake you up at #{wake_at}"
sleep time_until_alarm

itunes.play

my love for Java

Posted by crayz

Reading through some basic Sun language docs, I remember why I hated C++:

class SwitchDemo2 {
    public static void main(String[] args) {

        int month = 2;
        int year = 2000;
        int numDays = 0;

        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                numDays = 31;
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                numDays = 30;
                break;
            case 2:
                if ( ((year % 4 == 0) && !(year % 100 == 0))
                     || (year % 400 == 0) )
                    numDays = 29;
                else
                    numDays = 28;
                break;
            default:
                System.out.println("Invalid month.");
                break;
        }
        System.out.println("Number of Days = " + numDays);
    }
}

Or Ruby:

month, year = 3, 2000

days = case month
  when 1, 3, 5, 7, 8, 10, 12  then 31
  when 4, 6, 9, 11            then 30
  when 2 then
    if ((year % 4).zero? && (year % 100 != 0)) || (year % 400).zero?
      29
    else
      28
    end
  else raise "Invalid Month"
end

puts "Number of Days = #{days}"

You've gotta love Java having to wrap everything in a class though... It feels so "object oriented"

Leopard loves Ruby

Posted by crayz

The following uses ruby-growl and Apple's RubyOSA (think AppleScript for Ruby) to connect to iTunes and push the current track(if there is one) to its end, and pop-up a nice notification that it did. It's a slightly silly use, but I've always wanted a quick way to skip to the end of the current song while still bumping it's playcount(just hitting "next track" in iTunes won't increment your playcount of the current one, even though you just listened to 75% of it)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
GROWL_PASS = "zzzzz"

require 'rubygems'
require 'rbosa'
require 'ruby-growl'

def growl_notify(message)
  growl = Growl.new "localhost", "ruby-growl", ["itunes-ruby"], nil, GROWL_PASS
  growl.notify "itunes-ruby", "iTunes / ruby", message
end

itunes = OSA.app('iTunes')
if(current_track = itunes.current_track.get rescue false)
  track_name = current_track.name
  itunes.player_position = current_track.finish
  growl_notify "track '#{track_name}' ended"
else
  growl_notify "no track currently playing"
end

(sorry about the fugly syntax hilighting)

"Apple loves Java", part 2

Posted by crayz

Heh, reminiscing back to that wonderfully hilarious statement that "Apple loves Java." They have shown how much they love it with their new Mac OS X 10.5, Leopard. On the page of 300 new features, number of sections in which the following technologies are mentioned
JavaScript: 3
Python: 4
Ruby: 5
Java: 0

Leopard has added the latest versions of Ruby, Rails, Mongrel, and a number of other gems included by default. They worked closely with the Ruby community to make this possible. Java... still at Java 5. Java 6, released nearly a year ago, will be added as a separate download at some undetermined later date. Can you feel the love?

Update: More here, here, and here. Funny stuff

blog, take 4

Posted by crayz

As you may have noticed, things are looking a little different. I finally got myself setup on a VPS, and switched the blog over to Mephisto, a Rails CMS/blog software. I may still end up settling on something different, but for now Mephisto seems fairly nice

This is now the fourth iteration of this blog, which has variously existed on:
  • my personal OS X box + Apache
  • couple different shared hosts
  • and now this VPS + nginx
And for software, running:
  • some hacked up PHP scripts and static text
  • MovableType
  • WordPress
  • and now Mephisto

The rest of the old content and such will be appearing once I can decide what to do with it. Hopefully I’ll maintain the blog a little better now that it’s running Ruby. I got fed up trying to work with the junky PHP code it was using

Java who?

Posted by crayz

Tim Bray, Sun Microsystems, discussing why Sun is sponsoring all three next-gen Ruby VMs:
[Ruby is] immensely useful for building all sorts of things, but it’s not fast enough... give me a Ruby with performance as good as a really good Smalltalk VM, and the space of things for which you need statically-typed languages shrinks to a really uninteresting size