Gifted Slacker

The dog always eats my homework

Archive for June 2009

Ruby LOAD_PATH

leave a comment »

I cleaned up an example from Giles’ post about issues with common Ruby idiom:

$:.unshift(File.dirname(__FILE__))

Giles gives an example for expanding relative paths, which I’ve cleaned up very minorly (he forgot the slash, big whoop):

class File
  def self.here(string)
    join(expand_path(dirname(__FILE__)), string)
  end
end

I suggest reading the previously mentioned post for context. It’s worth it simply for the Highlander joke.

Update: I should have read the comments. Much more good discussion there.

Written by Grant

June 7, 2009 at 11:28 am

Posted in Uncategorized

TweetTail – HTML

leave a comment »

I was reading through some Ruby code on GitHub tonight when I ran across Dr Nic‘s TweetTail utility. TweetTail prints a the latest Twitter search results to the console.

I decided I wanted to write a utility that would generate html listings of different searches I want to follow. I ran into some issues that I couldn’t satisfactorily solve, so I ended up forking and patching TweetTail itself. This is undesirable. I don’t want to have to maintain my own parallel version of TweetTail just to have a little bit different behavior.

I’m going to explain my problem. Hopefully someone will offer a better solution.

TweetPoller is the workhorse. Render_latest_results and format are the methods we’re concerned with:

class TweetTail::TweetPoller
# irrelevant code omitted
#...
  
  def render_latest_results
    @latest_results.inject("") do |output, tweet|
        output += format(tweet)
    end
  end

  def format(tweet)
    screen_name = tweet['from_user']
    message     = tweet['text']
    "#{screen_name}: #{message}\n"
  end

#...
# irrelevant code omitted
end 

What I wanted to do was just override the format method with my own version. I didn’t want to just monkeypatch over the top of it, so I inherited from TweetPoller and provided my own format method:

class HtmlTweetPoller < TweetTail::TweetPoller
  def format(tweet)
    screen_name = tweet['from_user']
    created_at = tweet['created_at']
    link = tweet['source']
    message = tweet['text']
    "<div class='tweet'>#{screen_name}: #{message}<br /> <a href='#{link}'>#{created_at}</a>\n"
  end
end

That didn’t work. It was obvious why after I took another look at the metaprogramming section of the Pickaxe book: The render_latest_results method was still calling TweetPoller’s format method.

Getting around that is easy, you simply override the TweetPoller’s render_latest_results method:

class HtmlTweetPoller < TweetTail::TweetPoller
  def render_latest_results
    @latest_results.inject("") do |output, tweet|
        output += format(tweet)
    end
  end

  def format(tweet)
    screen_name = tweet['from_user']
    created_at = tweet['created_at']
    link = tweet['source']
    message = tweet['text']
    "<div class='tweet'>#{screen_name}: #{message}<br /> <a href='#{link}'>#{created_at}</a>\n"
  end
end

Now I’ve got code that has been copied and pasted from TweetPoller into my own class. That doesn’t make me happy. I haven’t come up with a situation where it’s actually a problem. It just seems suspect to me.

This is where I’m stuck. This is the point I decided to modify TweetTail.

Any guidance is greatly appreciated.

Written by Grant

June 7, 2009 at 12:14 am

Posted in Uncategorized

Work Is Great

leave a comment »

I’m having a blast at work. Definitely enjoying it much more than I would have guessed.

I recently got to start throwing a little bit of my work time (and quite a few off-the-clock hours) into learning Django + Pinax and getting them setup as a blogging/wiki/forum platform.

I think having two or more projects is a real good thing for me. It means I have two carrots to motivate myself; Get X amount done on project A and then I can do Y amount of work on project B.

It’s also helpful to me to switch platforms during the day. Most of my work is on WebForms/C#, which harshes my melon. Getting to look at non-evil stuff is very refreshing, even if it’s a tiny amount of my working day.

(Apologies for lack of structure and coherence. I wrote this completely in the quickpress widget.)

Written by Grant

June 5, 2009 at 5:08 pm

Posted in Uncategorized

Follow

Get every new post delivered to your Inbox.