<?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>acts_as_developer: Category Ruby</title>
    <link>http://www.robertshowalter.com/articles/category/ruby</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description></description>
    <item>
      <title>Loading Multiple Ruby Files and Finding Descendants of a Class</title>
      <description>&lt;p&gt;I was playing around with writing a little Ruby program to run simulations of the &lt;a href="http://en.wikipedia.org/wiki/Prisoner's_dilemma"&gt;Prisoner&amp;#8217;s Dilemma&lt;/a&gt;. I wanted to be able to program various strategies by creating multiple classes that descended from  a common &lt;code&gt;Player&lt;/code&gt; class:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="keyword"&gt;class &lt;/span&gt;&lt;span class="class"&gt;FirstPlayer&lt;/span&gt; &lt;span class="punct"&gt;&amp;lt;&lt;/span&gt; &lt;span class="constant"&gt;Player&lt;/span&gt;
 &lt;span class="punct"&gt;...&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;I put my player classes in little ruby files and wrote a &amp;#8220;supervisor&amp;#8221; program to load all my classes and then play them against each other, round-robin fashion. I run the supervisor like this:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;ruby supervisor.rb player1.rb player2.rb&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

In order to create my supervisor, I had to do two things:
	&lt;ol&gt;
	&lt;li&gt;Load the player files specified on the command line, and&lt;/li&gt;
		&lt;li&gt;Find all the player classes&lt;/li&gt;
	&lt;/ol&gt;


	&lt;p&gt;I noticed that my command line looks a lot like what happens when you run e.g. &lt;code&gt;rake test:units&lt;/code&gt; in a Rails project. This rake task loads all your unit tests and then finds all the test cases (i.e. the classes that are descendants of &lt;code&gt;Test::Unit::TestCase&lt;/code&gt;).&lt;/p&gt;


	&lt;p&gt;The job of loading all the test files is handled by &lt;code&gt;rake_test_loader.rb&lt;/code&gt;, which is part of the &lt;code&gt;rake&lt;/code&gt; gem. The relevant code is trivial:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="constant"&gt;ARGV&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;each&lt;/span&gt; &lt;span class="punct"&gt;{&lt;/span&gt; &lt;span class="punct"&gt;|&lt;/span&gt;&lt;span class="ident"&gt;f&lt;/span&gt;&lt;span class="punct"&gt;|&lt;/span&gt; &lt;span class="ident"&gt;load&lt;/span&gt; &lt;span class="ident"&gt;f&lt;/span&gt; &lt;span class="keyword"&gt;unless&lt;/span&gt; &lt;span class="ident"&gt;f&lt;/span&gt; &lt;span class="punct"&gt;=~&lt;/span&gt; &lt;span class="punct"&gt;/&lt;/span&gt;&lt;span class="regex"&gt;^-&lt;/span&gt;&lt;span class="punct"&gt;/&lt;/span&gt;  &lt;span class="punct"&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;code&gt;ARGV&lt;/code&gt; contains all the command line arguments (after &lt;code&gt;supervisor.rb&lt;/code&gt;). The &lt;code&gt;unless&lt;/code&gt; part rejects arguments that start with a dash, since those would presumably be option flags.

	&lt;p&gt;So I can use that code as-is to load my player classes.&lt;/p&gt;


	&lt;p&gt;In order to find my player classes, I needed to borrow a technique from &lt;code&gt;Test::Unit&lt;/code&gt; to find all classes that are descendants from my base &lt;code&gt;Player&lt;/code&gt; class.&lt;/p&gt;


	&lt;p&gt;The relevant code is in &lt;code&gt;test/unit/autorunner.rb&lt;/code&gt; in your standard Ruby library directory. The &lt;code&gt;Test::Unit&lt;/code&gt; is more complex than what I needed, but I was able to distill it down to this:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="ident"&gt;players&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="punct"&gt;[]&lt;/span&gt;
&lt;span class="constant"&gt;ObjectSpace&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;each_object&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="constant"&gt;Class&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt; &lt;span class="keyword"&gt;do&lt;/span&gt; &lt;span class="punct"&gt;|&lt;/span&gt;&lt;span class="ident"&gt;klass&lt;/span&gt;&lt;span class="punct"&gt;|&lt;/span&gt;
  &lt;span class="ident"&gt;players&lt;/span&gt; &lt;span class="punct"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="ident"&gt;klass&lt;/span&gt; &lt;span class="keyword"&gt;if&lt;/span&gt; &lt;span class="ident"&gt;klass&lt;/span&gt; &lt;span class="punct"&gt;&amp;lt;&lt;/span&gt; &lt;span class="constant"&gt;Player&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;code&gt;ObjectSpace&lt;/code&gt; is a handy Ruby gizmo that, among other things, lets you iterate over all the objects in your current process. By specifying &lt;code&gt;Class&lt;/code&gt; in the call to &lt;code&gt;each_object&lt;/code&gt;, we iterate over all classes. To find those that are descendants of my &lt;code&gt;Player&lt;/code&gt; class, we use a handy &lt;code&gt;&amp;lt;&lt;/code&gt; operator defined in &lt;/code&gt;Module&lt;/code&gt;. This operator returns true if the left-hand argument is a descendant of the right-hand argument.

	&lt;h3&gt;Can It Be Done in One Line?&lt;/h3&gt;


	&lt;p&gt;You may be wondering why I&amp;#8217;m using an array here and then appending to the array as I go. Isn&amp;#8217;t there some way to use &lt;code&gt;collect()&lt;/code&gt;/&lt;code&gt;select()&lt;/code&gt; to extract this data in one call?&lt;/p&gt;


	&lt;p&gt;The problem is that methods like &lt;code&gt;select()&lt;/code&gt; are in module &lt;code&gt;Enumerable&lt;/code&gt;, but &lt;code&gt;ObjectSpace&lt;/code&gt; is not. However, Ruby provides a handy class called &lt;code&gt;Enumerable::Enumerator&lt;/code&gt; that can turn any object with a method that yields to a block into an &lt;code&gt;Enumerable&lt;/code&gt; object.&lt;/p&gt;


	&lt;p&gt;To use this for our class collector, you would write:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="ident"&gt;require&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;enumerator&lt;/span&gt;&lt;span class="punct"&gt;'&lt;/span&gt;
&lt;span class="ident"&gt;players&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="constant"&gt;Enumerator&lt;/span&gt;&lt;span class="punct"&gt;::&lt;/span&gt;&lt;span class="constant"&gt;Enumerable&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;new&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="constant"&gt;ObjectSpace&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="symbol"&gt;:each_object&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="constant"&gt;Class&lt;/span&gt;&lt;span class="punct"&gt;).&lt;/span&gt;&lt;span class="ident"&gt;select&lt;/span&gt; &lt;span class="punct"&gt;{|&lt;/span&gt;&lt;span class="ident"&gt;klass&lt;/span&gt;&lt;span class="punct"&gt;|&lt;/span&gt; &lt;span class="ident"&gt;klass&lt;/span&gt; &lt;span class="punct"&gt;&amp;lt;&lt;/span&gt; &lt;span class="constant"&gt;Player&lt;/span&gt;&lt;span class="punct"&gt;})&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;I&amp;#8217;ll let you decide which form is more readable.&lt;/p&gt;</description>
      <pubDate>Wed, 26 Sep 2007 11:35:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:60850adf-cca5-4e79-94c8-b40fa13a5fbf</guid>
      <author>Bob Showalter</author>
      <link>http://www.robertshowalter.com/articles/2007/09/26/loading-multiple-ruby-files-and-finding-descendants-of-a-class</link>
      <category>Ruby</category>
    </item>
    <item>
      <title>Minimize or disable WEBrick logging</title>
      <description>&lt;p&gt;I wrote a simple application today that used WEBrick servlets to serve up some content, and I wanted to minimize the logging that WEBrick puts out.&lt;/p&gt;


	&lt;p&gt;There are two kings of logging used by WEBrick:&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;Server logging, which is controlled by the &lt;code&gt;:Server&lt;/code&gt; parameter passed to &lt;code&gt;WEBrick::HTTPServer.new&lt;/code&gt;. This uses syslog-style log levels.&lt;/li&gt;
		&lt;li&gt;Access logging, which is controlled by the &lt;code&gt;:AccessLog&lt;/code&gt; parameter. This logs each request, and is similar to the Apache access log.&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;The default server log level is &lt;code&gt;INFO&lt;/code&gt;, but I wanted to change it to &lt;code&gt;WARN&lt;/code&gt;. I also wanted to disable Access logging altogether.&lt;/p&gt;


Here&amp;#8217;s what I ended up using:
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="ident"&gt;include&lt;/span&gt; &lt;span class="constant"&gt;WEBrick&lt;/span&gt;
&lt;span class="ident"&gt;server&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="constant"&gt;HTTPServer&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;new&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;
  &lt;span class="symbol"&gt;:Port&lt;/span&gt; &lt;span class="punct"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="number"&gt;8000&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt;
  &lt;span class="symbol"&gt;:Logger&lt;/span&gt; &lt;span class="punct"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="constant"&gt;Log&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;new&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="constant"&gt;nil&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="constant"&gt;BasicLog&lt;/span&gt;&lt;span class="punct"&gt;::&lt;/span&gt;&lt;span class="constant"&gt;WARN&lt;/span&gt;&lt;span class="punct"&gt;),&lt;/span&gt;
  &lt;span class="symbol"&gt;:AccessLog&lt;/span&gt; &lt;span class="punct"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="punct"&gt;[]&lt;/span&gt;
&lt;span class="punct"&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
Now the server is silent unless an unexpected problem occurs.</description>
      <pubDate>Sun, 24 Jun 2007 22:22:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:624b5593-7706-4583-9ec6-308cc27e3232</guid>
      <author>Bob Showalter</author>
      <link>http://www.robertshowalter.com/articles/2007/06/24/minimize-or-disable-webrick-logging</link>
      <category>Ruby</category>
    </item>
  </channel>
</rss>
