<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" version="2.0">

<channel>
	<title>Along Came Betty</title>
	
	<link>http://blog.darevay.com</link>
	<description>You know, software and some other stuff like maybe guitar or something</description>
	<lastBuildDate>Tue, 24 Nov 2009 03:21:41 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/darevay" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="darevay" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Can anything I write here help open source?</title>
		<link>http://blog.darevay.com/2009/11/can-anything-i-write-here-help-open-source/</link>
		<comments>http://blog.darevay.com/2009/11/can-anything-i-write-here-help-open-source/#comments</comments>
		<pubDate>Tue, 24 Nov 2009 03:21:41 +0000</pubDate>
		<dc:creator>dave</dc:creator>
				<category><![CDATA[open source]]></category>

		<guid isPermaLink="false">http://blog.darevay.com/?p=223</guid>
		<description><![CDATA[I spend a lot of my time working with open source software and I like to think that I&#8217;ve picked up a number of the tricks, gotchas, and other secrets of these systems.  So, whenever I think of posting to this blog, it usually along the lines of &#8220;man, I just spent a day figuring [...]]]></description>
			<content:encoded><![CDATA[<p>I spend a lot of my time working with open source software and I like to think that I&#8217;ve picked up a number of the tricks, gotchas, and other secrets of these systems.  So, whenever I think of posting to this blog, it usually along the lines of &#8220;man, I just spent a day figuring out X. I should document that for the next poor sap that comes along.&#8221; Invariably, I don&#8217;t. Occasionally this is due to time constraints, but more often it&#8217;s because there&#8217;s already a wiki or something for whatever I&#8217;m dealing with and I believe that keeping related information in one place is a good idea. So, I don&#8217;t write it here, or there.</p>
<p>Unfortunately, writing open source documentation isn&#8217;t as fun as coding or as gratifying as blog traffic* so there&#8217;s a pretty good chance the open source knowledge diaspora will continue. Important, headache preventing, technical information will remain in change logs, mailing lists, and random blog posts while user guides and other documentation will be ignored or buried**.</p>
<p><a href="http://stackoverflow.com">Stack Overflow</a> is helping a bit, but it would be nice if some of the best answers there were pulled back into (or at least linked to from) the how-tos and getting started guides of the projects they concern.</p>
<p>I, for my part, am going to start writing this stuff down. Where it belongs.</p>
<p>* yes, I recognize the irony here.<br />
** many open source projects could do a hell of a lot better job at making documentation easier to find.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.darevay.com/2009/11/can-anything-i-write-here-help-open-source/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Using rails.vim with JRuby</title>
		<link>http://blog.darevay.com/2009/07/using-rails-vim-with-jruby/</link>
		<comments>http://blog.darevay.com/2009/07/using-rails-vim-with-jruby/#comments</comments>
		<pubDate>Fri, 24 Jul 2009 02:45:15 +0000</pubDate>
		<dc:creator>dave</dc:creator>
				<category><![CDATA[jruby]]></category>

		<guid isPermaLink="false">http://blog.darevay.com/?p=220</guid>
		<description><![CDATA[I spent this evening fiddling around with rails.vim and JRuby.  I have some Java code that I&#8217;d like to use with Rails, so I&#8217;m using JRuby (also, JRuby seems to hate Windows slightly less than MRI).  Anyway, this is really about using vim to edit Rails code. I installed rails.vim and found that it was [...]]]></description>
			<content:encoded><![CDATA[<p>I spent this evening fiddling around with <a href="http://www.vim.org/scripts/script.php?script_id=1567">rails.vim</a> and <a href="http://jruby.org">JRuby</a>.  I have some <a href="http://jsoar.googlecode.com">Java code</a> that I&#8217;d like to use with Rails, so I&#8217;m using JRuby (also, JRuby seems to hate Windows slightly less than MRI).  Anyway, this is really about using vim to edit Rails code. I installed rails.vim and found that it was hard-coded to use plain old Ruby. Assuming that <strong>jruby.bat</strong> (or jruby, I guess) is on your system path, all you need to do is modify the <strong>app_ruby_shell_command </strong>in <strong>autoload/rails.vim</strong> like this:</p>
<pre>function! s:app_ruby_shell_command(cmd) dict abort
  if self.path() =~ '://'
    return "jruby.bat ".a:cmd
  else
    return "jruby.bat -C ".s:rquote(self.path())." -S ".a:cmd
  endif
endfunction</pre>
<p>Basically, just change &#8220;ruby&#8221; to &#8220;jruby.bat&#8221; and add the <strong>-S</strong> parameter.</p>
<p>The <strong>-S</strong> flag is important. Otherwise, it seems JRuby&#8217;s implementation of the <strong>-C</strong> flag is a <a href="http://jira.codehaus.org/browse/JRUBY-3621">little buggy</a>. For example, almost any <strong>script/</strong> command will fail:</p>
<pre>&gt;jruby -C c:\path\to\rails\app script/generate
C:/Program Files/jruby-1.3.1/bin/../lib/ruby/1.8/pathname.rb:711:in `relative_path_from':
        different prefix: "C:/" and "C:\\path/to/rails/app" (ArgumentError)
        from C:/Program Files/jruby-1.3.1/lib/ruby/gems/1.8/gems/rails-2.3.3/lib/rails_generator/lookup.rb:110:in
        `use_component_sources!'</pre>
<p>Everything seems to be working well now. Yay.</p>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px;">app_ruby_shell_command</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.darevay.com/2009/07/using-rails-vim-with-jruby/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>99 Prolog Problems in Ruby</title>
		<link>http://blog.darevay.com/2009/04/99-prolog-problems-in-ruby/</link>
		<comments>http://blog.darevay.com/2009/04/99-prolog-problems-in-ruby/#comments</comments>
		<pubDate>Mon, 13 Apr 2009 01:24:54 +0000</pubDate>
		<dc:creator>dave</dc:creator>
				<category><![CDATA[ruby]]></category>
		<category><![CDATA[99 problems]]></category>

		<guid isPermaLink="false">http://blog.darevay.com/?p=215</guid>
		<description><![CDATA[I spent the weekend on the shores of Lake Huron. During the kids&#8217; naps, I worked through the first 28 problems of P-99: Ninety-Nine Prolog Problems in Ruby. Many have done it before me (in many languages and variations). Many will do it after.
So far P27 gave me the most trouble because I refused to [...]]]></description>
			<content:encoded><![CDATA[<p>I spent the weekend on the shores of Lake Huron. During the kids&#8217; naps, I worked through the first 28 problems of <a href="https://prof.ti.bfh.ch/hew1/informatik3/prolog/p-99/">P-99: Ninety-Nine Prolog Problems</a> in Ruby. Many have done it before me (in many languages and variations). Many will do it after.</p>
<p>So far P27 gave me the most trouble because I refused to use my brain. Figured it out on a nice long walk with the family. P28 was a pain because I was figuring out how Ruby blocks/procs work. <a href="http://github.com/daveray/ruby99/tree/master">Here&#8217;s</a> the code.</p>
<p>p.s. an immutable list,<a href="http://www.scala-lang.org/docu/files/api/scala/List.html"> like in Scala</a> (or Lisp, etc, etc), with pattern matching of course, would be handy in Ruby. I wonder if one exists&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.darevay.com/2009/04/99-prolog-problems-in-ruby/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ten Little Soul Crushing Features of Java</title>
		<link>http://blog.darevay.com/2009/04/ten-little-soul-crushing-features-of-java/</link>
		<comments>http://blog.darevay.com/2009/04/ten-little-soul-crushing-features-of-java/#comments</comments>
		<pubDate>Thu, 09 Apr 2009 03:04:23 +0000</pubDate>
		<dc:creator>dave</dc:creator>
				<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://blog.darevay.com/?p=205</guid>
		<description><![CDATA[There are a lot of big things (lack of closures, type inference, etc, etc) to dislike about Java. This is a list, in no particular order of little things that make day to day Java development just that much more irritating. Most of these are just convenience methods whose omission is unforgivable. Libraries exist to [...]]]></description>
			<content:encoded><![CDATA[<p>There are a lot of big things (lack of closures, type inference, etc, etc) to dislike about Java. This is a list, in no particular order of little things that make day to day Java development just that much more irritating. Most of these are just convenience methods whose omission is unforgivable. Libraries exist to address all of them, but that&#8217;s beside the point.</p>
<p><strong>1) No string join method</strong></p>
<p>How many times have I written this?</p>
<p><strong>2) No way to set additional JVM parameters in manifest of executable jar</strong></p>
<p>It&#8217;s really convenient to just double-click a jar &#8230; until you need to set your heap size or something and you have to go crawling back to a shell script.</p>
<p><strong>3) java.net.URL constructor throws checked MalformedURLException rather than unchecked IllegalArgumentException</strong></p>
<p>Seriously, what is so special about this exception?</p>
<p><strong>4) java.io.File has no getExtension() method</strong></p>
<p>Same as join() above.</p>
<p><strong>5) No immutable collections.  Collections.unmodifiableList() and friends don&#8217;t count</strong></p>
<p><strong>6) java.io.File.delete() silently does nothing if you try to delete a non-empty directory</strong></p>
<p>It&#8217;s even more irritating to write deleteFolder() than join() and getExtension().</p>
<p><strong>7) java.util.Random has a setSeed() method, but no getSeed() method</strong></p>
<p><strong>8) java.util.logging is built-in and incredibly lame</strong></p>
<p>They sucked every ounce of the joy out of log4j.</p>
<p><strong>9) JTree selection behavior gives me a headache every time I have to deal with it</strong></p>
<p><strong>10) No method to just read an entire InputStream or Reader into a byte array or string</strong></p>
<p>Same as join() above.</p>
<p><strong>Bonus) No method to copy a file!</strong></p>
<p><strong><br />
</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.darevay.com/2009/04/ten-little-soul-crushing-features-of-java/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Importing Multiple WSDLs with Maven</title>
		<link>http://blog.darevay.com/2009/03/importing-multiple-wsdls-with-maven/</link>
		<comments>http://blog.darevay.com/2009/03/importing-multiple-wsdls-with-maven/#comments</comments>
		<pubDate>Thu, 12 Mar 2009 01:40:14 +0000</pubDate>
		<dc:creator>dave</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[jax-ws]]></category>
		<category><![CDATA[maven]]></category>
		<category><![CDATA[netbeans]]></category>
		<category><![CDATA[wsdl]]></category>

		<guid isPermaLink="false">http://blog.darevay.com/?p=198</guid>
		<description><![CDATA[The jax-ws-maven plugin for Maven includes  the handy wsimport goal. This goal will take a WSDL from a URL or file and generate Java bindings for the service described.  The generated code may not be beautiful, but it works. Anyway, today I spent more than an hour fiddling with wsimport. In my case I was [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="https://jax-ws-commons.dev.java.net/jaxws-maven-plugin/"><strong>jax-ws-maven</strong></a> plugin for <a href="http://maven.apache.org/">Maven </a>includes  the handy <strong>wsimport</strong> goal. This goal will take a WSDL from a URL or file and generate Java bindings for the service described.  The generated code may not be beautiful, but it works. Anyway, today I spent more than an hour fiddling with wsimport. In my case I was trying to import multiple WSDLs into my project with different target packages. I ran into several hurdles and figured that I&#8217;d document them here for future victims.</p>
<p>First, for better or worse, I&#8217;m working in <a href="http://www.netbeans.org/">NetBeans</a>.  The Maven support is passable, but out of the box, the error reporting leaves <strong>a lot</strong> to be desired. In particular, detailed error reporting must be enabled. Otherwise, when there&#8217;s an error in your pom.xml file, you&#8217;ll get a nice <strong>&lt;Badly formed Maven project&gt;</strong> error with no other explanation. To enable error reporting go to <strong>Tools-&gt;Options-&gt;Miscellaneous-&gt;Maven</strong>, and check &#8220;Produce Exception Error Messages&#8221;. That will make your life easier. Now, about wsimport&#8230;</p>
<p>The obvious way to import multiple WSDLs is to include multiple executions in the jaxws-maven-plugin section of pom.xml. In fact, this is even the right way to do it&#8230; but if you just take your single-WSDL example, stolen from <a href="http://java.sun.com/mailers/techtips/enterprise/2008/TechTips_Jan08.html">some tutorial </a>somewhere, and copy it, you&#8217;ll end up with a couple of problems. First, when multiple execution are present, they must each be given a unique id, using of all things, the &lt;id&gt; tag.  This wasn&#8217;t that tough to figure out once I turned on error reporting as described above.</p>
<p>The second issue was more problematic and, in my opinion, probably a bug in the plugin. Each execution includes a &#8220;staleFile&#8221; which is used to manage dependencies, i.e. correctly recompiling WSDL when it changes. However, it happens that when multiple executions are present they <strong>use the same staleFile</strong>.  This means that the import of the second WSDL always thinks it&#8217;s up to date and thus never runs.  After a bunch of googling, I managed to find a solution in this <a href="http://java.sun.com/mailers/techtips/enterprise/2008/TechTips_Jan08.html">bug report</a>. So, the solution is to manually set a staleFile for each WSDL. Here&#8217;s the resulting &lt;plugin&gt; block from pom.xml:</p>
<pre>&lt;plugin&gt;
   &lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;
   &lt;artifactId&gt;jaxws-maven-plugin&lt;/artifactId&gt;
   &lt;executions&gt;
      &lt;execution&gt;
         &lt;id&gt;FirstWsdl&lt;/id&gt;
         &lt;goals&gt;
            &lt;goal&gt;wsimport&lt;/goal&gt;
         &lt;/goals&gt;
         &lt;configuration&gt;
            &lt;wsdlLocation&gt;http://localhost:8080/FirstWsdl?wsdl&lt;/wsdlLocation&gt;
            &lt;wsdlFiles&gt;
            &lt;wsdlFile&gt;path/to/FirstWsdl.wsdl&lt;/wsdlFile&gt;
            &lt;/wsdlFiles&gt;
            &lt;packageName&gt;com.example.first&lt;/packageName&gt;
            &lt;!-- Without this, multiple WSDLs won't be processed :( --&gt;
            &lt;staleFile&gt;${project.build.directory}/jaxws/stale/wsdl.FirstWsdl.done&lt;/staleFile&gt;
         &lt;/configuration&gt;
      &lt;/execution&gt;
      &lt;execution&gt;
         &lt;id&gt;SecondWsdl&lt;/id&gt;
         &lt;goals&gt;
            &lt;goal&gt;wsimport&lt;/goal&gt;
         &lt;/goals&gt;
         &lt;configuration&gt;
            &lt;wsdlLocation&gt;http://localhost:8080/SecondWsdl?wsdl&lt;/wsdlLocation&gt;
            &lt;wsdlFiles&gt;
            &lt;wsdlFile&gt;path/to/SecondWsdl.wsdl&lt;/wsdlFile&gt;
            &lt;/wsdlFiles&gt;
            &lt;packageName&gt;com.example.second&lt;/packageName&gt;
            &lt;!-- Without this, multiple WSDLs won't be processed :( --&gt;
            &lt;staleFile&gt;${project.build.directory}/jaxws/stale/wsdl.SecondWsdl.done&lt;/staleFile&gt;
         &lt;/configuration&gt;
      &lt;/execution&gt;
   &lt;/executions&gt;
&lt;/plugin&gt;</pre>
<p>That&#8217;s it. Cheers.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.darevay.com/2009/03/importing-multiple-wsdls-with-maven/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>The Ruby Way: Chapter 1 … bipolar</title>
		<link>http://blog.darevay.com/2009/01/the-ruby-way-chapter-1-bipolar/</link>
		<comments>http://blog.darevay.com/2009/01/the-ruby-way-chapter-1-bipolar/#comments</comments>
		<pubDate>Wed, 28 Jan 2009 03:46:59 +0000</pubDate>
		<dc:creator>dave</dc:creator>
				<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://blog.darevay.com/?p=189</guid>
		<description><![CDATA[I just finished chapter 1 of &#8220;The Ruby Way&#8221; by Hal Fulton.  I feel a little torn because it is simultaneously one of the worst chapters I have read, and also one of my favorites.  I&#8217;ll start with the bad.
The editing is pretty bad. I came across a number of typos which make me wonder [...]]]></description>
			<content:encoded><![CDATA[<p>I just finished chapter 1 of <a href="http://www.amazon.com/Ruby-Way-Second-Addison-Wesley-Professional/dp/0672328844">&#8220;The Ruby Way&#8221;</a> by Hal Fulton.  I feel a little torn because it is simultaneously one of the worst chapters I have read, and also one of my favorites.  I&#8217;ll start with the bad.</p>
<p>The editing is pretty bad. I came across a number of typos which make me wonder if anyone actually read it before going to print. On top of that, this is some seriously sloppy technical writing (kind of like this blog). New or alternative terms are introduced without note, examples are only explained by cryptic, embedded comments, and seemingly complex concepts are given no examples at all.</p>
<p>The one statement that stuck out for me was on page 5 where the author describes the <em>is-a</em> relationship:</p>
<blockquote><p>The relationship between a class and its superclass is interesting and important; it is usually described as the <em>is-a</em> relationship, because a Square &#8220;is a&#8221; Rectangle, and a Rectangle &#8220;is a&#8221; Polygon, and so on.</p></blockquote>
<p>Ummm, please tell me that your first example of a class hierarchy isn&#8217;t one that if not just wrong is at least <a href="http://groups.google.com/groups/search?hl=en&amp;ie=UTF-8&amp;q=is+a+square+a+rectangle+oop&amp;btnG=Search&amp;sitesearch=groups.google.com">highly controversial</a>.  You couldn&#8217;t have started with <a href="http://www.google.com/search?hl=en&amp;q=person+employee+oop&amp;btnG=Search">Person and Employee</a>?</p>
<p>Hal Fulton calls this chapter &#8220;our review of object-oriented programming and our whirlwind tour of the Ruby language&#8221; so besides the typos and sloppiness, I&#8217;ll give him a break. That&#8217;s really a lot to jam into one chapter of a book whose intended audience is assumed to basically know all that stuff.</p>
<p>It&#8217;s like a <a href="http://en.wikipedia.org/wiki/Man_page">man page</a>. It all makes perfect sense if you already know it.</p>
<p>So as my hesitation grew, I reached section 1.5, &#8220;Training Your Intuition: Things to Remember&#8221;. This is where, in my opinion, the author turns everything around. This is one of my favorite sections of a technical book I&#8217;ve read in a long time. It&#8217;s basically a somewhat random laundry list of Ruby idioms, gotchas, and nuggets of general wisdom. It reminds me of the best stuff you find in a FAQ when learning a new language. It reminds me of the lists I write for colleagues when I have to bring them up to speed on a new technology. It reminds me of how I felt the first time I read <a href="http://www.amazon.com/Effective-Specific-Addison-Wesley-Professional-Computing/dp/0201924889">Effective C++</a> when I was too young and foolish to recognize that for the <a href="http://blog.oup.com/2009/01/oxymoron/">oxymoron</a> it was.  It&#8217;s all the stuff that you wish that a language manual would come out and tell you right away, but is completely missing or at least hidden between simplistic examples.</p>
<p>Great stuff. Anyway, at least he ended on a high note. Flipping through the rest of the recipe-style chapters of the book, I&#8217;m pretty confident I&#8217;ll find it invaluable specifically because it continues that laundry list style that for some reason appeals to me so much.</p>
<p>p.s. my <a href="http://blog.darevay.com/category/scala/">&#8220;learning scala&#8221;</a> project was a simple <a href="http://rooscaloo.googlecode.com">rules engine</a>. For Ruby, I&#8217;m planning on a <a href="http://www.tcl.tk/man/tcl8.4/TclCmd/Tcl.htm">tcl interpreter</a>. Hopefully, I can do it in less than <a href="http://antirez.com/page/picol.html">550 lines</a>. Combining the speed of Ruby with the speed and expressiveness of Tcl &#8230; good times.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.darevay.com/2009/01/the-ruby-way-chapter-1-bipolar/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Remedial Scala: Interpreting Scala from Scala</title>
		<link>http://blog.darevay.com/2009/01/remedial-scala-interpreting-scala-from-scala/</link>
		<comments>http://blog.darevay.com/2009/01/remedial-scala-interpreting-scala-from-scala/#comments</comments>
		<pubDate>Sun, 25 Jan 2009 16:17:56 +0000</pubDate>
		<dc:creator>dave</dc:creator>
				<category><![CDATA[scala]]></category>
		<category><![CDATA[scripting]]></category>

		<guid isPermaLink="false">http://blog.darevay.com/?p=183</guid>
		<description><![CDATA[For my &#8220;learning Scala&#8221; project, rooscaloo, I needed to dynamically generate and compile Scala code as rules are loaded into the engine. I had initially intended to do this as an internal DSL, but I honestly couldn&#8217;t think of a way to handle the fact that variables are bound at match time in the rete [...]]]></description>
			<content:encoded><![CDATA[<p>For my &#8220;learning Scala&#8221; project, <a href="http://rooscaloo.googlecode.com">rooscaloo</a>, I needed to dynamically generate and compile Scala code as rules are loaded into the engine. I had initially intended to do this as an<a href="http://debasishg.blogspot.com/2008/05/designing-internal-dsls-in-scala.html"> internal DSL</a>, but I honestly couldn&#8217;t think of a way to handle the fact that variables are bound at match time in the rete network. This is a learning project anyway, so I decided to just generate code and compile it.  Now, I could have sworn I had seen a Scala scripting example using <a href="http://jcp.org/en/jsr/detail?id=223">JSR-223</a> in a blog somewhere, but couldn&#8217;t find it again.  I only found one very basic example of how to do this, in the <a href="http://lampsvn.epfl.ch/trac/scala/ticket/874">Scala bug tracker</a>. So I figured I&#8217;d write down what I came up with.</p>
<p><em>Note that this code makes use of the <strong>scala.tools.nsc</strong> package which is not part of the standard Scala library. Thus, it may change in future versions of Scala. Also note that after the fact, I was able to track down two partial JSR-223 implementations <a href="http://ifcx.svn.sourceforge.net/viewvc/ifcx/thirdparty/scripting/engines/scala/src/org/ifcx/scripting/scala/ScalaScriptEngine.java?revision=91&amp;view=markup">here</a> and <a href="http://code.google.com/p/netgents/source/browse/trunk/scala-scripting/src/com/netgents/script/scala/ScalaScriptEngine.scala">here</a>. It&#8217;s comforting that I ended up in basically the same place.</em></p>
<p>The example given in the bug tracker works, but it&#8217;s very simple and there are two things it doesn&#8217;t do. First, it prints all its output to stdout, including error information. Second, it doesn&#8217;t show how to retrieve results from executing scripts. That is, code goes in, but nothing comes out. These are both solved pretty easily.  In the first case, we simply supply our own PrintWriter when instantiating the internal Scala interpreter:</p>
<pre>  private val writer = new java.io.StringWriter()
  private val interpreter = new ScalaInterpreter(new Settings(),
                                                 <strong>new PrintWriter(writer)</strong>);

  ...

  def exec(code : String) {
    // Clear the previous output buffer
    writer.getBuffer.setLength(0)

    // Execute the code and catch the result
    val ir = interpreter.interpret(code);

    // Return value or throw an exception based on result
    ir match {
      case Success =&gt; ()
      case Error =&gt; throw new ScriptException("error in: '" +
                                              code + "'\n" + writer toString)
      case Incomplete =&gt; throw new ScriptException("incomplete in :'" +
                                                   code + "'\n" + writer toString)
    }
  }</pre>
<p>For the second issue, returning results, I had to think a little longer, probably longer than necessary. <strong>scala.tools.nsc.Interpreter</strong> doesn&#8217;t return anything besides Success, Error, or Incomplete as seen above. But, you can pass objects in, so I ended up creating a container object to hold the result:</p>
<pre><strong>class ResultHolder(var value : Any)</strong>

  ...

  def eval(code : String) : Any = {
    // Clear the previous output buffer
    writer.getBuffer.setLength(0)

    // Create an object to hold the result and bind in the interpreter
    <strong>val holder = new ResultHolder(null)
    bind("$result__", holder);
</strong>
    // Execute the code and catch the result
    val ir = interpreter.interpret(<strong>"$result__.value = " + code</strong>);

    // Return value or throw an exception based on result
    ir match {
      case Success =&gt; <strong>holder.value</strong>
      case Error =&gt; throw new ScriptException("error in: '" +
                                              code + "'\n" + writer toString)
      case Incomplete =&gt; throw new ScriptException("incomplete in :'" +
                                                   code + "'\n" + writer toString)
    }
  }</pre>
<p>As you can see, I ended up with two separate routines. <strong>exec</strong> executes a Scala statement, ignoring its return value. This is useful for <strong>import</strong>s, class declarations, anything that does not evaluate to a value.  <strong>eval</strong> executes a Scala statement, catches the result and returns it.</p>
<p>Anyway, that&#8217;s about it. It seems that full JSR-223 support is in the plan for Scala. Until then, what I have here works for me and it was a good learning experience. <a href="http://code.google.com/p/rooscaloo/source/browse/trunk/rooscaloo/src/org/darevay/rooscaloo/Interpreter.scala">Here&#8217;s</a> the full source for my solution.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.darevay.com/2009/01/remedial-scala-interpreting-scala-from-scala/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Remedial Scala: XML</title>
		<link>http://blog.darevay.com/2009/01/remedial-scala-xml/</link>
		<comments>http://blog.darevay.com/2009/01/remedial-scala-xml/#comments</comments>
		<pubDate>Thu, 22 Jan 2009 04:20:29 +0000</pubDate>
		<dc:creator>dave</dc:creator>
				<category><![CDATA[scala]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://blog.darevay.com/?p=175</guid>
		<description><![CDATA[Tonight I finally got to the XML chapter of Programming in Scala. I was excited because I had seen a few examples on the web and wanted to try something out that wasn&#8217;t jdom or jax-b or whatever. So, I read the chapter and it was &#8230; short. The authors even admit in the conclusion [...]]]></description>
			<content:encoded><![CDATA[<p>Tonight I finally got to the XML chapter of <a href="http://www.artima.com/shop/programming_in_scala">Programming in Scala</a>. I was excited because I had seen a few examples on the web and wanted to try something out that wasn&#8217;t jdom or jax-b or whatever. So, I read the chapter and it was &#8230; short. The authors even admit in the conclusion that it barely scratches the surface and I have to agree :)</p>
<p>Anyway, my exercise for Scala&#8217;s XML support was adding an XML encoding of rules to <a href="http://rooscaloo.googlecode.com">rooscaloo</a>. My plan is to eventually write a parser with parser combinators, but that&#8217;s not until chapter 31.</p>
<p>Overall, Scala&#8217;s XML support is a dream compared to anything I&#8217;ve done in Java before. I began by creating some case classes for my AST:</p>
<pre>  case class RuleAST(name : String,
                     fireCode : String, unfireCode : String,
                     conditions : ConditionAST*)

  case class ConditionAST

  case class ObjectConditionAST(typeName : String,
                                binding : String,
                                testCode : String) extends ConditionAST

  case class NotAST(children : ConditionAST*) extends ConditionAST</pre>
<p>really simple stuff.  Here&#8217;s what a rule looks like in XML:</p>
<pre>    &lt;rule name="bob-is-not-a-cat-lover"&gt;
      &lt;if&gt;
        &lt;object type="Person" binding="p"&gt; p.name == "bob" &lt;/object&gt;
        &lt;not&gt;
            &lt;object type="Cat" binding="c"&gt; c.owner == p &lt;/object&gt;
        &lt;/not&gt;
      &lt;/if&gt;
      &lt;then&gt;println("bob does not fancy cats")&lt;/then&gt;
      &lt;unfire&gt;Tprintln("bob now owns a cat!")&lt;/unfire&gt;
    &lt;/rule&gt;</pre>
<p><em>(I couldn&#8217;t think of a better name for rule retraction than unfire&#8230;)</em></p>
<p>and here&#8217;s the satisfyingly compact code to convert an XML node to a RuleAST object:</p>
<pre>  def parseRule(node: scala.xml.Node) : RuleAST =
    RuleAST(node \ "@name" text,
            node \ "then" text,
            node \ "unfire" text,
            parseConditionList(node \ "if" \ "_") : _*)

  def parseConditionList(xml : scala.xml.NodeSeq) : Seq[ConditionAST] =
    for(e &lt;- xml)
      yield e match {
        case &lt;object&gt;{ _* }&lt;/object&gt; =&gt; parseObjectCondition(e)
        case &lt;not&gt;{ _* }&lt;/not&gt; =&gt; parseNot(e)
      }

  def parseObjectCondition(xml : scala.xml.Node) : ObjectConditionAST =
    ObjectConditionAST(xml \ "@type" text, xml \ "@binding" text, xml.text)

  def parseNot(xml : scala.xml.Node) : NotAST =
    NotAST(parseConditionList(xml \ "_") : _*)</pre>
<p>This could actually be much more compact but I chose to break it up into methods so I could test each construct individually. Still, I&#8217;m fairly certain there&#8217;s an even better way to do this, but this represents 45 minutes of work including unit tests, so I&#8217;m still pretty happy. One thing I discovered that wasn&#8217;t covered in the book was the use of the <strong>&#8220;_&#8221;</strong> wildcard to get all sub-elements of a node. For example we can see above that I use:</p>
<pre>   node \ "if" \ "_"</pre>
<p>to get the list of all child elements of the <strong>&lt;if&gt;</strong> node.</p>
<p>The <strong>match</strong> statement in <strong>parseConditionList</strong> is a little irritating to me and I feel like there&#8217;s probably a better way to do that.</p>
<p>Just like the book, I&#8217;ve obviously only started to scratch the surface of  Scala&#8217;s XML support. Since I also just read the chapter on <a href="http://www.scala-lang.org/node/112">extractors</a>, it seems like it might be interesting to define extractors to automatically convert XML to an AST. Of course, there&#8217;s also XPath, transforms, etc.</p>
<p>Maybe tomorrow night. It&#8217;s time to sleep.</p>
<p>p.s. here are some more Scala XML resources I&#8217;ve found useful:</p>
<ul>
<li><a href="http://www.scala-lang.org/node/131">A Tour of Scala &#8230;</a></li>
<li><a href="http://www.ibm.com/developerworks/java/library/x-scalaxml/index.html">A nice introduction</a> by Michael Galpin</li>
<li>The Scala XML <a href="http://burak.emir.googlepages.com/scalaxbook.docbk.html">&#8220;book&#8221;</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.darevay.com/2009/01/remedial-scala-xml/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Remedial Scala: Emulating C# Extension Methods</title>
		<link>http://blog.darevay.com/2009/01/remedial-scala-emulating-c-extension-methods/</link>
		<comments>http://blog.darevay.com/2009/01/remedial-scala-emulating-c-extension-methods/#comments</comments>
		<pubDate>Sat, 17 Jan 2009 20:57:40 +0000</pubDate>
		<dc:creator>dave</dc:creator>
				<category><![CDATA[scala]]></category>
		<category><![CDATA[c-sharp]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://blog.darevay.com/?p=163</guid>
		<description><![CDATA[I admit that, as a Java programmer, I&#8217;ve been jealous of C#&#8217;s extension methods for a while. Java&#8217;s getting pretty long in the tooth. While C# is adding lots of cool new features, Sun doesn&#8217;t have the balls to even add closures despite three working implementations to choose from.  I get bimonthly reports from a [...]]]></description>
			<content:encoded><![CDATA[<p>I admit that, as a Java programmer, I&#8217;ve been jealous of <a href="http://msdn.microsoft.com/en-us/library/bb383977.aspx">C#&#8217;s extension methods</a> for a while. Java&#8217;s getting pretty long in the tooth. While C# is adding lots of <a href="http://msdn.microsoft.com/en-us/library/bb308959.aspx">cool new features</a>, Sun doesn&#8217;t have the balls to even add closures despite<a href="http://rickyclarkson.blogspot.com/2008/12/java-just-died-no-closures-in-java-7.html"> three working implementations to choose from</a>.  I get bimonthly reports from a former co-worker and C# aficionado of this or that cool feature that Java will never have.  He loves to rub it in.</p>
<p>I know that extension methods are just <a href="http://www.hanselman.com/blog/HowDoExtensionMethodsWorkAndWhyWasANewCLRNotRequired.aspx">syntactic sugar</a> and any Lisp adherent will tell you they&#8217;re really just a poor man&#8217;s version of <a href="http://en.wikipedia.org/wiki/Generic_function">generic functions</a>.  But I want them nonetheless.</p>
<p>So, forget about Java. I&#8217;m pleased to say that in Scala it is easy to emulate extension methods. Here&#8217;s Microsoft&#8217;s example code in C#:</p>
<pre>namespace ExtensionMethods
{
    public static class MyExtensions
    {
        public static int WordCount(this String str)
        {
            return str.Split(new char[] { ' ', '.', '?' },
                             StringSplitOptions.RemoveEmptyEntries).Length;
        }
    }
}</pre>
<p>this just adds a <strong>WordCount</strong> method to the built in string class. Here&#8217;s the equivalent code in Scala, with a <strong>main</strong> method for good measure:</p>
<pre>object Main {

  implicit def stringToWordCounter(s : String) = {
    new {
      def wordCounts : Int = {
        s.split(Array(' ', '.', '?')).map(_ trim).filter(! _.isEmpty).size
      }
    }
  }

  def main(args: Array[String]) {
    println("Hello Extension Methods".wordCounts)
  }

}</pre>
<p>This makes use of Scala&#8217;s support for implicit conversions. If a method is called on an object that doesn&#8217;t exist on that object&#8217;s base type, the compiler searches for an implicit method (<strong>stringToWordCounter</strong> in this case) that converts to a type that does have that method. <strong>stringToWordCounter</strong> converts implicitly to a anonymous class that just happens to have a <strong>wordCount</strong> method. Cool. Note that the returned type can have any number of methods and may also implement traits (interfaces).</p>
<p>One thing I don&#8217;t know about is performance. Implicit conversions mean constructing a new intermediary object to call the method on. Maybe the compiler is smart? Anyway, this is <strong>remedial</strong> Scala, so I&#8217;ll worry about that later.</p>
<p>Also of note in this example is how much nicer the code to split, trim, and filter the input string is. I&#8217;m assuming there&#8217;s some other, more functional way to do this in C# without resorting to the long-winded <strong>StringSplitOptions.RemoveEmptyEntries</strong> flag.</p>
<p>That&#8217;s it for today&#8217;s installment of <strong>Remedial Scala</strong>. See you next time.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.darevay.com/2009/01/remedial-scala-emulating-c-extension-methods/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Buffalo buffalo buffalo buffalo …</title>
		<link>http://blog.darevay.com/2009/01/buffalo-buffalo-buffalo-buffalo/</link>
		<comments>http://blog.darevay.com/2009/01/buffalo-buffalo-buffalo-buffalo/#comments</comments>
		<pubDate>Fri, 16 Jan 2009 21:00:49 +0000</pubDate>
		<dc:creator>dave</dc:creator>
				<category><![CDATA[software engineering]]></category>
		<category><![CDATA[naming]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://blog.darevay.com/?p=158</guid>
		<description><![CDATA[Today I had a programming experience akin to the grammatically correct sentence &#8220;Buffalo buffalo Buffalo buffalo buffalo buffalo Buffalo buffalo.&#8221;
I&#8217;m working on a DSL that has values, which probably should have been called elements instead. I say this because I had to add a value keyword to the language which, of course, entailed a class [...]]]></description>
			<content:encoded><![CDATA[<p>Today I had a programming experience akin to the grammatically correct sentence <a href="http://en.wikipedia.org/wiki/Buffalo_buffalo_Buffalo_buffalo_buffalo_buffalo_Buffalo_buffalo">&#8220;Buffalo buffalo Buffalo buffalo buffalo buffalo Buffalo buffalo.&#8221;</a></p>
<p>I&#8217;m working on a DSL that has values, which probably should have been called elements instead. I say this because I had to add a <strong>value</strong> keyword to the language which, of course, entailed a class named <strong>ValueValue</strong>. See where this is going? I knew things had gotten out of hand when I wrote this line of code:</p>
<pre>    ValueValue valueValue = (ValueValue) value;</pre>
<p>Yipes. Maybe this is why some think naming constructs is <a href="http://stackoverflow.com/questions/421965/anyone-else-find-naming-classes-and-methods-one-of-the-most-difficult-part-in-pro">the hardest thing about programming.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.darevay.com/2009/01/buffalo-buffalo-buffalo-buffalo/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
