<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Object Mentor Blog: Bauble, Bauble...</title>
    <link>http://blog.objectmentor.com/articles/2008/07/20/bauble-bauble</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description></description>
    <item>
      <title>Bauble, Bauble...</title>
      <description>&lt;p&gt;In Ruby, I hate require statements that look like this:&lt;/p&gt;


&lt;code&gt;
require File.dirname(__FILE__)+"myComponent/component.rb" 
&lt;/code&gt;

	&lt;p&gt;So I decided to do something about it.&lt;/p&gt;


	&lt;p&gt;This all started when my Son, Micah, told me about his &lt;a href="http://limelight.8thlight.com/"&gt;Limelight&lt;/a&gt; project.  Limelight is a jruby/swing &lt;span class="caps"&gt;GUI&lt;/span&gt; framework.  If you want to build a fancy &lt;span class="caps"&gt;GUI&lt;/span&gt; in Ruby, consider this tool.&lt;/p&gt;


	&lt;p&gt;I have neither the time nor inclination to write a framework like this; but my curiosity was piqued.  So in order to see what it was like to do Swing in JRuby I spent a few hours cobbling together an implementation of &lt;a href="http://en.wikipedia.org/wiki/Langton's_ant"&gt;Langton&amp;#8217;s Ant&lt;/a&gt;.   This turned out to be quite simple.&lt;/p&gt;


	&lt;p&gt;The result, however, was a mess.  There was swing code mixed up with &amp;#8220;ant&amp;#8221; code, in the classic &lt;span class="caps"&gt;GUI&lt;/span&gt;/Business-rule goulash that we &amp;#8220;clean-coders&amp;#8221; hate so much.  Despite the fact that this was throw-away code, I could not leave it in that state &amp;#8211; the moral outrage was just too great.  So I spent some more time separating the program into two modules.&lt;/p&gt;


	&lt;p&gt;The first module knew all about Langton&amp;#8217;s ant, but nothing about Swing.  The second module was a tiny framework for implementing cellular automata in Swing.  (Here are all the &lt;a href="http://www.fitnesse.org/files/unclebob/BaubleBlog.tz"&gt;files&lt;/a&gt;).&lt;/p&gt;


	&lt;p&gt;I was quite happy with the separation, but did not like the horrible &lt;code&gt;require&lt;/code&gt; statements that I had to use.  The cellular_automaton component had two classes, in two separate files.  In order to get the &lt;code&gt;require&lt;/code&gt; right, I had to either use absolute directory paths, or the horrible &lt;code&gt;File.dirname(__FILE__)...&lt;/code&gt; structure.&lt;/p&gt;


	&lt;p&gt;What I wanted was for cellular_automaton to behave like a &lt;a href="http://www.rubygems.org"&gt;gem&lt;/a&gt;.  But I didn&amp;#8217;t want to make it into a gem.  Gem&amp;#8217;s are kind of &amp;#8220;heavy&amp;#8221; for a dumb little thing like &amp;#8220;cellular_automaton&amp;#8221;.&lt;/p&gt;


	&lt;p&gt;So I created a module named &amp;#8220;Bauble&amp;#8221; which gave me some gem-like behaviors.  Here it is:&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
module Bauble
  def self.use(bauble)
    bauble_name = File.basename(bauble)
    ensure_in_path "#{bauble}/lib" 
    require bauble_name
  end

  def self.ensure_in_path(path)
    $LOAD_PATH &amp;lt;&amp;lt; path unless $LOAD_PATH.include? path
  end
end
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;This is no great shakes, but it solved my problem.  Now, in my langton&amp;#8217;s ant program all I need to do is this:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
require 'bauble'
Bauble.use('../cellular_automaton')
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;All the ugly requires are gone.&lt;/p&gt;


	&lt;p&gt;I&amp;#8217;m thinking about turning Bauble into a rubyforge project, and making a publicly available gem out of it in order to give folks a standard way to avoid those horrible &lt;code&gt;__FILE__&lt;/code&gt; requires.  I think there are several other utilities that could be placed in Bauble such as &lt;code&gt;require_relative&lt;/code&gt; etc.&lt;/p&gt;


	&lt;p&gt;Anyway, what do you think?&lt;/p&gt;</description>
      <pubDate>Sun, 20 Jul 2008 15:42:29 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:23960702-3bfc-4787-92b6-4dd489cd5f24</guid>
      <author>Uncle Bob</author>
      <link>http://blog.objectmentor.com/articles/2008/07/20/bauble-bauble</link>
      <category>Uncle Bob's Blatherings</category>
      <category>Dynamic Languages</category>
      <category>Clean Code</category>
    </item>
    <item>
      <title>"Bauble, Bauble..." by Unclebob</title>
      <description>&lt;p&gt;The problem I am trying to solve is when I have many different components that I want to import into my app and I don&amp;#8217;t want to turn them all into gems.&lt;/p&gt;</description>
      <pubDate>Mon, 21 Jul 2008 21:31:39 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:f14f4c83-8e25-4246-97a7-02d112095397</guid>
      <link>http://blog.objectmentor.com/articles/2008/07/20/bauble-bauble#comment-1905</link>
    </item>
    <item>
      <title>"Bauble, Bauble..." by Micah Martin</title>
      <description>&lt;p&gt;Using &lt;code&gt;Bauble.use&lt;/code&gt; instead of &lt;code&gt;require&lt;/code&gt; would be a bit unorthodox.  But you could get used to it.&lt;/p&gt;


	&lt;p&gt;I imagine you could monkey patch &lt;code&gt;Kernel.require&lt;/code&gt; to do exactly what &lt;code&gt;Bauble&lt;/code&gt; is doing.  That&amp;#8217;s a bit scary though.&lt;/p&gt;


	&lt;p&gt;I agree that require statements can get nutty.  I blogged about &lt;a href="http://blog.8thlight.com/articles/2007/10/08/micahs-general-guidelines-on-ruby-require" rel="nofollow"&gt;Ruby require guidlines&lt;/a&gt; a while back.  In a nut shell, I recommend using &lt;code&gt;File.dirname(__FILE__)&lt;/code&gt; one time in your code base to set up a search path, and never use it again.&lt;/p&gt;</description>
      <pubDate>Mon, 21 Jul 2008 15:02:22 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:68cb75b1-1f37-498e-8b28-89b12293f77a</guid>
      <link>http://blog.objectmentor.com/articles/2008/07/20/bauble-bauble#comment-1903</link>
    </item>
    <item>
      <title>"Bauble, Bauble..." by Jim Weirich</title>
      <description>&lt;p&gt;I&amp;#8217;ve never been a fan of the require/&lt;i&gt;FILE&lt;/i&gt; idiom either.  However, I also don&amp;#8217;t like using paths like &amp;#8221;../cellular_automaton&amp;#8221; either &amp;#8230; mainly because (if I&amp;#8217;ve read you code right), the location of the cellular_automaton directory is calculated based on your &lt;em&gt;current&lt;/em&gt; directory.  That&amp;#8217;s fine for running in the project&amp;#8217;s directory, but what if I want to run it from my home directory.  IMHO, refs to &amp;#8221;../dir&amp;#8221; do not belong in code.&lt;/p&gt;


	&lt;p&gt;Here&amp;#8217;s what I do.  I project is structured like this:&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;project_dir
	&lt;ul&gt;
	&lt;li&gt;lib
	&lt;ul&gt;
	&lt;li&gt;langton
	&lt;ul&gt;
	&lt;li&gt;cellular_automaton&lt;/li&gt;
		&lt;li&gt;ui&lt;/li&gt;
	&lt;/ul&gt;&lt;/li&gt;
	&lt;/ul&gt;&lt;/li&gt;
	&lt;/ul&gt;&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;All requires would be of the form: require &amp;#8216;langton/cellular_automation/whatever&amp;#8217;.  This only requires a -Ilib option on any manually typed ruby command (ie. all refs are relative to &amp;#8216;lib&amp;#8217;).  The standard rake test task understands this and will add a -Ilib option automatially).  This removes the need for any &lt;i&gt;FILE&lt;/i&gt; relative requires.&lt;/p&gt;


	&lt;p&gt;And the truly lazy can put &amp;#8216;lib&amp;#8217; into their RUBYLIB environment variable.&lt;/p&gt;


	&lt;p&gt;This scales nicely to installed libraries, which would either copy the &amp;#8216;lib/*&amp;#8217; to a directory listed in $LOAD_PATH, or as a GEM which would add the projects &amp;#8216;lib&amp;#8217; directory to $LOAD_PATH.&lt;/p&gt;</description>
      <pubDate>Mon, 21 Jul 2008 13:13:40 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:a3b59313-7b08-4876-8001-66ddf71a750b</guid>
      <link>http://blog.objectmentor.com/articles/2008/07/20/bauble-bauble#comment-1901</link>
    </item>
    <item>
      <title>"Bauble, Bauble..." by Bil Kleb</title>
      <description>If you used Hoe&amp;#8217;s &lt;code&gt;sow&lt;/code&gt; command, e.g.,
&lt;pre&gt;
  $ sow bauble
&lt;/pre&gt;
you may have a gem before you might expect.
&lt;pre&gt;
  $  sudo gem install hoe
&lt;/pre&gt;</description>
      <pubDate>Sun, 20 Jul 2008 18:13:14 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:8877ce74-3fc0-4bf7-b247-6e9528ceca39</guid>
      <link>http://blog.objectmentor.com/articles/2008/07/20/bauble-bauble#comment-1900</link>
    </item>
  </channel>
</rss>
