<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
 
 <title>The Ruby Ahead - home</title>
 <link href="http://www.nach-vorne.de/feed/atom.xml" rel="self"/>
 <link href="http://www.nach-vorne.de/"/>
 <updated>2010-05-03T15:11:16+02:00</updated>
 <id>tag:www.nach-vorne.de,2009:jekyll/</id>
 <author>
   <name>Gregor Schmidt</name>
   <email>schmidt@nach-vorne.eu</email>
 </author>

 
 <entry>
   <title>Smaller Update</title>
   <link href="http://www.nach-vorne.de/2010/03/14/smaller-update.html"/>
   <updated>2010-03-14T00:00:00+01:00</updated>
   <id>tag:www.nach-vorne.de,/2010/03/14/smaller-update</id>
   <content type="html">&lt;p&gt;I just wanted to let you know, that this blog just moved from &lt;a href='http://www.mephistoblog.com/'&gt;mephisto&lt;/a&gt; to &lt;a href='http://www.jekyllrb.com/'&gt;jekyll&lt;/a&gt;. This move will make it easier to keep the content online. While I was quit lazy in the past years, I also do not plan to get much more active, in the forcoming future. Having static pages, served by a rock-solid apache web server, will be much more reliable, then the old mongrel + mod_proxy_balance setup.&lt;/p&gt;

&lt;p&gt;Unfortunately I was not able to keep the comments. There were not many of them, so I might copy them manually. All future post will not receive any. I you want to comment on the posts, you may want to send an email. Apart from that, this site will become a one-way street.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Seven Things</title>
   <link href="http://www.nach-vorne.de/2009/1/23/seven-things/index.html"/>
   <updated>2009-01-23T00:00:00+01:00</updated>
   <id>tag:www.nach-vorne.de,/2009/1/23/seven-things/seven-things</id>
   <content type="html">&lt;p&gt;It was way too long ago, that this site was updated. Sorry for that. Let&amp;#8217;s use &lt;a href='http://upstream-berlin.com/2009/01/22/seven-things/'&gt;this meme&lt;/a&gt; as opportunity to start again.&lt;/p&gt;

&lt;p&gt;These are the rules:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Link your original tagger(s), and list these rules on your blog.&lt;/li&gt;

&lt;li&gt;Share seven facts about yourself in the post — some random, some weird.&lt;/li&gt;

&lt;li&gt;Tag seven people at the end of your post by leaving their names and the links to their blogs.&lt;/li&gt;

&lt;li&gt;Let them know they’ve been tagged by leaving a comment on their blogs and/or Twitter.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And here are the facts:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&amp;#8220;The Ruby Ahead&amp;#8221; is a more or less obvious variation of &amp;#8220;The Road Ahead&amp;#8221; - A book by Bill Gates covering the his thoughts on the development of broad band internet, cloud vs. desktop computing and general predictions.&lt;/li&gt;

&lt;li&gt;I really liked writing blog entries about my master&amp;#8217;s thesis, but writing the thesis itself was the horror.&lt;/li&gt;

&lt;li&gt;I still owe &lt;a href='http://chneukirchen.org/'&gt;Christian Neukirchen&lt;/a&gt; a paper copy of my thesis. He gave me permission to use the name &lt;a href='http://www.contextr.org/'&gt;ContextR&lt;/a&gt; and I promised to send him a copy in turn.&lt;/li&gt;

&lt;li&gt;I&amp;#8217;m using &lt;a href='http://pons.eu'&gt;pons.eu&lt;/a&gt; to write this article.&lt;/li&gt;

&lt;li&gt;I&amp;#8217;ve just developed my first mobile website. Everybody complaining about IE6 should be glad.&lt;/li&gt;

&lt;li&gt;The movie &amp;#8220;Forrest Gump&amp;#8221; brings tears to my eyes regularly.&lt;/li&gt;

&lt;li&gt;I really dislike these kinds of &amp;#8220;Stöckchen&amp;#8221; or chain letters.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Concerning the 7th fact I will disobey the laws and not torture 7 others with this kind of stuff.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Done</title>
   <link href="http://www.nach-vorne.de/2008/4/16/done/index.html"/>
   <updated>2008-04-16T00:00:00+02:00</updated>
   <id>tag:www.nach-vorne.de,/2008/4/16/done/done</id>
   <content type="html">&lt;p&gt;Today I finished my master&amp;#8217;s thesis. Hooray. The title is &amp;#8220;ContextR &amp;amp; ContextWiki - Modularisierung von Webanwendungen mit kontextorientierter Programmierung&amp;#8221; or &amp;#8220;Modularization of Web Applications Using Context-oriented Programming&amp;#8221;. An abstract will be available, after it was graded. The full version will be public as well, although I&amp;#8217;m not sure when.&lt;/p&gt;

&lt;p&gt;In the mean time, you might be happy with the code. &lt;a href='https://github.com/schmidt/contextr/'&gt;ContextR&lt;/a&gt; and &lt;a href='https://github.com/schmidt/context_wiki/'&gt;ContextWiki&lt;/a&gt; are available at github.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Euruko 2008</title>
   <link href="http://www.nach-vorne.de/2008/3/19/euruko-2008/index.html"/>
   <updated>2008-03-19T00:00:00+01:00</updated>
   <id>tag:www.nach-vorne.de,/2008/3/19/euruko-2008/euruko-2008</id>
   <content type="html">&lt;p&gt;I will happily attend the Euruko 2008 in Prague. Hope to see you there.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Mechanize Use Case</title>
   <link href="http://www.nach-vorne.de/2008/3/16/mechanize-use-case/index.html"/>
   <updated>2008-03-16T00:00:00+01:00</updated>
   <id>tag:www.nach-vorne.de,/2008/3/16/mechanize-use-case/mechanize-use-case</id>
   <content type="html">&lt;p&gt;I&amp;#8217;m using &lt;a href='http://www.bibsonomy.org'&gt;www.bibsonomy.org&lt;/a&gt; to manage my bibtex entries. This is a must have, when writing any scientific paper. Bibsonomy is like del.icio.us just for bibtex. Among the social and webscraping features to collect bibliography entries it has a bibtex export to create &lt;code&gt;.bib&lt;/code&gt;-files for your local latex project.&lt;/p&gt;

&lt;p&gt;The problem, we will solve today: Fetch the bibtex export from bibsonomy.&lt;/p&gt;

&lt;h3 id='the_older_solution'&gt;The older solution:&lt;/h3&gt;

&lt;pre&gt;&lt;code&gt;wget http://www.bibsonomy.org/bib/user/schmidtwisser?items=1000 -O literatur.bib&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This solution is simple and was useful for a long time. Unfortunately I recently added private bib-entries. Since this solution doesn&amp;#8217;t use any authentication, it only delivers my public entries.&lt;/p&gt;

&lt;h3 id='mechanize'&gt;Mechanize&lt;/h3&gt;

&lt;p&gt;So I needed to either use bibsonomy&amp;#8217;s API, which would need registration, or I need to authenticate, keep the cookie and then fetch my bibliography. Enter &lt;a href='http://mechanize.rubyforge.org/'&gt;mechanize&lt;/a&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;require &amp;quot;rubygems&amp;quot;
require &amp;quot;active_support&amp;quot;
require &amp;quot;mechanize&amp;quot;

config = YAML::load_file(File.join(ENV[&amp;#39;HOME&amp;#39;], 
                                   &amp;quot;.bibsonomy.yml&amp;quot;)).symbolize_keys

agent = WWW::Mechanize.new
page = agent.get(&amp;#39;http://www.bibsonomy.org/&amp;#39;)

form = page.forms.action(&amp;#39;/login_process&amp;#39;).first
form.userName = config[:username]
form.loginPassword = config[:password] 
page = agent.submit(form)

page = agent.get(&amp;quot;/bib/user/#{config[:username]}?items=1000&amp;quot;)

File.open(&amp;quot;literatur.bib&amp;quot;, &amp;quot;w&amp;quot;) do |f|
  f.puts page.body 
end&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It is so easy, I won&amp;#8217;t even explain the code.&lt;/p&gt;

&lt;p&gt;Just one bit: Don&amp;#8217;t embed your password in any public code, so do I. My credentials are therefore stored in my Home-directory.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>The difference between alias_method_chain and plain old super</title>
   <link href="http://www.nach-vorne.de/2008/3/7/the-difference-between-alias_method_chain-and-plain-old-super/index.html"/>
   <updated>2008-03-07T00:00:00+01:00</updated>
   <id>tag:www.nach-vorne.de,/2008/3/7/the-difference-between-alias_method_chain-and-plain-old-super/the-difference-between-alias_method_chain-and-plain-old-super</id>
   <content type="html">&lt;p&gt;Today a simple question arose. How to use &lt;code&gt;alias_method_chain&lt;/code&gt; for class methods a.k.a. singleton methods a.k.a. instance specific methods for class. I just tried to answer the question, but finally I recognized, that it was&amp;#8217;t actually necessary to use &lt;code&gt;alias_method_chain&lt;/code&gt; at all. The aim was to add behavior to &lt;code&gt;validates_presence_of&lt;/code&gt;, an ActiveRecord method, that installs a check to ensure, that a certain value is set, before data is send to the database.&lt;/p&gt;

&lt;p&gt;So let&amp;#8217;s have a look at the prerequisites first. I&amp;#8217;ll translate the actual implementation into an easier example. ActiveRecord will most likely implement class methods in a module, that is mixed into the singleton class of all subclasses. This looks a lot like the following code, where &lt;code&gt;ActiveRecord::Base&lt;/code&gt; becomes &lt;code&gt;Base&lt;/code&gt; and &lt;code&gt;validates_presence_of&lt;/code&gt; becomes &lt;code&gt;class_method&lt;/code&gt;.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class Base
  module ClassMethods
    def class_method
      &amp;quot;base implementation used&amp;quot;
    end
  end

  def self.inherited(sub)
    sub.extend ClassMethods
  end
end&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Whenever this class is subclassed, the inherited method will be called an the class methods added as described. If you want to add more specific behavior to the &lt;code&gt;class_method&lt;/code&gt; you got multiple options. But there is no need to use method aliasing with ActiveSupport&amp;#8217;s &lt;code&gt;alias_method_chain&lt;/code&gt;. Modules, even the ones extending Objects have an inheritance relation. So let&amp;#8217;s use &lt;code&gt;super&lt;/code&gt; powers.&lt;/p&gt;

&lt;h3 id='first_option_reusability'&gt;First option: Reusability&lt;/h3&gt;

&lt;p&gt;Define your own &lt;code&gt;ExtendedBase&lt;/code&gt; that you may mix in to your classes, that need the additional behavior.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;module ExtendedBase
  module ClassMethods
    def class_method
      &amp;quot;extended and &amp;quot; + super
    end
  end
  def self.included(sub)
    sub.extend ClassMethods
  end
end&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This code uses the same &lt;code&gt;ClassMethods&lt;/code&gt; technique as the class above, but the call back is called &lt;code&gt;inherited&lt;/code&gt; this time. It provides a special implementation for &lt;code&gt;class_method&lt;/code&gt; and delegates to the next one in the inheritance chain for the more general stuff.&lt;/p&gt;

&lt;p&gt;It is used like the following:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class A &amp;lt; Base
  include ExtendedBase
end&lt;/code&gt;&lt;/pre&gt;

&lt;h3 id='second_option_ad_hoc_usage'&gt;Second option: Ad hoc usage&lt;/h3&gt;

&lt;p&gt;If no reuse is necessary, you may savely add the behavior to the single class, where you need it. Just open up the metaclass a.k.a. eigenclass a.k.a. singleton class and define your methods. Again, you may simply delegate to the base implementation using super.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class A &amp;lt; Base
  def self.class_method
    &amp;quot;custom and &amp;quot; + super
  end
end&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Hehe. The description was more verbose than the code, but trust me it was still correct.&lt;/p&gt;

&lt;h3 id='never_trust_your_code_without_tests'&gt;Never trust your code without tests&lt;/h3&gt;

&lt;p&gt;To make clear, that everything works as expected, I will add a test. I assume, that all the above definitions are loaded.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;require &amp;quot;rubygems&amp;quot;
require &amp;quot;dust&amp;quot;

unit_tests do
  test &amp;quot;should call all three implementations&amp;quot; do
    assert_equal(&amp;quot;custom and extended and base implementation used&amp;quot;,
                 A.class_method)
  end
end&lt;/code&gt;&lt;/pre&gt;

&lt;h3 id='conclusion_know_your_inheritance_chain'&gt;Conclusion: Know your inheritance chain&lt;/h3&gt;

&lt;p&gt;You should always have a look at your tools and know how your inheritance hierarchy looks like. It can things so much easier. Ruby&amp;#8217;s method lookup follows a simple rule.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Definitions in the class itself&lt;/li&gt;

&lt;li&gt;Definitions in included modules, modules included later are used first&lt;/li&gt;

&lt;li&gt;Definitions in super classes&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;It is always possible to jump from one implementation to the other with the use of super. And if you are not sure, which implementation will be used first, ask your &lt;a href='http://ruby-doc.org/core/classes/Module.html#M001700'&gt;&lt;code&gt;ancestors&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In this case a super call is much more expressive than the use of method aliasing. It does not pollute your objects instance methods list and it will never produce name clashes.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>undef_method != remove_method</title>
   <link href="http://www.nach-vorne.de/2008/2/28/undef_method-remove_method/index.html"/>
   <updated>2008-02-28T00:00:00+01:00</updated>
   <id>tag:www.nach-vorne.de,/2008/2/28/undef_method-remove_method/undef_method-remove_method</id>
   <content type="html">&lt;p&gt;When trying to remove behavior from your classes or objects, you will quickly stumble upon &lt;code&gt;undef_method&lt;/code&gt; and &lt;code&gt;remove_method&lt;/code&gt;. Even when your are just trying to change an implementation you will need one of these. Otherwise Ruby will warn you, that you are discarding an older code chunk.&lt;/p&gt;

&lt;p&gt;So let&amp;#8217;s quickly examine the basic difference between the two. The RDoc of &lt;a href='http://ruby-doc.org/core/classes/Module.html#M001675'&gt;&lt;code&gt;undef_method&lt;/code&gt;&lt;/a&gt; already gives good starting point.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Prevents the current class from responding to calls to the named method. Contrast this with remove_method, which deletes the method from the particular class; Ruby will still search superclasses and mixed-in modules for a possible receiver.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;There is also some sample code, but mine will be better. (No, it will not, but have you ever heard of NIH?) And I will use two test techniques promoted by Jay Fields to make sure, your mind is still able to move a bit. These are &lt;a href='http://blog.jayfields.com/2007/08/rubygems-dust.html'&gt;dust&lt;/a&gt; and &lt;a href='http://blog.jayfields.com/2008/02/ruby-creating-anonymous-classes.html'&gt;anonymous classes for tests&lt;/a&gt;. Not too much magic for the rest of us, but both make posting test code a lot easier, quicker and more readable.&lt;/p&gt;

&lt;p&gt;I will post it in chunks. At the end of the article you will have a complete ruby test.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;require &amp;quot;rubygems&amp;quot;
require &amp;quot;dust&amp;quot;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;No magic. Just wait a bit more&amp;#8230;&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;unit_tests do&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This is &lt;code&gt;dust&lt;/code&gt;&amp;#8217;s way to say &lt;code&gt;class FooTest &amp;lt; Test::Unit::TestCase&lt;/code&gt;. I really like &lt;code&gt;dust&lt;/code&gt;.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;  def setup
    @base_class = Class.new do
      def method
        &amp;quot;answered in base class&amp;quot;
      end
    end
    @sub_class = Class.new(@base_class) do
      def method
        &amp;quot;answered in sub class&amp;quot;
      end
    end
    @base_instance = @base_class.new
    @sub_instance = @sub_class.new
  end&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The setup method is pretty basic. We are creating a fresh inheritance hierarchy for every test method. This will simply prevent undesired side effects. Changes to classes are so global. Okay, the &lt;code&gt;@base_class&lt;/code&gt; &amp;#8211; actually it has no name, but anyways &amp;#8211; has a single method &lt;code&gt;method&lt;/code&gt;. &lt;code&gt;@sub_class&lt;/code&gt; inherits from &lt;code&gt;@base_class&lt;/code&gt; &amp;#8211; now I get the naming schema &amp;#8211; and defines its own version of the &lt;code&gt;method&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;For each run, I also instantiate an instance for each class to play with.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;  test &amp;quot;Basic setup works&amp;quot; do
    assert_equal @base_instance.method, &amp;quot;answered in base class&amp;quot;
    assert_equal @sub_instance.method, &amp;quot;answered in sub class&amp;quot;
  end&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This is &lt;code&gt;dust&lt;/code&gt;&amp;#8217;s way to say &lt;code&gt;def test_basic_setup_works; ...; end&lt;/code&gt;. How do you fell?&lt;/p&gt;

&lt;p&gt;The code itself is really basic. I&amp;#8217;m just making sure, that the setup works and nobody changed my tests significantly.&lt;/p&gt;

&lt;h2 id='id2'&gt;&lt;code&gt;remove_method&lt;/code&gt;&lt;/h2&gt;

&lt;p&gt;Let&amp;#8217;s start with the well-behaving twin: &lt;code&gt;remove_method&lt;/code&gt;.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;  test &amp;quot;remove_method let&amp;#39;s you undo method definitions&amp;quot; do
    @sub_class.send(:remove_method, :method)

    assert_equal @base_instance.method, &amp;quot;answered in base class&amp;quot;
    assert_equal @sub_instance.method, &amp;quot;answered in base class&amp;quot;
  end&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I&amp;#8217;m using &lt;code&gt;send&lt;/code&gt; here to circumvent the visibility restrictions. Don&amp;#8217;t do this at home. Use &lt;code&gt;class_eval&lt;/code&gt; instead. Your children (using 1.9) will be happier.&lt;/p&gt;

&lt;p&gt;After removing a method, its definition is removed from the class itself. When you try to access it, the correct implementation will be searched within the ancestors. This way you can get rid of your customizations and activate a more general definition. This is mainly the thing you would want to use, before redefining a method, to get rid of the warning.&lt;/p&gt;

&lt;p&gt;There is not much else to say about it. Sorry.&lt;/p&gt;

&lt;h2 id='id3'&gt;&lt;code&gt;undef_method&lt;/code&gt;&lt;/h2&gt;

&lt;p&gt;The less it sounds like plain english, the hackier it is (ref. awk vs. grep). &lt;code&gt;undef_method&lt;/code&gt; does not only remove the definition from the current class, but causes Exceptions. Let&amp;#8217;s have a look at the tests first.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;  test &amp;quot;undef_method causes NoMethodErrors&amp;quot; do
    @sub_class.send(:undef_method, :method)

    assert_raise(NoMethodError) { @sub_instance.method }
  end&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Although, there is a definition in the base class, it is not used. Instead, we get an error. Okay, but this might make you think, that now, the method &lt;code&gt;method&lt;/code&gt; is totally gone &amp;#8230;&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;  test &amp;quot;undef_method does not affect superclasses&amp;quot; do 
    @sub_class.send(:undef_method, :method)
    
    assert_equal @base_instance.method, &amp;quot;answered in base class&amp;quot;
  end&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&amp;#8230; no it is not. It is still there &amp;#8211; in the base class. &lt;code&gt;undef_method&lt;/code&gt; actually hides all implementations in superclasses. This somehow breaks the inheritance relation. Although sub inherits from base and although base has an implementation and sub does not, the happy programmer caused a NoMethodError. The following test shall stress this reasoning.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;  test &amp;quot;undef_method breaks inheritance&amp;quot; do
    @sub_class.send(:undef_method, :method)
    
    assert @base_class.instance_methods.include?(&amp;quot;method&amp;quot;)
    assert @sub_instance.kind_of?(@base_class)
    
    # This is the point where you should say: No!
    assert !@sub_instance.respond_to?(:method)
  end&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We are still missing an&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;end&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;here. The test is done, it will work on all 1.8 compatible machines.&lt;/p&gt;

&lt;h2 id='strange_summary'&gt;Strange summary&lt;/h2&gt;

&lt;p&gt;More or less &amp;#8211; without the hooks to update the functionality of &lt;code&gt;respond_to&lt;/code&gt;, &lt;code&gt;instance_methods&lt;/code&gt; and their friends &amp;#8211; &lt;code&gt;remove_method&lt;/code&gt; and &lt;code&gt;undef_method&lt;/code&gt; could be implemented like the following&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class Module
  def remove_method(name)
    define_method(name) do
      super
    end
  end
  
  def undef_method(name)
    define_method(name) do
      raise NoMethodError, 
            &amp;quot;undefined method `#{name}&amp;#39; for #{self}&amp;quot;, 
            caller(1)
    end
  end
end&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The one passes control flow to an implementation somewhere else, the other raises Exceptions. Try it, think about it, and tell me, why one needs &lt;code&gt;undef_method&lt;/code&gt;.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Syntax Highlighting enabled</title>
   <link href="http://www.nach-vorne.de/2008/2/28/syntax-highlighting-enabled/index.html"/>
   <updated>2008-02-28T00:00:00+01:00</updated>
   <id>tag:www.nach-vorne.de,/2008/2/28/syntax-highlighting-enabled/syntax-highlighting-enabled</id>
   <content type="html">&lt;p&gt;Were you as annoyed as I was about this site having no syntax highlighting but loads of code examples. The times they are a-changing. Thanks to &lt;a href='http://svn.danwebb.net/external/CodeHighlighter/'&gt;Dan Webb&lt;/a&gt; for the JavaScript and &lt;a href='http://henrik.nyh.se/tumble/'&gt;Henrik&lt;/a&gt; for the CSS.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Quick Rake Goodies</title>
   <link href="http://www.nach-vorne.de/2008/2/27/rake-quickies/index.html"/>
   <updated>2008-02-27T00:00:00+01:00</updated>
   <id>tag:www.nach-vorne.de,/2008/2/27/rake-quickies/rake-quickies</id>
   <content type="html">&lt;p&gt;Since we are all social guys, telling everybody what we are doing right now, as often and as detailed as possible, it is just right, to do this automatically. So for everybody living on the command line and in Ruby the following quick snippets might be useful.&lt;/p&gt;

&lt;p&gt;As you might know, I&amp;#8217;m currently writing my master&amp;#8217;s thesis &amp;#8211; in LaTeX. And there is a quick command to calculate the current word count.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;detex mainfile.tex | wc -w&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;detex&lt;/code&gt; strips all the LaTeX commands out of your document, but it follows includes, so all you got in the end is a plain text version on STDOUT. &lt;code&gt;wc&lt;/code&gt; is one of this great old unix tools, it simply counts lines, words and characters. The &lt;code&gt;-w&lt;/code&gt; options limits the output to a single number &amp;#8211; the number of words.&lt;/p&gt;

&lt;p&gt;This will be the first building block.&lt;/p&gt;

&lt;h3 id='skype'&gt;Skype&lt;/h3&gt;

&lt;p&gt;The first target of social striptease will be Skype. At least in my contact list, it is usual to let everybody know what you are listening to, where you are and what you are doing, with the help of mood messages. Sometimes they are even used for chatting. To set a mood message from the command line is pretty easy. I will be using the &lt;code&gt;rb-appscript&lt;/code&gt; gem which allows to execute AppleScript from Ruby. Consequently this works only on a Mac.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;require &amp;#39;rubygems&amp;#39;
require &amp;#39;appscript&amp;#39;
include Appscript

app(&amp;quot;Skype&amp;quot;).send_ :script_name =&amp;gt; &amp;quot;Raketask&amp;quot;,
                   :command =&amp;gt; &amp;quot;SET PROFILE MOOD_TEXT I&amp;#39;m in Ruby mood&amp;quot;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The necessary information for this snippet were taken from the &lt;a href='http://rb-skypemac.rubyforge.org/'&gt;&lt;code&gt;rb-skypemac&lt;/code&gt; gem&lt;/a&gt; (the way to talk to Skype via &lt;code&gt;rb-appscript&lt;/code&gt;) and from this &lt;a href='http://blog.grundprinzip.de/articles/2008/02/25/mac-skype-status-von-der-kommandozeile-setzen/'&gt;blog post (german)&lt;/a&gt; of a friend of mine (the way to set a mood message via AppleScript).&lt;/p&gt;

&lt;h3 id='twitter'&gt;Twitter&lt;/h3&gt;

&lt;p&gt;Wouldn&amp;#8217;t it be great if all my friends get an SMS, when I wrote five words, yes it would. So let&amp;#8217;s build a simple Twitter integration. There is the great &lt;code&gt;twitter&lt;/code&gt; gem which let&amp;#8217;s you forget all the API nastiness and gives you really simple access from Ruby and your plain old command line. If you set up your Twitter credentials in &lt;code&gt;~/.twitter&lt;/code&gt; the command line tool automatically picks them to authenticate. I will use them to minimize the setup costs. Unfortunately, the Ruby API has no official way to access the config file in your home directory. But this is not a reason to stop. I&amp;#8217;m using the send method here to circumvent visibility restrictions.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;require &amp;quot;rubygems&amp;quot;
require &amp;quot;twitter&amp;quot;
require &amp;quot;twitter/command&amp;quot;

config = Twitter::Command.send(:create_or_find_config)

Twitter::Base.new(config[&amp;#39;email&amp;#39;], config[&amp;#39;password&amp;#39;]).post(&amp;quot;Ruby? Anybody?&amp;quot;)&lt;/code&gt;&lt;/pre&gt;

&lt;h3 id='all_together_now'&gt;All together now&lt;/h3&gt;

&lt;p&gt;I have a Rakefile to compile my thesis using latex. In there are options to do a quick compile for preview and a longer compile including images for a final version. I have a task to open the generated PDF in Preview (using AppleScript again) or to fetch my bibtex file from &lt;a href='http://www.bibsonomy.org'&gt;bibsonomy.org&lt;/a&gt;. So why not add tasks to count words and to post the current status to Skype and Twitter. Now we have everything in place. So without and further ado, here is a snippet from my Rakefile:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;desc &amp;quot;Counts words of main document&amp;quot;
task :count do
  puts &amp;quot;#{`detex #{PROJECT_NAME} | wc -w`.strip} words in thesis&amp;quot;
  if (file = ENV[&amp;quot;file&amp;quot;])
    puts &amp;quot;#{`detex #{file} | wc -w`.strip} words in #{file}&amp;quot;
  end
end&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This is the main &lt;code&gt;count&lt;/code&gt; task. It simply prints the current word count to the command line. Additionally it is possible to count the words in a single file by passing in a file parameter (&lt;code&gt;rake count file=chapter_1.tex&lt;/code&gt;).&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;namespace :count do
  def count
    count = `detex #{PROJECT_NAME} | wc -w`.strip
    &amp;quot;Current word count in master&amp;#39;s thesis: #{count}&amp;quot;
  end

  desc &amp;quot;Post word count to Twitter&amp;quot;
  task :twitter do
    require &amp;quot;rubygems&amp;quot;
    require &amp;quot;twitter&amp;quot;
    require &amp;quot;twitter/command&amp;quot;

    config = Twitter::Command.send(:create_or_find_config)

    Twitter::Base.new(config[&amp;#39;email&amp;#39;], config[&amp;#39;password&amp;#39;]).post(count)
  end

  desc &amp;quot;Post word count to Skype&amp;quot;
  task :skype do
    require &amp;#39;rubygems&amp;#39;
    require &amp;#39;appscript&amp;#39;
    include Appscript

    app(&amp;quot;Skype&amp;quot;).send_ :script_name =&amp;gt; &amp;quot;Raketask&amp;quot;,
                       :command =&amp;gt; &amp;quot;SET PROFILE MOOD_TEXT #{count}&amp;quot;
  end              
end&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This code may look familiar. It is the Skype and Twitter integration. The task use a common method &lt;code&gt;count&lt;/code&gt; to get the numbers from the command line.&lt;/p&gt;

&lt;p&gt;This were the not so quick Rake goodies. Thanks for your attention.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>structured_warnings Highlights</title>
   <link href="http://www.nach-vorne.de/2008/2/22/structured_warnings-highlights/index.html"/>
   <updated>2008-02-22T00:00:00+01:00</updated>
   <id>tag:www.nach-vorne.de,/2008/2/22/structured_warnings-highlights/structured_warnings-highlights</id>
   <content type="html">&lt;p&gt;After my short Announcement yesterday, I&amp;#8217;d like to go on an name some of the highlights.&lt;/p&gt;

&lt;p&gt;Besides the basic functionality proposed in &lt;a href='http://www.oreillynet.com/ruby/blog/2008/02/structured_warnings_now.html'&gt;the original article&lt;/a&gt;, it has some more neat features, that make the life of its users enjoyable and fun.&lt;/p&gt;

&lt;p&gt;So you better start using structured_warnings now.&lt;/p&gt;

&lt;h3 id='dynamic_scoping'&gt;Dynamic scoping&lt;/h3&gt;

&lt;p&gt;Using the block syntax to disable warnings provides a dynamic scope of deactivation. Not only everything within the block will raise no warning, but every piece of code, that is accessed from there on. This can become very powerful for certain uses.&lt;/p&gt;

&lt;h3 id='last_one_wins'&gt;Last one wins&lt;/h3&gt;

&lt;p&gt;You never know exactly, so deactivating a warning for a dynamic scope may be overridden by enabling it again in a deeper scope and the other way round.&lt;/p&gt;

&lt;h3 id='warner_architecture'&gt;Warner architecture&lt;/h3&gt;

&lt;p&gt;Each warning is passed to a warner instance, which it there to format the information, which is put to stdout. This warner may be changed for &amp;#8211;again &amp;#8211; a dynamic scope. This way you may instruct your favourite web framework to use a specific warner, that fetches all the information and writes it to a database instead of stdout.&lt;/p&gt;

&lt;p&gt;Perhaps you want to have warnings in your merchant library to be to you via twitter message. Who knows? It will be pretty easy. Just have a look at the warner that is used for the test assertions. It will give you a good start.&lt;/p&gt;

&lt;h3 id='inheritance_done_right'&gt;Inheritance done right&lt;/h3&gt;

&lt;p&gt;Instead of redefining the &lt;code&gt;Kernel#warn&lt;/code&gt; method directly, structured_warnings simply defines its own &lt;code&gt;warn&lt;/code&gt;. The module is mixed into Object, so that every warn will go to this implementation first. After collecting all the necessary information, checking if the current warning is not disabled and formatting the output, the resulting message is passed on to the base implementation.&lt;/p&gt;

&lt;p&gt;This way it is possible to add other extensions to you warning mechanism, you may freely redirect stderr to whatever you want. structured_warnings &lt;code&gt;warn&lt;/code&gt; will just do, what it is supposed to.&lt;/p&gt;

&lt;h3 id='fully_documented_sources'&gt;Fully documented sources&lt;/h3&gt;

&lt;p&gt;Today I finished the documentation part. Now each and every method, public or not is documented. Just have a look at the &lt;a href='http://rug-b.rubyforge.org/structured_warnings/rdoc'&gt;RDoc generated API site&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id='performance'&gt;Performance&lt;/h3&gt;

&lt;p&gt;Come on. The main goal is to avoid the use of code, that raises warnings. With the help of this library it is as easy as it gets to do that. A warning should always be an exception (not in the sense of the Ruby class, but in the sense of &amp;#8220;not common&amp;#8221;), so who will be concerned about performance.&lt;/p&gt;

&lt;p&gt;The only thing I can tell you is, that structured_warnings will slow down calls to &lt;code&gt;warn&lt;/code&gt; but it will not slow down your whole code and that is what counts.&lt;/p&gt;</content>
 </entry>
 
</feed>

