Archive for December, 2008

Dead simple reload! for Ruby

Wednesday, December 31st, 2008

One of the things I miss most about Rails when working in gems is the reload! function, which rebuilds the environment to update anything that’s changed since the last save. Well, I wanted to recreate that functionality, but hopefully in a really simple way.

The good news is, it’s actually pretty easy. I have two ways of doing it, pick the one you like best.

module AutoReload
 
  @mes = {}
 
  def reload!
    diffs = AutoReload.differences # we can only call it once per reload, obviously
    if diffs.size > 0
      diffs.each {|f| Kernel.load(f)}
      puts "reloaded #{diffs.size} file(s): #{diffs.join(', ')}"
    else
      puts "nothing to reload"
    end
  end
 
  def self.update_modtimes
    $".each do |f|
      @mes[f] = File.mtime(f) if File.exists?(f)
    end
  end
 
  def self.differences
    oldlist = @mes.clone
    AutoReload.update_modtimes
    newlist = @mes.clone
    oldlist.delete_if {|key, value| newlist[key] == value }
    oldlist.keys.uniq
  end
 
end
 
include AutoReload

You will then need to initialise it somewhere after all your requires. AutoReload.update_modtimes will do the trick. If you can’t manage that, it will only work properly after the first time you use reload!.

This is the way to do it if you have a lot of files, I think, since it maintains a list of what was changed when, and then only reloads changed files.

Note that it’s not perfect. It will only be able to find files which are in local path, ie won’t be able to reload gems. However, that’s all I need for now.

The next way is even simpler, since it doesn’t bother to maintain a list – it just blindly reloads everything it can:

def reload!
  diffs = []
  $".each {|f| diffs << f if File.exists?(f)}
  if diffs.size > 0
    diffs.each {|f| Kernel.load(f)}
    puts "reloaded #{diffs.size} file(s): #{diffs.join(', ')}"
  else
    puts "nothing to reload"
  end
end

As you can see, this just blindly reloads everything it can find. Probably not the best if you have a lot of constants etc, but for a simple project could be just the ticket. The good news is you don’t need to initialise it. if you have a lot of files you probably should return "OK" or something, else you’ll have pages of reloads scrolling past.

Let’s bear in mind that this kind of trick is always a bit of a hack. Kernel.load() has no ability to unload anything, even if it doesn’t appear in the file anymore. All it can do is overwrite. If you break your code by deleting something important, then reloading using this kind of trick won’t show it up – the object is still there until you reload ruby itself. It’s a convenience thing only, so don’t rely on it too much, do a full reload once in a while.

However, for my use case – making a lot of small changes and working in a very interactive manner with irb – this is a real time-saver, hope you find it useful too.

If you’d prefer it to happen automatically, rspec-style, there is a gem available which will do this for you here which basically does the same thing, just every 1 second instead of manually.

Album of the Year 2008 – RFC

Saturday, December 27th, 2008

I’d love to know what people thought were the best albums for 2008.

Here’s my shortlist:

1. Josh Fix (US)Free at Last (pretty stable #1)
2. Girl Talk (US) – (if you don’t like this album, you don’t like music)
3. Roger Joseph Manning Jr. (US) – Catnip Dynamite (as featured here – good strong #3)
4. Above and Beyond (UK) – Anjunabeats Volume 6 (2008). My #1 favourite mix album from the year. Great album, get it. I was thinking I shouldn’t include mix albums but then remembered I had Girl Talk on here so why not.
5. Nightwish (FI) – Dark Passion Play (Far from their best but OK)

I just can’t think of any other standouts this year. I’m interested in suggestions/reminders. Anyone?

Wow, top 3 are all American. That’s a turnaround. And can’t help but notice there’s no Asian representation. Maybe that’s because I didn’t like anything this year, or maybe I’m just sick of all things Japanese and didn’t give anything a fair chance. Could be either. I was down on US, up on JP for a long time; seems to be somewhat reversed now. Ever changing world!

Songs of the Week – Christmas Special

Sunday, December 21st, 2008

Christmas! I love Christmas. The best part of Christmas is being able to play all the christmas songs I love, which by culture I can’t really play the rest of the year.

What is Christmas Music? Christmas Music equals:

1. Trans Siberian Orchestra
2. Home Alone theme
3. The Red Army Choir, which is a little too “in your face” normally so I substitute the “Hunt For Red October” soundtrack, but I’ll include a bonus track here for purists

I need a playlist, I hear you ask? Your wish is my command, dearest reader. After you hear this playlist, you won’t care about any other presents. You’ve already reached the top. You may as well kill yourself after listening to this, actually, ‘cos it’s all downhill from here. Don’t tell anyone I said that.

All right. Let’s go! This christmas is gonna ROCK!!!!

Part 1 – Trans Siberian Orchestra

Track 1: What Child is This? (MP3, 13.6M)

A great starting track which will let the relatives know what they’re in for. Sets the scene with pensive, edgy piano, then kicks ass (in the service of the Lord) when the chorus finally arrives. Perfect beginning to your Christmas lunch!!

Track 2: O Come All Ye Faithful / O Holy Night (MP3, 9.9M)

Another great track which will get feet tapping around the table. Hey, is this Christmas, or ROCKmas? Hint: Both.

Track 3: A Star To Follow (MP3, 8.8M)

Controversial choice, this one. You see, I hate the beginning. Goes for way too long, and in the past I’ve gone so far as to edit that whole beginning out. However, maybe we can get away with it .. the crowd has been lulled into a trance by the first two all-winner-no-filler anthem classics, so maybe they can tolerate 1:08 of annoying chants. Gets good after that. I might post the edited version if anyone wants it, I got it right here.

That’s the thing with Trans Siberian Orchestra. I love their sound, and their concept, but sometimes I can’t stand their arrangements. Ah well, can’t have it all.

Track 4: Faith Noel (MP3, 10.4M)

God I’m sick of hearing stupid little kids. Let’s get back to what we’re all here for: AXE SOLO!!!!! Oh come let us adore him…

Track 5a: Christmas Canon Rock (MP3, 11.6M)
Track 5b: Queen Of The Winter Night (MP3, 7.3M)

Kind of filler but everyone loves these songs.

Track 6: Boughs Of Holly (MP3, 7.1M)

Oh yeah, back into it. Deck the Halls with Boughs of AWESOME! By now everyone should be nice and drunk and not paying much attention anyway. All they know is that this is the best christmas EVAR. This is the time to make a move on your cousin, if you haven’t already! (of course you have!)

Track 7: The March of the Kings/Hark! The Herald Angels Sing (MP3, 6.2M)

More of the same, really. Feel free to skip it if you think it’s a bit noisy.

Track 8: Joy/Angels We Have Heard on High (MP3, 6.3M)

Jesu, joy of man’s desiring. Nice slow wrap-up to the TSO.

Part 2: Japanese section

Free free to ignore this if you hate Japan. Godless heathens, who do they think they are making Christmas music, anyway?

Track 9: 山下達郎: CHRISTMAS EVE (MP3, 5.9M)
Track 9: 山下達郎: CHRISTMAS EVE (english version) (MP3, 5.9M)

English and Japanese versions of this classic. The Japanese is better, but if it gives you jungle flashbacks you might want to stick with the Queen’s Tongue.

Part 3: Soundtracks

Track 10: Home Alone Main Title (“Somewhere In My Memory”) (MP3, 6.7M)

What could evoke more Christmas cheer than the thought of young McCauley Culkin evading bumbling home invaders? That’s right, nothing.

Track 11: O Holy Night (Home Alone) (MP3, 3.8M)

A nice arrangement of this classic.

Track 12: Hymn to the Red October (MP3, 7.1M)

In 1984, the USSR’s best submarine captain in their newest sub violates orders and heads for the USA. Is he trying to defect, or to start a war?

Another Christmas classic.

Part 4: Bonus

Track 13: The Red Army Choir – National Anthem Of The USSR (MP3, 8.6M)

Self explanatory. The very essence of Christmas, distilled.

——————–

That’s all, folks. After that lot’s played through – you won’t have any choice but to hit START OVER. This never gets old. Trust me.

Any requests, tell santa to put your comment in my little comment stocking below! Ho, ho, ho!

Song of the day – Wuthering Heights

Monday, December 15th, 2008

I have always loved Kate Bush’s 1978 song Wuthering Heights. I’d love to be able to sing like that! Maybe after my operation.

Wuthering Heights (MP3, 11.4M)

Lyrics after the break. Most of the lyrics floating around are wrong, I’ve corrected them.

(more…)

Song of the day – Beauty of the Beast

Monday, December 8th, 2008

One more Nightwish song, since my moods change like the weather. A mere 10:22 in duration.

Beauty of the Beast (MP3 23.8M)

Another in my tradition of having nothing to say, so posting music. The lyrics in this are a bit, uh, over the top, but still kind of work.

(more…)

Song of the Week – I thought about it first

Thursday, December 4th, 2008

Song of the Week is “I thought about it first” by Josh Fix, cementing the end of my multi-month Nightwish binge. I particularly love the occasional “advance downbeat” (I’m sure there’s a proper name for that), which is really effective and leaves you waiting for the next one.

The album, Free At Last, is in first place for my Album of the Year 2008.

Download (MP3, 10.8M)

Lyrics after the break (transcribed by me – I guess someone has to do it first).

(more…)