<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="wordpress/2.3.3" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	>

<channel>
	<title>Stefan Kleine Stegemann</title>
	<link>http://stefankst.net</link>
	<description>Weblog</description>
	<pubDate>Wed, 17 Oct 2007 18:17:28 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.3.3</generator>
	<language>en</language>
			<item>
		<title>Using the Dashboard for Todo-Management</title>
		<link>http://stefankst.net/2007/10/12/using-the-dashboard-for-todo-management/</link>
		<comments>http://stefankst.net/2007/10/12/using-the-dashboard-for-todo-management/#comments</comments>
		<pubDate>Fri, 12 Oct 2007 14:57:01 +0000</pubDate>
		<dc:creator>Stefan Kleine Stegemann</dc:creator>
		
		<category><![CDATA[Misc]]></category>

		<guid isPermaLink="false">http://stefankst.net/2007/10/12/using-the-dashboard-for-todo-management/</guid>
		<description><![CDATA[Keeping a list of todos unquestionably relieves stress from our lives. A whole community has evolved around task management and getting things done. While I&#8217;m not a particular fond of the latter, I&#8217;d still like to maintain a simple list of todos. For a long time, I wrote them down to index cards, stickies, whiteboards [...]]]></description>
			<content:encoded><![CDATA[<p>Keeping a list of todos unquestionably relieves stress from our lives. A whole community has evolved around task management and <a href="http://en.wikipedia.org/wiki/Getting_Things_Done">getting things done</a>. While I&#8217;m not a particular fond of the latter, I&#8217;d still like to maintain a simple list of todos. For a long time, I wrote them down to index cards, stickies, whiteboards and the like. However, keeping organized is rather difficult if the information is spread over the whole desk or even over different locations. So I made several attempts to organize my todos at the computer. I tried plain text-files, outliners, <a href="http://orgmode.org/">emacs org-mode</a> (which is btw. a great peace of software) and GTD applications. Despite there a quite a few remarkable applications out there, none of them lasted for long on my hard-drive. The problem was that I never felt comfortable with the idea of organizing my tasks in a hierarchical structure. It seems that it just doesn&#8217;t fit with the way my brain works. In fact, I&#8217;m not a big fan of hierarchical structures in general. I often wonder how people use Windows Explorer, with tons of folders open ten levels deep, and still manage to keep an overview. I&#8217;m really distracted by those applications because it feels like they hurt my brain.</p>
<p>The problem with the hierarchical approach is that it is too limited. The world is not organized in hierarchies. My desk, for example is organized in a spacial way. Stickies can be moved around on the desk in order to reflect relationships like dependencies or subjects. I can use different colors to define priorities. If there are too many stickies floating around on my desk, I can build piles and work them down from top to bottom. Most of this is lost with the hierarchical approach. Which is why I think that I&#8217;m not working very well with most of the todo- and task-management applications.</p>
<p>With Mac OS 10.4, Apple gave us the Dashboard. I was always unsure about the value of this &#8220;invention&#8221;. For some time, my Dashboard hosted a variety of widgets for different purposes. However, the longer I used the system, the less I looked at the Dashboard. Things like the calculator widget are difficult to use if you want to copy numbers from an application: copy text, activate Dashboard, paste text into calculator, deactivate Dashboard and so on. Not exactly a good usability. The awareness of things happening on the Dashboard is very limited because it is not visible unless you activate it. So over time, the Dashboard literally faded out of my mind.</p>
<p>It remained unused until I recently discovered a new way of using it: manage my todos. Luckily, Apple bundled a simple stickies widget with the Dashboard. So here is my current approach.</p>
<div style="text-align: center">
   <a href='http://stefankst.net/wp-content/uploads/2007/10/dashboard-notes.jpg'  title='Dashboard-Notes_small.jpg'><img src='http://stefankst.net/wp-content/uploads/2007/10/dashboard-notes_small.jpg' alt='Dashboard-Notes_small.jpg' /></a><br />Todos on the Dashboard
</div>
<p>The Dashboard hosts nothing but stickies. When I want to create a new todo, I create a new instance of the sticky widget. Stickies can be colored and moved around freely. This way, I can organize my todos in a spacial way. I create piles of stickies that are not so important at the moment. Colors are used to define priority of a todo. So far, I&#8217;m quite happy with this approach. It&#8217;s not important that the Dashboard is always in the background since I don&#8217;t need (and want) to see the todos all the time. There a few problems with this approach though. One is limited space. Especially on my 12&#8243; PowerBook the screen gets filled up with stickies very quickly. A &#8220;virtual&#8221; Dashboard with an unlimited size and a zoom function would be helpful. An archive function for completed todos and integration with other applications (Mail, Firefox etc.) would be nice too. The basic idea, however, works quite well for me, so I guess I will stick with it for some more time.</p>
]]></content:encoded>
			<wfw:commentRss>http://stefankst.net/2007/10/12/using-the-dashboard-for-todo-management/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Capture standard output in Ruby</title>
		<link>http://stefankst.net/2007/06/05/capture-standard-output-in-ruby/</link>
		<comments>http://stefankst.net/2007/06/05/capture-standard-output-in-ruby/#comments</comments>
		<pubDate>Tue, 05 Jun 2007 15:17:44 +0000</pubDate>
		<dc:creator>Stefan Kleine Stegemann</dc:creator>
		
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://stefankst.net/2007/06/05/capture-standard-output-in-ruby/</guid>
		<description><![CDATA[Recently, I wrote a little Ruby script where I wanted to capture the data that is send to standard output. To make things clear, it&#8217;s not about capturing output from a subprocess. What I wanted to do was calling a method and redirecting everything this method writes to standard output to a string buffer. It [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, I wrote a little Ruby script where I wanted to capture the data that is send to standard output. To make things clear, it&#8217;s not about capturing output from a subprocess. What I wanted to do was calling a method and redirecting everything this method writes to standard output to a string buffer. It took me a while to figure out how this can be done, so I thought I better write it down. Maybe this is helpful also for someone else.</p>
<p>Ruby has two ways to access standard output. First, there is a global constant <code>STDOUT</code> that refers to an IO object. You can print data to standard output by sending for example a puts message to <code>STDOUT</code>.</p>
<pre class="codesnippet">
STDOUT.puts "Hello, World!"
=> Hello, World!
</pre>
<div class="note">
(This code is rarely seen in Ruby programs. The Kernel module also provides methods like puts and print which delegate to standard output, so one can simply write <code>puts "Hello, World!</code> instead of <code>STDOUT.puts "Hello, World!"</code>).
</div>
<p>The second way to access standard output is through the global variable <code>$stdout</code>. Both, <code>STDOUT</code> and <code>$stdout</code> refer to the same IO object, so you can substitute <code>STDOUT</code> with <code>$stdout</code> in the example above. The difference, however, is that <code>STDOUT</code> is a global constant and <code>$stdout</code> is a variable. This means that you cannot assign a new value to <code>STDOUT</code> (well, you can, but Ruby will issue a warning and you will burn in programmer&#8217;s hell forever if you ignore it). But what you can do is assign another value to <code>$stdout</code>. We will see in a moment why this is important.</p>
<p>My first attempt was to use the reopen method that is provided by the IO class. This method takes an IO object or a path and optionally a mode string. In the first case, reopen re-associates the receiver with the given IO object. In the second case, reopen opens a new output stream on the path and then re-associates the receiver with this stream.</p>
<pre class="codesnippet">
old_stdout = STDOUT.dup
STDOUT.reopen('/tmp/ruby-output')
puts "Hello, World!"
STDOUT.reopen(old_stdout)
</pre>
<p>In the example above, we use reopen on <code>STDOUT</code> to send all data that is written to standard output to the file /tmp/ruby-output (warning: don&#8217;t try this in irb since after <code>STDOUT.reopen(...)</code>, you won&#8217;t see any more output!). So far so good, but as I mentioned earlier, I wanted to have the output in a string. Of course, we could use <code>File.read('/tmp/ruby-output')</code> to read the contents of the file into a string but well, that&#8217;s not exactly an elegant solution. What we really want is an alternative implementation of IO that collects all output into a string. Searching the ruby documentation quickly revealed <code>StringIO</code>, an IO compatible class that provides pseudo I/O on a <code>String</code> object. Fine, so let&#8217;s try it out:</p>
<pre class="codesnippet">
old_stdout = STDOUT.dup
out = StringIO.new
STDOUT.reopen(out)
puts "Hello, World!"
STDOUT.reopen(old_stdout)
puts out.string
</pre>
<p>Opposed to the previous example, we don&#8217;t reopen <code>STDOUT</code> to a file but to a StringIO object. Sadly, this doesn&#8217;t work. The <code>reopen</code> statement fails with &#8220;can&#8217;t convert StringIO into String (TypeError)&#8221;. It looks like reopen does not accept the StringIO object. In fact, the implementation of <code>IO#reopen</code> checks if the first argument is an IO object. If not, the argument is converted to a string and a new IO object is opened on the path represented by that string. The error occurs because reopen does not recognize the StringIO object as a valid IO object and fails to convert it to a string. Typing <code>StringIO.superclass</code> into irb reveals Data as the superclass of StringIO! Since reopen checks whether the class of the first argument is_a?(IO), it does not accept our StringIO object as an IO object.</p>
<p>As a side node, the IO implementation does not follow the concept of duck typing here (while it could do so easily). Instead of checking the base-class of the first argument, it would be better to check if the object behaves like an IO object. It doesn&#8217;t really matter whether its class is a subclass of IO. As long as it behaves like an IO, it should be ok (duck typing: &#8220;looks like a duck, walks like a duck, must be a duck!&#8221;). Checking the behavior at this point would mean to check that the object responds to messages that could be send to an IO object. In fact, reopen could simply omit the check and let errors happen as soon as messages are send to the object that it doesn&#8217;t understand.</p>
<p>So, back to our reopen problem. A solution would be to create a subclass of IO that simply delegates all messages to a StringIO object. Or we could implement our own StringIO class. Both solutions are feasible, but require some work and add complexity to the application. Luckily, we still have the global variable <code>$stdout</code>. Since it is a variable, we can assign it a new value.</p>
<pre class="codesnippet">
old_stdout = $stdout
out = StringIO.new
$stdout = out
puts "Hello, World!"
$stdout = old_stdout
puts out.string
</pre>
<p>Simply assigning a StringIO object to <code>$stdout</code> does the trick. Since the implementation of <code>Kernel#puts </code>delegates to <code>$stdout</code>, the output is send to our StringIO object. This solution has a few drawbacks, however. First, it doesn&#8217;t work if someone writes to standard output directly via <code>STDOUT</code>.  Second, output from subprocesses is not captured. If this is a problem, you need to reopen <code>STDOUT</code>, either to a file or to a custom IO object. If not, the simple solution from above should work fine.</p>
<p>There is one more pitfall in the solution (that also applies to the reopen solution). If an exception occurs between <code>$stdout=out</code> and <code>$stdout = old_stdout</code>, <code>$stdout</code> won&#8217;t be set back to its original value. To avoid this problem, we have to surround our &#8220;business code&#8221; with a <code>begin...ensure</code> block.</p>
<pre class="codesnippet">
old_stdout = $stdout
out = StringIO.new
$stdout = out
begin
   puts "Hello, World!"
ensure
   $stdout = old_stdout
end
puts out.string
</pre>
<p>Now, the ratio of &#8220;infrastructure code&#8221; to &#8220;business code&#8221; is not very good. Well, it wasn&#8217;t good before either :-). We have exactly two lines where the actual work is done and seven lines dealing with standard output redirection. Fortunately, Ruby gives us blocks so we could easily encapsulate the infrastructure code.</p>
<pre>
def with_stdout_captured
   old_stdout = $stdout
   out = StringIO.new
   $stdout = out
   begin
      yield
   ensure
      $stdout = old_stdout
   end
   out.string
end

out = with_stdout_captured do
   puts "Hello, World!"
end
puts out
</pre>
<p>The method with_stdout_captured makes the code easier to read. Not only does it hide the details of redirecting standard output but it also clearly reveals the intention when someone else is reading the code.</p>
<p>Ok, this was a rather lengthy post about something simple as capturing standard output. However, at least I learned a lot about standard output in Ruby and if you read until here, I hope you enjoyed it.</p>
<p>Links:</p>
<ul>
<li><a href="http://www.ruzee.com/blog/2006/11/redirecting-stdout-to-logger-with-ruby-on-rails/">Redirecting $stdout to Logger with Ruby on Rails</a></li>
<li><a href="http://blog.segment7.net/articles/2006/08/16/setting-stdout-per-thread">Setting $stdout per-thread</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://stefankst.net/2007/06/05/capture-standard-output-in-ruby/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Mixin Module Methods</title>
		<link>http://stefankst.net/2007/05/18/mixin-module-methods/</link>
		<comments>http://stefankst.net/2007/05/18/mixin-module-methods/#comments</comments>
		<pubDate>Fri, 18 May 2007 16:18:17 +0000</pubDate>
		<dc:creator>Stefan Kleine Stegemann</dc:creator>
		
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://stefankst.net/2007/05/18/mixin-module-methods/</guid>
		<description><![CDATA[In Ruby, a module can be included in other modules or classes which adds the features of the included module to the including module or class. This works fine if I want to mixin instance methods into a class.

module Hello
   def say_hello
      puts "Hello, World! Here is #{self.to_s}."
 [...]]]></description>
			<content:encoded><![CDATA[<p>In Ruby, a module can be included in other modules or classes which adds the features of the included module to the including module or class. This works fine if I want to mixin instance methods into a class.</p>
<pre class="codesnippet">
module Hello
   def say_hello
      puts "Hello, World! Here is #{self.to_s}."
   end
end
class Person
   include Hello
   def initialize(name)
      @name = name
   end
   def to_s
      "#{@name}, a Person."
   end
end
Person.new("Stefan").say_hello
=> Hello, World! Here is Stefan, a Person.
</pre>
<p>The <code>include Hello</code> statement in the Person class causes the Ruby interpreter to add all instance methods that are defined in Hello to Person. This works well for instance methods but things get a bit more tricky if we want to mixin module or class methods. The following code looks straightforward, but unfortunately doesn&#8217;t work.</p>
<pre class="codesnippet">
module Hello
   def self.say_hello
      puts "Hello, World! Here is #{self.to_s}."
   end
end
module Stefan
   include Hello
end
Stefan.say_hello
=>  undefined method `say_hello' for Stefan::Module (NoMethodError)
</pre>
<p>Here we define a module method say_hello in the module Hello and then include Hello in Stefan, hoping that say_hello will be mixed into Stefan as a module method. But Ruby reports a NoMethodError when we try to send the say_hello message to Stefan. Reading the documentation reveals the problem: include works <b>only</b> for instance methods (and constants and module variables). Hence our module method say_hello isn&#8217;t included. Bad Luck.</p>
<div class="note">
<b>Note:</b> There is a more elegant solution to the problem of mixing module/class methods into modules and classes in the update at the end of this post. However, if you are interested in how singleton classes work, the next paragraphs may also be worth reading.
</div>
<p>Fortunately, we can work around this. Ruby has a feature called <emph>singleton classes</emph> (also known as <emph>metaclasses</emph>). Singleton classes are used to add methods to objects. Normally, the methods provided by an object are determined by it&#8217;s class. You can&#8217;t store methods directly in an object. But as we know, Ruby allows us to do this. You can define a method that belongs only to a specific object.</p>
<pre>
stefan = Object.new
def stefan.say_hello
   puts "Hello, World! Here is Stefan."
end
stefan.say_hello
=> Hello, World! Here is Stefan.
</pre>
<p>In this example, we create a new object &#8217;stefan&#8217; and add a method say_hello to &#8217;stefan&#8217;. Now how does this work if we can&#8217;t store methods in an object? The solution is that the method is not added to the object &#8217;stefan&#8217; but to it&#8217;s singleton class. This class does not affect an object&#8217;s inheritance chain, &#8217;stefan&#8217; is still an Object. A singleton class intercepts the messages send to an object before it goes up the inheritance chain. If the singleton class can respond to a message it will do so. If not, the message is passed up through the inheritance chain to see if another class can handle that message.</p>
<p>Back to our module mixin problem. In Ruby everything is an object. A module is really an object of class Module. And as every object, a module can also have a singleton class. If we define a module method, the method isn&#8217;t really added to the module object but to it&#8217;s singleton class. The same happens for class methods, by the way. In fact, we could write a module method as follows.</p>
<pre class="codensippet">
module Stefan
   class << self
      def say_hello
         puts "Hello, World! Here is Stefan."
      end
   end
end
Stefan.say_hello
=> Hello, World! Here is Stefan.
</pre>
<p>The <code>class << self</code> syntax opens a singleton class, in this case for the enclosing module Stefan. In the singleton class, we define the method say_hello. The same happens behind the scenes if you write <code>def self.say_hello</code>. If we now send the message &#8217;say_hello&#8217; to Stefan, first the module&#8217;s singleton class will get the opportunity to respond to the message. Since our singleton class has a method say_hello, it will handle the message by calling this method. If the singleton class doesn&#8217;t know how to respond to a message, Stefan&#8217;s class (Module) will get the chance to handle it. Be aware that Stefan is a module, and a module is an object of class Module!</p>
<p>With this in mind, we now have a solution for mixing module methods into a module. We first have to define a module that provides the methods to be included. We than include this module into another module&#8217;s <emph>singleton class</emph>, not in the module itself.</p>
<pre class="codesnippet">
module Hello
   def say_hello
      puts "Hello, World! Here is #{self.to_s}."
   end
end
module Stefan
   class << self; include Hello; end
end
Stefan.say_hello
=> Hello, World! Here is Stefan.
</pre>
<p>Et voila, say_hello is now a module method of Stefan. Note that say_hello is defined in Hello as an instance method, not as a module method, and how Hello is included in Stefan. This technique works for mixing class methods into classes as well.</p>
<p><b>Update (May 19, 2007):</b></p>
<p><a href="http://rubyreports.org/">Gregory Brown</a> commented about a much cleaner solution: simply use object.extend which adds all instance methods from the modules given as parameter to object. Since modules and classes are objects, we can send them the extend message.</p>
<pre class="codesnippet">
module Hello
   def say_hello
      puts "Hello, World! Here is #{self.to_s}."
   end
end
module Stefan
   extend Hello
end
Stefan.say_hello
=> Hello, World! Here is Stefan.
</pre>
<p>Works like the same but looks much cleaner than my solution. As Gregory pointed out, we could also use a combination of extend and include to mixin instance and module methods at the same time.</p>
<pre class="codesnippet">
module Hello
   module ClassMethods
      def say_hello
         puts "Hello, World! Here is #{self.to_s}."
      end
   end
   def included(base)
      base.extend(ClassMethods)
   end
end
module Stefan
   include Hello
end
Stefan.say_hello
=> Hello, World! Here is Stefan!
</pre>
<p>In this example, the &#8216;included&#8217; callback is used. This callback is invoked on a module whenever it is included in another module or class. The parameter &#8216;base&#8217; holds the including module or class. Note that the say_hello method is placed in a separate module ClassMethods. The example uses the included callback to extend base with the methods from this module when base includes Hello. This way, the methods defined in ClassMethods become module methods of Stefan when it includes Hello.</p>
<p>Gregories solution is obviously much more elegant than my approach since you don&#8217;t need to cope with singleton classes. To my excuse, I wasn&#8217;t aware of the extend methods and it&#8217;s function. However, at least I learned a lot about singleton classes when writing this post :-).</p>
<p>Links:</p>
<ul>
<li><a href="http://www.whytheluckystiff.net/articles/seeingMetaclassesClearly.html">Seeing Metaclasses Clearly</a> for a great and fun to read introduction to metaclasses (just another name for singleton classes).</li>
<li><a href="http://ola-bini.blogspot.com/2006/09/ruby-singleton-class.html">The Ruby singleton class</a> for another good introduction to singleton classes.</li>
<li><a href="http://redcorundum.blogspot.com/2006/06/mixing-in-class-methods.html">Mixing in Class Methods</a> for another blog post about mixing in class methods.</li>
<li><a href="http://www.ruby-forum.com/topic/68638#new">Why the lack of mixing-in support for Class methods?</a> A discussion in the ruby forum about why class methods are not included.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://stefankst.net/2007/05/18/mixin-module-methods/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Language Expressiveness</title>
		<link>http://stefankst.net/2007/04/11/language-expressiveness/</link>
		<comments>http://stefankst.net/2007/04/11/language-expressiveness/#comments</comments>
		<pubDate>Wed, 11 Apr 2007 21:31:36 +0000</pubDate>
		<dc:creator>Stefan Kleine Stegemann</dc:creator>
		
		<category><![CDATA[Languages]]></category>

		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://stefankst.net/?p=6</guid>
		<description><![CDATA[Programming languages vary in their expressiveness. Why is expressiveness important? Because it&#8217;s closely related to abstraction. If I can code at a higher level of abstraction, I&#8217;m more productive and the code usually better expresses what it achieves. Example? Determine the date at 12 days and 5 hours in the future:

SMALLTALK:
future := DateAndTime now + [...]]]></description>
			<content:encoded><![CDATA[<p>Programming languages vary in their expressiveness. Why is expressiveness important? Because it&#8217;s closely related to abstraction. If I can code at a higher level of abstraction, I&#8217;m more productive and the code usually better expresses what it achieves. Example? Determine the date at 12 days and 5 hours in the future:</p>
<pre class="codesnippet">
SMALLTALK:
future := DateAndTime now + 12 days + 5 hours.

JAVA:
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
cal.add(Calendar.DAY_OF_MONTH, 12);
cal.add(Calendar.HOUR_OF_DAY, 5);
Date future = cal.getTime();
</pre>
<p>(Sun could at least make Calendar&#8217;s add method return the Calendar itself so that we could daisy-chain the add calls like cal.add(&#8230;).add(&#8230;)).</p>
<p>Good old Smalltalk obviously wins. And it wins not just because the libraries are so well designed (I have to admit that the example is a bit unfair because the Java Calendar class really represents the bottom end of API design). It wins because expressiveness is build right into the language. Surprisingly, the Smalltalk language with its very few concepts is more expressive than Java that is way more complex, especially these days where Generics hit the ground. Here, the &#8220;everything is an object&#8221; metaphor together with dynamic typing makes the difference.</p>
]]></content:encoded>
			<wfw:commentRss>http://stefankst.net/2007/04/11/language-expressiveness/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Delicious Hpricot</title>
		<link>http://stefankst.net/2007/04/11/delcious-hpricot/</link>
		<comments>http://stefankst.net/2007/04/11/delcious-hpricot/#comments</comments>
		<pubDate>Wed, 11 Apr 2007 13:38:41 +0000</pubDate>
		<dc:creator>Stefan Kleine Stegemann</dc:creator>
		
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://stefankst.net/?p=4</guid>
		<description><![CDATA[I keep a list of the blogs I frequently read in my del.ico.us bookmarks. Every blog I&#8217;m currently interested in is bookmarked under the tag &#8220;blogroll&#8221;. I prefer reading the blogs in a web browser over feed readers or aggregators. Therefore a bookmark points to the blog&#8217;s webpage and not to an atom or rss [...]]]></description>
			<content:encoded><![CDATA[<p>I keep a list of the blogs I frequently read in my <a href="http://del.ico.us/stefankst">del.ico.us</a> bookmarks. Every blog I&#8217;m currently interested in is bookmarked under the tag &#8220;blogroll&#8221;. I prefer reading the blogs in a web browser over feed readers or aggregators. Therefore a bookmark points to the blog&#8217;s webpage and not to an atom or rss feed. </p>
<p>Yesterday I wanted to create a list with the authors of all blogs I&#8217;m currently reading. Unfortunatly, there is no way to safely extract this information from a webpage (oh dear, we are still lightyears away from the semantic web). The only way to discover details about the author is to look at a blog&#8217;s news feed. Atom feeds usually have an <code>author</code> element with meta information about the author. Rss feeds don&#8217;t but at least the email address of the creator can be extracted from most feeds. Luckily any blog I&#8217;m aware of provides a feed in atom or rss format. Information about available feeds is usually encoded in the header of a blog&#8217;s webpage. For each feed, a <code>link</code> element with <code>rel="alternate"</code> exists that specifies the feed&#8217;s URL and type. So the solution to my problem was to extract the feed URLs from a webpage, load the feed and get the author from the feed header.</p>
<p>Now I could have used regular expression to extract the alternate links. However, I recently came accross <a href="http://code.whytheluckystiff.net/hpricot/">Hpricot,</a> &#8220;a fast, enjoyable HTML parser for ruby&#8221;. And while I find regexps quite useful, I&#8217;m not a particular fan of them, especially when they get complex. So I decided to give Hpricot a try. I wrote a little ruby script to extract feed URLs from a webpage. At its center is the following code:</p>
<pre class="codesnippet">
   def discover_feed(href)
      doc = Hpricot(open(href))
      feeds = []
      doc.search('//head/link[@rel=alternate]').each do |candidate|
         if candidate['type'] =~ /^([a-zA-Z]+\/)?([a-zA-Z]*)+.*$/
            format = $2.downcase
            feeds << {:address => candidate[&#8217;href&#8217;], :format => format}
         end
      end
   end
</pre>
<p>Thanks to Hpricot&#8217;s XPath-Style queries, finding all alternate links in the header is almost a no-brainer. I then use a regular expression to extract the feed&#8217;s format from the link&#8217;s type attribute which specifies a mime-type. The discover_feed function returns an array of hashes, one for each feed. I can now easliy select a feed:</p>
<pre class="codesnippet">
   avail_feeds = discover_feeds(webpage)
   feed = avail_feeds.find {|f| f[:format] == 'atom'} || avail_feeds.first
</pre>
<p>The second line looks for an atom feed in the first place and falls back the first available feed if no atom feed has been discovered. Quite simple, it took me around 30 minutes to write the whole script (including installation of Hpricot). Working out a regular expression would have taken much longer (at least for me). Not to talk of the nightmare of remembering what the expression was meant to do when looking at the code next month.</p>
]]></content:encoded>
			<wfw:commentRss>http://stefankst.net/2007/04/11/delcious-hpricot/feed/</wfw:commentRss>
		</item>
		<item>
		<title>IE sucks</title>
		<link>http://stefankst.net/2007/04/11/ie-sucks/</link>
		<comments>http://stefankst.net/2007/04/11/ie-sucks/#comments</comments>
		<pubDate>Wed, 11 Apr 2007 13:20:51 +0000</pubDate>
		<dc:creator>Stefan Kleine Stegemann</dc:creator>
		
		<category><![CDATA[Misc]]></category>

		<guid isPermaLink="false">http://stefankst.net/?p=5</guid>
		<description><![CDATA[Nothing new, I know. However, I just discoverd that Internet Explorer scrambles my brand new weblog. Among some other odds and ends the header is not displayed correctly. The layout is decent in Safari, Firefox and companions. It is even displayed correctly on my Nokia N80 browser. Only IE is not able to get it [...]]]></description>
			<content:encoded><![CDATA[<p>Nothing new, I know. However, I just discoverd that Internet Explorer scrambles my brand new weblog. Among some other odds and ends the header is not displayed correctly. The layout is decent in Safari, Firefox and companions. It is even displayed correctly on my Nokia N80 browser. Only IE is not able to get it right. So here is my strategy: if you are looking at this page in IE and you are disgusted by the broken layout, go and <a href="http://www.mozilla.com/">download Firefox</a>. I will not spend a minute to tweak this site for IE.</p>
]]></content:encoded>
			<wfw:commentRss>http://stefankst.net/2007/04/11/ie-sucks/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Problem - Solution</title>
		<link>http://stefankst.net/2007/04/09/problem-solution/</link>
		<comments>http://stefankst.net/2007/04/09/problem-solution/#comments</comments>
		<pubDate>Mon, 09 Apr 2007 08:21:40 +0000</pubDate>
		<dc:creator>Stefan Kleine Stegemann</dc:creator>
		
		<category><![CDATA[Writing]]></category>

		<guid isPermaLink="false">http://stefankst.net/?p=3</guid>
		<description><![CDATA[I write quite a few research papers these days. And I read many of them. I often find myself stuck in a paragraph or sentence. Its the feeling that something is wrong, that the argumentation is unclear or that I don&#8217;t know how to continue. The same happens when I read papers. Sometimes the argumentation [...]]]></description>
			<content:encoded><![CDATA[<p>I write quite a few research papers these days. And I read many of them. I often find myself stuck in a paragraph or sentence. Its the feeling that something is wrong, that the argumentation is unclear or that I don&#8217;t know how to continue. The same happens when I read papers. Sometimes the argumentation seems clear but I feel that there is some important bit missing. In (luckily) rare cases, it seems that really I don&#8217;t get what the authors want to say. Most often the solution to the problem is the problem itself: <strong>WHAT IS THE PROBLEM?</strong> To me, that&#8217;s the most important question for writing and reading. This is a lesson that I&#8217;ve learned during many interesting and valuable discussions with a good colleague and friend. So my solution to the &#8220;stuck writer situation&#8221; is to get the problem right.</p>
<ul>
<li>Write down the problem</li>
<li>Get the argumentation for the problem right (references from literature)</li>
<li>Describe and discuss the solution</li>
<li>STICK TO THE PROBLEM. For me, it&#8217;s often tempting telling the reader about too many side-aspects. I would bet that in 99% this leads me to the wrong track.</li>
</ul>
<p>While the solution is simple, its surprises me how often I make the same mistake of not getting the problem right or leaving the track. It just happened some minutes ago which motivated me to write this post. And its not me alone. Many scientific papers fail in getting the problem right or in justifying the problem. In the worst case, the result is some unclear bla-bla about this and that. It feels that such a paper never hits the spot. And yes, I wrote such papers myself.<br />
Hitting the spot is IMO what makes articles from authors like Martin Fowler such a pleasure to read. He has a problem and writes about the solution to this problem using a great style of writing. Simple as that.</p>
]]></content:encoded>
			<wfw:commentRss>http://stefankst.net/2007/04/09/problem-solution/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
