Fun with Wordle

I saw a post on proggit about using tag clouds to detect code noise and thought I’d have some Wordle fun of my own.

Here’s a tag cloud I generated off of my current rss feed.
My Tag Cloud

And here’s a tag cloud I generated off of my google reader feed.
My Feeds

I think my feed cloud is skewed towards tech news since I pulled it up so early, before all the lazy developer bloggers got to posting. Also, I just realized that the images I pulled are both really similar. There are a lot of options for messing with the fonts, colors and layouts…I’m just boring.

Searching Twitter with Ruby

After seeing this a few days ago, I thought it’d be fun and easy to whip up a little script that would dump tweets from around my area. Unfortunately, it was neither. I won’t bore you with my troubles, suffice it to say GitHub has now been added to my list of gem repositories.

I haven’t gone through much of the actual api yet, but so far it looks great. I did play around with a few different wrappers before finally getting down to business with twitter_search, which is nice, and thin, and jived nicely with my goal.

So here’s the code to fetch the last 15 tweets from ( thanks geocoder! ) my area. Simple, eh?

require 'rubygems'
require 'twitter_search'

tweets = TwitterSearch::Client.new.query :geocode => "28.599630,-81.289176,2mi"

tweets.each do |t|
  puts "@#{t.from_user} - #{t.text}n"
end

The next step is to figure out how to look up geocodes automatically, and maybe build some sort of web interface. But not tonight.

Implicit Getters and Setters in ColdFusion

Thanks to CF8’s new onMissingMethod method, it’s trivial to implement implicit getters and setters. As easy as it is, I couldn’t google up any code. Since it’s not the kind of thing I’d rather search for than write myself, I thought I’d go ahead and do you the favor and post it here.

I realize there’s a nasty stigma attached and I don’t disagree that it’s bad practice, but it does come in handy when building a proof of concepts or programming exploratoraly.

So here you go, irregardless of whether or not it’s bad practice:

<cfcomponent name="BaseObject">

	<cffunction name="Init" output="no">
		<cfargument name="instance" default="#StructNew()#"/>

		<cfset SetInstance( arguments.instance )/>

		<cfreturn this/>
	</cffunction>

	<cffunction name="OnMissingMethod" output="no">
		<cfargument name="missingmethodname"      required="yes" />
		<cfargument name="missingmethodarguments" />

		<cfset var prefix = Left( arguments.missingmethodname, 3 ) />
		<cfset var suffix = Mid( arguments.missingmethodname,
			4,
			Len( arguments.missingmethodname )
		) />

		<cfif ( CompareNoCase( prefix, "get" ) eq 0 ) and Has( suffix ) >
			<cfreturn Get( suffix )/>
		</cfif>
		<cfif CompareNoCase( prefix, "set" ) eq 0>
			<cfreturn Set( suffix, arguments.missingmethodarguments.1 )/>
		</cfif>

		<cfthrow message="Method #arguments.missingmethodname# not found" />
	</cffunction>

	<!--- private on down --->

	<cffunction name="GetInstance" output="no" access="private">
		<cfreturn variables.instance/>
	</cffunction>

	<cffunction name="Get" output="no" access="private">
		<cfargument name="field" required="yes"/>

		<cfset var instance = GetInstance()/>

		<cfreturn instance[ arguments.field ]/>
	</cffunction>

	<cffunction name="Has" output="no" access="private">
		<cfargument name="field" required="yes"/>

		<cfreturn StructKeyExists( GetInstance(), arguments.field )/>
	</cffunction>

	<cffunction name="Set" output="no" access="private">
		<cfargument name="field" required="yes"/>
		<cfargument name="value" required="yes"/>

		<cfset var instance = GetInstance()/>
		<cfset instance[ arguments.field ] = arguments.value/>

		<cfreturn Get( arguments.field )/>
	</cffunction>

	<cffunction name="SetInstance" output="no" access="private">
		<cfargument name="instance" required="yes">

		<cfset variables.instance = arguments.instance/>

		<cfreturn GetInstance()/>
	</cffunction>

</cfcomponent>

Download it and the MxUnit Tests!

IT Conversations and Other Podcasts

I’ve been listening to the IT Conversations Network for a few weeks now, and I couldn’t be happier with it. In short, it provides an aggregated rss feed of free tech podcasts, so if you love podcasts as much as I do, you should give it a shot.

In addition to the always great Stack Overflow Podcast, there were three I found particularly interesting this week.

While I’m on the subject here’s a few of my other favorites. In order of how much I love them:

All free, all great. Go!

MXUnit and Me

mxunit1If you’ve been keeping up with my blog over the last couple months, then you already know that I’ve been experimenting with test driven development. I love the work flow, the way it makes me view my code, and the peace of mind…I just don’t know that it will pay off in my work environment. Hence the experimentation.

Most of my TDD dabbling up to this point has been done in Ruby. I’ve finally gotten around to messing around with some of the major ColdFusion testing frameworks: cfunit, cfcUnit and finally MXUnit.

After doing small projects with each of them, I’ve finally decided to settle down with MXUnit. It’s got decent docs, a nice work around for private methods, I dig the web interface, the eclipse plug-in is great and creating my own stubs for the generator was a snap!

In short: Two thumbs up!

Here’s my test. I’m still really new to this sort of thing, so I’d love feedback should you feel so inclined:


	

		function Setup() {
			this.cfc = CreateObject( "component" , "BaseObject" );
			this.cfc = this.cfc.init();
	
			makePublic( this.cfc, "SetInstance" );
			makePublic( this.cfc, "Set" );
			makePublic( this.cfc, "Has" );
			makePublic( this.cfc, "Get" );
			makePublic( this.cfc, "OnMissingMethod" );
		}
	
		function TestSetInstance() {
			var value = "a";
			AssertEquals( value, this.cfc.SetInstance( value ) );
	
			value = StructNew();
			AssertEquals( value, this.cfc.SetInstance( value ) );
		}
	
		function TestGetInstance() {
			var value = "a";
	
			this.cfc.SetInstance( value );
			AssertEquals( value, this.cfc.GetInstance() );
	
			value = "b";
			this.cfc.SetInstance( value );
			AssertEquals( value, this.cfc.GetInstance() );
		}
	
		function Testinit() {
			var value = "a";
	
			this.cfc = this.cfc.init( value );
			AssertEquals( value, this.cfc.GetInstance() );
	
			this.cfc = this.cfc.init();
			AssertEquals( StructNew(), this.cfc.GetInstance() );
		}
	
		function TestSet() {
			var field = "a";
			var value = "1";
	
			AssertEquals( value, this.cfc.Set( field, value ) );
	
			field = "w";
			value = "2";
	
			AssertEquals( value, this.cfc.Set( field, value ) );
		}
	
		function TestHas() {
			var field = "a";
			var value = "1";
	
			AssertEquals( false, this.cfc.Has( field ) );
	
			this.cfc.Set( field, value );
			AssertEquals( true,  this.cfc.Has( field ) );
	
			field = "w";
			value = "2";
	
			AssertEquals( false, this.cfc.Has( field ) );
		}
	
		function TestGet() {
			var field = "a";
			var value = "1";
	
			this.cfc.Set( field, value );
			AssertEquals( value, this.cfc.Get( field ) );
	
			field = "w";
			value = "2";
	
			this.cfc.Set( field, value );
			AssertEquals( value, this.cfc.Get( field ) );
		}
	
		function OnMissingMethod() {
			var value = "a";
	
			AssertEquals( value, this.cfc.SetSomething( value ) );
			AssertEquals( value, this.cfc.GetSomething() );
		}

	

Click to download the component I’m testing and the file above. Now.

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: