August 25, 2007 at 12:40 am
· Filed under ruby
Permalink
August 23, 2007 at 9:22 pm
· Filed under ruby
I was thinking about the PPM format and the following analogy:
PPM : graphics :: WAV : audio
So I figured I’d read up on the WAV format and start generating.
It turns out that one of the shortcomings of not ever having taken a computer science class is that it wasn’t obvious to me how to take an integer and figure out how it would be encoded in a little endian fashion. I’ve got something working (at least VLC and Quicktime don’t barf when I play my WAVs), but I’m sure (so sure) there’s a better solution. As an act of cowardice, I will not share mine. Please show me the light in the comments.
I have yet to make anything that sounds nice. One day, perhaps.
Until then, though, I have this (warning! loud and high-pitched):
Permalink
August 15, 2007 at 11:28 pm
· Filed under regular expressions, ruby
When I wanted to play around with Oniguruma (the forthcoming regular expression engine for Ruby 2.0) I used the Oniguruma gem, which kept me from needing to recompile Ruby or installing 1.9. One of the things about using the gem, though, is that you can’t do this:
001:0> re = Oniguruma::ORegexp.new('e')
002:0> "test".match(re)
TypeError: wrong argument type Oniguruma::ORegexp (expected Regexp)
from (irb):2:in `match'
from (irb):2
Now, I’m really used to the String#match way of doing things. That’s been my idiom. I’ve never ever even thought of using Regexp#match. For some reason (dumb luck, probably) String#match makes more sense to me. So I thought I was pretty smart by adding this to my .irbrc:
require ‘oniguruma‘
include Oniguruma
class String
alias_method :old_match, :match
def match(regexp)
case regexp
when Oniguruma::ORegexp
regexp.match(self)
else
old_match(regexp)
end
end
end
But what I found out (after Hpricot stopped working, looking at its source code, googling for $’ [fun], and playing around in irb) was that no longer were global variables $~, $', $1, etc. being set. Oh wait, did I say global variables?
Surprise! They aren’t. They’re local. They have the scope of wherever the match is happening. So when I wrapped everything within a method, those variables aren’t viewable outside the method. But then, they aren’t really variables anyway. They’re “parser-level macros”, says taw. The confusion is that a lot of people call them global variables (because they certainly look that way).
It tripped me up.
I’ve stopped redefining String#match, and I’m getting used to ORegexp#match when I need an Oniguruma expression.
I hope this information will ever prove useful to someone.
Permalink
August 14, 2007 at 10:39 am
· Filed under rails, ruby
I don’t quite understand why Mongrel and WEBrick insist on loading your .irbrc every single request. It’s especially bad when your .irbrc is full of stuff that kills Rails (GuessMethod, redefining String#match to handle Oniguruma regular expressions, etc.).
Thankfully, Peter Jones clued me in to this:
env IRBRC=/dev/null script/server
Besides not killing Rails anymore, every request doesn’t load 8 unneeded gems, complain that there’s no constant named IRB, and define methods only usable for irb sessions.
Permalink
August 12, 2007 at 6:53 pm
· Filed under ruby
This week’s Ruby Quiz is a one-dimensional cellular automata. I wrote a quick (and oh so dirty) solution which I’m not going to submit, but I rarely submit my solutions so it’s no surprise to me. It even generates images via RMagick, though I could have easily generated PPMs which only require the ability to write bytes to a file. I always forget about PPM. I was actually excited to use RMagick since it turned out to not be very hard to install on OS X (anymore, at least).
Inspired, I wrote a quick (and less dirty) script for generating animated gifs of honest-to-goodness two-dimensional cellular automata, like Conway’s infamous Game of Life.
What I need to learn now is one of those GUI toolkits to figure out how to have an animated canvas. Instead of just dumping images out, it’d be nice to be able to observe (and interact, even) with an unboundedly-long-running world.
Until then, though, I have this:

Permalink
August 9, 2007 at 9:15 am
· Filed under ruby
I’m sick of Google groups. I think ruby-forum is okay enough, but I’m sick of the F5 key. I’m not really into using email for mailing lists. I think I need to get over that. Until then…

One day I’ll have a real solution and I’ll be able to rm rf.
Permalink
August 1, 2007 at 10:26 pm
· Filed under GuessMethod, ruby
I finally got around to writing some specs for GuessMethod. I know the idea is to do that while programming, but I still haven’t quite fully made that part of my habit. This is the sweet spot of the tests so far though:
Cass.nw.clss.should == Clas
Maybe having Clas on the right side of the == is going overboard, but this is what GuessMethod is for: fixing your mistakes so that you don’t have to.
Permalink