Author Archives: joe

About joe

.NET developer and board game geek located in the greater Atlanta region.

Gosu Extensions Update

tests_complete

I’ve finally finished updating my Gosu Extensions. It’s far from perfect, but it’s in a good spot and I’m relatively happy with it. It feels good to get something “done”.

Although my coding process hasn’t been 100% TDD, I would say at least it’s been test centric and irregardless of whether I actually do anything with this, I think it’s been a good experience.

I plan on updating some of my “games” to use this new version, so I can see if these extensions actually save me any work.

Here are a few of the highlights

  • JavaScript like elements and events ( on_click, on_focus etc )
  • Wrappers for Gosu Images, Samples, Fonts and Text
  • Bounding Boxes for basic collision detection
  • Easy cursor support
  • Basic grid / matrix support
  • Basic scheduling system

Enough chit chat, Download the code!

Implementing Prototype’s Event.observe in Ruby – Take 2

I revisited my Event.observe proof of concept and tidied it up a bit with a little help from stackoverflow.com.

Here’s how it do

This here defines a method to determine which method is currently running. There’s a slicker way to do this in 1.9 and you can read about them both on stackoverflow.com.

module Kernel
 private
  def current_method_name
    caller[0] =~ /`([^']*)'/ and $1
  end
end

Here’s my actual observer class, named after it’s inspiration.

class Event

  def initialize
    @events = []
  end

  def observe object, method, callback
    handle   = Handle.new self, object, method, callback
    @events << handle
    handle
  end

  def delete handle
    @events.delete handle
  end

  def call object, method
    handle = Handle.new self, object, method
    @events.each do |e|
      e.call if e.shares_type_with 
    end
  end

end

And finally there's a basic handle class:

class Handle

  attr_reader :observer, :object, :method, :callback

  def initialize observer, object, method, callback = nil
    @observer  = observer
    @object    = object
    @method    = method
    @callback  = callback
  end

  def shares_type_with? handle
    object == handle.object && method == handle.method
  end

  def call
    callback.call
  end

  def stop_observing
    observer.delete self
  end
end

And here's how you use it, In the following example we want our "second" Test_Object to run it's test_method whenever we run the same method for our "first" object.

The important thing here is the "notify" method, you'll need to drop this method into whatever you'd like to be able to observe. If you wanted to be able to observe everything you could always loop through the methods, alias and override.

class Test_Object

  def initialize observer, label
    @observer = observer
    @label    = label
  end

  def notify method
    @observer.call self, method
  end

  def test_method
    puts "Calling => #{@label}"
    notify current_method_name 
  end

end

observer = Event.new

first    = Test_Object.new observer, "First"
second   = Test_Object.new observer, "Second"

h1  = observer.observe(
  first,
  "test_method",
  second.method :test_method
)

first.test_method

# Calling First
# Calling Second
#=> nil

And we can drop the handle by simply calling it's stop_observing method

h1.stop_observing
first.test_method

# Calling First
#=> nil

That's it.

Download the Proof of Concept!

Ruby-er than you!

When I first started with Ruby, I wrote it like I would any other language: Classes, methods, loops, conditionals. It worked, and I appreciated the sweet syntax, but I wasn’t taking full advantage. It’s been a while since I first began with my hobby language, and slowly but surely, my Ruby is becoming more Ruby-like.

I’ve picked up a few tricks and I’m getting better at leveraging the more (see below) advanced features appropriately and effectively. Enough so that I’m often horrified by things I wrote just a few months back. It can be a tad discouraging, but ultimately it’s a good thing because it means I’m getting better.

Besides, as a co-worker of mine likes to point out, how awful would it be if the situation where reversed?

Newayz, here are a few links to some of them fancy fixin’s I was talking about:

Implementing Prototype’s Event.observe in Ruby

I love Prototype. I wanted to implement something akin to Event.observe in some of the gui applications I’ve been playing with. The code’s fugly, but it’s a first pass and I just finished my last Netflix.

Example JavaScript Implementation is pretty:

a = new Test_Object( "First" )
b = new Test_Object( "Second" )

Event.observe(
  a,
  "test_method",
  b.test_method
)

Event.observe(
  b,
  "test_method",
  function(e) { new Test_Object("Third").test_method() }
)

Example Ruby Implementation..not so pretty.

$event = Event.new
a      = Test_Object.new( "First" )
b      = Test_Object.new( "Second" )

$event.observe(
  a,
  "test_method",
  b.method( "test_method" )
)

$event.observe(
  b,
  "test_method",
  Proc.new { Test_Object.new("Third").test_method }
)

a.test_method

Yep, that’s a global. I’m still working on it. Any ideas would be welcome!

Download the Ruby code!

Open Letter to Audio Books Makers

Dear Audio Book Makers.

Your products, in my humble opinion are too expensive. Five yes, ten maybe, thirty NO.

Also, while I have your attention: More IT, please. I understand a lot of the techy books I’m interested in may have code examples or diagrams that don’t translate, but if the price is right I’ll deal. Perhaps you could shoot me a pdf with my purchase?

Finally, I wanted to let you know that I’m okay with text to speech. I don’t know if that would save you, and ultimately me, any money but It’s not so bad these days. I might have abandoned you completely for your cousin the eBook Makers, had the Kindle not been smote. Although, a cursory search reveals that I might have some of the same issues with them. Maybe I’ll write them a letter too.

For now, I’ll stick to my podcasts. I’ll check back with you later.

ID3Mapper – A Ruby ID3 renaming utility

I have a Zune. At the time I had a working Windows box, hated iTunes, and thought I would actually use the radio tuner and I actually really like it. The desktop software…not so much.

There’s only a Windows version, it runs horribly when you have a lot of artists, it only picks up half of my podcasts. I figured out a while back that the reason my podcasts weren’t showing up had to do with the id3 tags, so I wrote a horrible little ruby script ( utilizing id3lib-ruby ) that would set the correct genre of “podcast” for everything in my podcast directory.

For fun, I thought it would be cool to write a little desktop app using the most popular Ruby gui toolkit Shoes that would make it easier for me to correct some other id3 tag irregularities in my media collection.

The first step was to re-factor my id3 script. I’ve been tooling around with automated testing a bit over the last couple months and I thought this would be an excellent chance for me to attempt a full-on test first methodology. I did, and I’m pretty happy how it turned out, although it certainly didn’t save me any time.

Click to download the new and improved id3mapper.

I’ll eventually package this up into it’s own module, but for now this is how it works:

require '../settings'
i = ID3Mapper_Console.new

i.set_artist "/home/joe/Podcasts/WNYC Radio Lab", "Radio Lab"
i.set_genre "/home/joe/Podcasts/WNYC Radio Lab", "podcast"

ColdFusion Button Maker

button maker

I started working on a button maker using ColdFusion’s built in image functions. The ultimate goal is to create graphical buttons using an simple and quick interface. Steps being as follows:

1. Select (or create a new) configuration consisting of background images, and fonts
2. Type in some text and drag (if necessary) the text to the desired location.
3. Export!

For now all I’ve done is set up the base cfc’s for the image stretching and text writing. Next up is the interface. You can check out the results from a test script, or just download the code.