Filtering Each Word in a String in Ruby

Posted by Zach Baker Thu, 27 Apr 2006 21:03:00 GMT

Here’s a little Ruby method I found handy. I recently needed to convert some strings from all capital letters into something more polite. Normally, using String#scan as a word iterator will do just fine. However, I was looking for a way to modify each word in the string but keep punctuation and other non-word stuff in place.

So I created a filter (and filter!) method for String. It pulls each word from the string, yields it to the given block and uses the result to replace the original word. Sort of like map for strings:

"e. e. cummings".filter {|w| w.capitalize }
=> "E. E. Cummings"

"Merrye Olde Englande".filter {|w| w.chop }
=> "Merry Old England"

"EVETS KAINZOW".filter {|w| w.reverse.downcase.capitalize }
=> "Steve Wozniak"

So here’s the code.

class String
  def filter(word_pattern = '\w+', &proc)
    result_string, rest_of_string = '', self
    word_re = Regexp.new(word_pattern)
    while word_match = word_re.match(rest_of_string)
      rest_of_string = word_match.post_match
      # concatenate the part before the match and the result of the yield (unless nil)
      result_string << word_match.pre_match + (yield word_match[0] or word_match[0])
    end
    result_string + rest_of_string
  end
  def filter!(word_pattern = '\w+', &proc)
    replace(filter(word_pattern, &proc))
  end
end

And here’s the rub:

"DUDE, WHERE'S MY CAR?".filter {|w| w.capitalize }
=> "Dude, Where'S My Car?"

Whoa! See that rogue capital S? That’s an example of what happens when using the default pattern /\w+/ to match words. There’s no logic to make the “that” and the apostrophe-s one single word. So you may find you need to pass in a more sophisticated regular expression:

"DUDE, WHERE'S MY CAR?".filter(/\w+'[Ss]|\w+/) {|w| w.capitalize }
=> "Dude, Where's My Car?"

Of course, you might want something a bit more sophisticated than that.

The Infamous Desert Bus

Posted by Zach Baker Fri, 03 Mar 2006 15:48:00 GMT

So for whatever reason, a mini-game in a never-released Sega CD game has gained some measure of popularity recently.

Desert Bus is part of Penn & Teller’s Smoke & Mirrors, a game which had mini-games you could use to play practical jokes on your friends, like in their book Cruel Tricks for Dear Friends. In this case, the world’s most boring and endless game, which takes eight mind-numbing hours of work to finish a single level. With no ability to pause.

My first job was working at Sega with one of the guilty involved parties, programmer Mike Fernie, who worked on Desert Bus (the bug on the windshield was his idea) as well as lip-syncing Mofo the Psychic Gorilla. Now that’s a resume item.

Apparently, it was finished but never published because the developer went under. However, he told me that it was actually approved by Sega, which is remarkable. Remarkable because one of Sega’s own testing requirements was to play through every level of the game. So, that’s right, the test department actually had to drive that bus through the desert for eight hours, not letting go of the controls. Apparently they did it in shifts. You think you’ve had pointless days at work! Try babysitting a purposely boring game (that won’t ever be published) for eight hours straight. And people think game testing is fun. I wonder what they would have done if the game crashed when they got to the end.

P.S.: Also at Lost Levels, you can read a history of Sonic X-Treme, the cancelled Sonic game for the Sega Saturn, involving Luxo’s own Chris Senn.

Last Day at Luxoflux

Posted by Zach Baker Thu, 09 Feb 2006 00:06:00 GMT

After four and three-quarters years, this is my last day programming at Luxoflux.

When I started at Luxo in May 2001, I was excited about finally making the jump into the “big leagues” of video game development. I jumped in working on a game that ended up selling five million copies. Activision bought us. We started working on Shrek 2. We poured everything into a True Crime sequel and it came up a little disappointing for everyone involved, including myself. So now it’s time for the next new thing. Let’s see.

Older posts: 1 ... 6 7 8