<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" xml:lang="en-US">
  <id>tag:blog.angelbob.com,2005:/posts</id>
  <link type="text/html" href="http://blog.angelbob.com" rel="alternate" />
  
  <title>Angelbob's Blog</title>
  <updated>2009-11-11T16:58:02Z</updated>
  <link rel="self" href="http://feeds.feedburner.com/AngelbobsBlog" type="application/atom+xml" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com" /><entry>
    <id>tag:blog.angelbob.com,2005:Post/247</id>
    <published>2009-11-11T16:58:02Z</published>
    <updated>2009-11-11T16:58:02Z</updated>
    <link type="text/html" href="http://feedproxy.google.com/~r/AngelbobsBlog/~3/Fv1ywnvn6tY/247-Ant-isn-t-going-to-become-my-new-favorite-Make-alike-tool---published" rel="alternate" />
    <title>Ant isn't going to become my new favorite Make-alike tool</title>
    <content type="html">&lt;p&gt;Ant has a &amp;lt;macrodef&amp;gt; tag.  It would appear that you can't use it to define new targets.  There don't seem to be any posts anywhere to this effect, or notes in the documentation -- but if you try it, you get an error about the target name being undefined, even if you've carefully given a name for that target.
&lt;/p&gt;
&lt;p&gt;Tags: published&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/AngelbobsBlog/~4/Fv1ywnvn6tY" height="1" width="1"/&gt;</content>
    <author>
      <name>Noah Gibbs</name>
    </author>
  <feedburner:origLink>http://blog.angelbob.com/posts/247-Ant-isn-t-going-to-become-my-new-favorite-Make-alike-tool---published</feedburner:origLink></entry>
  <entry>
    <id>tag:blog.angelbob.com,2005:Post/246</id>
    <published>2009-11-11T16:56:03Z</published>
    <updated>2009-11-11T16:56:28Z</updated>
    <link type="text/html" href="http://feedproxy.google.com/~r/AngelbobsBlog/~3/rVz2qoUfQEM/246-Facelets-updated---published-rails" rel="alternate" />
    <title>Facelets updated</title>
    <content type="html">&lt;p&gt;I put &lt;a href="http://blog.angelbob.com/toy/facelets.html"&gt;Facelets&lt;/a&gt; through JSLint until it had no warnings that annoyed me left.  Who knows, it may even work in IE by now with the Canvas library, though I don't have a machine at home running Windows to test on.&lt;/p&gt;

&lt;p&gt;Updating the version number seemed like a good idea, just for my own tracking purposes, so now it's 0.0.5.&lt;/p&gt;
&lt;p&gt;Tags: published rails&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/AngelbobsBlog/~4/rVz2qoUfQEM" height="1" width="1"/&gt;</content>
    <author>
      <name>Noah Gibbs</name>
    </author>
  <feedburner:origLink>http://blog.angelbob.com/posts/246-Facelets-updated---published-rails</feedburner:origLink></entry>
  <entry>
    <id>tag:blog.angelbob.com,2005:Post/245</id>
    <published>2009-11-09T21:59:54Z</published>
    <updated>2009-11-09T22:00:12Z</updated>
    <link type="text/html" href="http://feedproxy.google.com/~r/AngelbobsBlog/~3/wygXZ_4hw7w/245-EasyPartials-1---published-rails" rel="alternate" />
    <title>EasyPartials 0.0.1</title>
    <content type="html">&lt;p&gt;I took the code from &lt;a href="http://smellsblue.blogspot.com/2009/11/easy-partials-in-rails.html"&gt;Mike Stone's blog post on easy partials&lt;/a&gt; and packaged it up, quickly and roughly, as a gemplugin.&lt;/p&gt;

&lt;p&gt;Mike's now a coworker of mine.&lt;/p&gt;

&lt;p&gt;There's certainly more to do, but version 0.0.1 (nearly untested, quality commensurate with the version number) is up on RubyForge, or available &lt;a href="http://www.github.com/noahgibbs/easypartials"&gt;as easypartials on GitHub&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I've got a few plans for improving it in minor ways as well, though nothing earth-shattering.
&lt;/p&gt;
&lt;p&gt;Tags: published rails&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/AngelbobsBlog/~4/wygXZ_4hw7w" height="1" width="1"/&gt;</content>
    <author>
      <name>Noah Gibbs</name>
    </author>
  <feedburner:origLink>http://blog.angelbob.com/posts/245-EasyPartials-1---published-rails</feedburner:origLink></entry>
  <entry>
    <id>tag:blog.angelbob.com,2005:Post/244</id>
    <published>2009-11-07T01:07:06Z</published>
    <updated>2009-11-07T01:07:06Z</updated>
    <link type="text/html" href="http://feedproxy.google.com/~r/AngelbobsBlog/~3/bxbMwAyM4MI/244-Repost-from-long-gone-RubyGarden-List-of-Ruby-Idioms---published-ruby" rel="alternate" />
    <title>Repost from (long-gone) RubyGarden: List of Ruby Idioms</title>
    <content type="html">&lt;p&gt;Reposted from an old RubyGarden article&lt;/p&gt;

&lt;p&gt;&lt;hr /&gt;&lt;/p&gt;

&lt;p&gt;Ruby supports idiomatic programming. Please put your favorite Ruby idioms here. They don't have to be specific to Ruby.&lt;/p&gt;

&lt;p&gt;Using idioms does not always lead to the most readable code, but they can provide convenient shortcuts in many situations.&lt;/p&gt;

&lt;p&gt;&lt;h2&gt;I want this value, unless it evaluates to false in which case I want another value instead&lt;/h2&gt;&lt;/p&gt;

&lt;p&gt;Normally you would write something like&lt;/p&gt;

&lt;p&gt;&lt;pre&gt;
&lt;br /&gt;  if myvar
&lt;br /&gt;    return myvar
&lt;br /&gt;  else
&lt;br /&gt;    return another_value
&lt;br /&gt;  end
&lt;br /&gt;&lt;/pre&gt;&lt;/p&gt;

&lt;p&gt;But you can use the &lt;code&gt;||&lt;/code&gt; operator instead:&lt;/p&gt;

&lt;p&gt;&lt;pre&gt;return myvar || another_value&lt;/pre&gt;&lt;/p&gt;

&lt;p&gt;You can also use the &lt;code&gt;||=&lt;/code&gt; operator to assign a value to a variable if it evaluates to false. That is, instead of writing&lt;/p&gt;

&lt;p&gt;&lt;pre&gt;myvar = another_value unless myvar&lt;/pre&gt;&lt;/p&gt;

&lt;p&gt;or&lt;/p&gt;

&lt;p&gt;&lt;pre&gt;
&lt;br /&gt;  unless myvar
&lt;br /&gt;    myvar = another_value
&lt;br /&gt;  end&lt;/p&gt;

&lt;p&gt;&lt;/pre&gt;&lt;/p&gt;

&lt;p&gt;you could write&lt;/p&gt;

&lt;p&gt;&lt;pre&gt;myvar ||= another_value&lt;/pre&gt;&lt;/p&gt;

&lt;p&gt;handy to put anything or anthing else, if it doesn't exist(I didn't know about that possibility for a long time):&lt;/p&gt;

&lt;p&gt;&lt;pre&gt;puts page_title || "Untitled"&lt;/pre&gt;&lt;/p&gt;

&lt;p&gt;&lt;h2&gt;I want this value, unless it is nil in which case I want another value instead&lt;/h2&gt;&lt;/p&gt;

&lt;p&gt;Please note the difference between this idiom and the idiom above. In order to clearly see the different behavior assume myvar is &lt;code&gt;false&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;pre&gt;
&lt;br /&gt;   myvar.nil? ? other : myvar
&lt;br /&gt;&lt;/pre&gt;&lt;/p&gt;

&lt;p&gt;And setting myvar if it is nil (again not if it is false )&lt;/p&gt;

&lt;p&gt;&lt;pre&gt;
&lt;br /&gt;   myvar = newvalue if myvar.nil?
&lt;br /&gt;&lt;/pre&gt;&lt;/p&gt;

&lt;p&gt;while&lt;/p&gt;

&lt;p&gt;&lt;pre&gt;  myvar ||= newvalue&lt;/pre&gt;&lt;/p&gt;

&lt;p&gt;corresponds to&lt;/p&gt;

&lt;p&gt;&lt;pre&gt;
&lt;br /&gt;  myvar = newvalue unless myvar
&lt;br /&gt;&lt;/pre&gt;&lt;/p&gt;

&lt;p&gt;&lt;h2&gt;Invoke this method on my object, unless I have no object&lt;/h2&gt;&lt;/p&gt;

&lt;p&gt;If you want to invoke a method on an object in a variable, but in some cases the variable could be &lt;code&gt;nil&lt;/code&gt;, you could do this:&lt;/p&gt;

&lt;p&gt;&lt;pre&gt;
&lt;br /&gt;  if myvar
&lt;br /&gt;    return myvar.size
&lt;br /&gt;  else
&lt;br /&gt;    return nil
&lt;br /&gt;  end
&lt;br /&gt;&lt;/pre&gt;&lt;/p&gt;

&lt;p&gt;But using the &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt; operator you can turn this into:&lt;/p&gt;

&lt;p&gt;&lt;pre&gt;
&lt;br /&gt;  return myvar &amp;amp;&amp;amp; myvar.size
&lt;br /&gt;&lt;/pre&gt;
&lt;br /&gt;If you want to return something other than &lt;code&gt;nil&lt;/code&gt; if both &lt;code&gt;myvar&lt;/code&gt; and &lt;code&gt;myvar.size&lt;/code&gt; are &lt;code&gt;nil&lt;/code&gt;, you can use the &lt;code&gt;||&lt;/code&gt; operator again:&lt;/p&gt;

&lt;p&gt;&lt;pre&gt;
&lt;br /&gt;  return myvar &amp;amp;&amp;amp; myvar.size &lt;code&gt;||&lt;/code&gt; 0
&lt;br /&gt;&lt;/pre&gt;
&lt;br /&gt;... or use the ternary operator:
&lt;br /&gt;&lt;pre&gt;
&lt;br /&gt;  return myvar ? myvar.size : 0
&lt;br /&gt;&lt;/pre&gt;&lt;/p&gt;

&lt;p&gt;&lt;h2&gt;Run this code only when the file is the main program&lt;/h2&gt;&lt;/p&gt;

&lt;p&gt;Sometimes it is useful to add code to a file that is executed only if the file is run as the main program, but is ignored otherwise.  Here's how:
&lt;br /&gt;&lt;pre&gt;
&lt;br /&gt;  if $PROGRAM_NAME == __FILE__
&lt;br /&gt;      do_stuff
&lt;br /&gt;  end
&lt;br /&gt;&lt;/pre&gt;&lt;/p&gt;

&lt;p&gt;Some applications of this idiom:
&lt;br /&gt;&lt;ul&gt;
&lt;br /&gt;&lt;li&gt; Include test code at the bottom of a library file, so that running the file as a program tests the file.&lt;/li&gt;
&lt;br /&gt;&lt;li&gt; Require a program's file into a test module, so that its functions can be tested without running the whole program.&lt;/li&gt;
&lt;br /&gt;&lt;li&gt; Provide a Ruby API to a program's functionality.&lt;/li&gt;
&lt;br /&gt;&lt;/ul&gt;&lt;/p&gt;

&lt;p&gt;&lt;h2&gt;Destroy this object when it goes out of scope&lt;/h2&gt;&lt;/p&gt;

&lt;p&gt;C++ programmers are used to using the "construction acquires, destruction releases" idiom to control the management of external resources such as files or sockets.  They are often put off by Ruby because garbage collection and finalizers don't act like objects on the stack with destructors.&lt;/p&gt;

&lt;p&gt;Ruby's blocks can be used to the same end.  In the class that represents an external resource, define a class method that takes a block.  That method&lt;/p&gt;

&lt;p&gt;&lt;ul&gt;
&lt;br /&gt;&lt;li&gt; allocates an instance of the class, thereby acquiring the resource
&lt;br /&gt;&lt;li&gt; passes the instance to the block, and then
&lt;br /&gt;&lt;li&gt; releases the resource when the block returns.
&lt;br /&gt;&lt;/ul&gt;&lt;/p&gt;

&lt;p&gt;Here's an abstract example of this idiom in use:&lt;/p&gt;

&lt;p&gt;&lt;pre&gt;
&lt;br /&gt; Resource.open( identifier ) do |resource|
&lt;br /&gt;     process( resource )
&lt;br /&gt; end
&lt;br /&gt; # resource is now closed
&lt;br /&gt;&lt;/pre&gt;&lt;/p&gt;

&lt;p&gt;The implementation of the class method must use a &lt;code&gt;begin...ensure&lt;/code&gt; statement to ensure that the resource is always released:&lt;/p&gt;

&lt;p&gt;&lt;pre&gt;
&lt;br /&gt; def Resource.open( identifier )  # :yield: resource
&lt;br /&gt;     resource = Resource.new( identifier )
&lt;br /&gt;     yield resource
&lt;br /&gt; ensure
&lt;br /&gt;     resource.close
&lt;br /&gt; end
&lt;br /&gt;&lt;/pre&gt;&lt;/p&gt;

&lt;p&gt;A practical example is the &lt;code&gt;open&lt;/code&gt; method of the &lt;code&gt;File&lt;/code&gt; class.&lt;/p&gt;

&lt;p&gt;Often, though, the behaviour of new is conditional, depending on whether a block is provided.  For instance, &lt;code&gt;File.open&lt;/code&gt; behaves just like &lt;code&gt;File.new&lt;/code&gt; &lt;em&gt;unless&lt;/em&gt; you provide a block.  This way, programmers can choose between the interfaces.  Applying that to our general &lt;code&gt;Resource&lt;/code&gt; class, we get this:&lt;/p&gt;

&lt;p&gt;&lt;pre&gt;
&lt;br /&gt; def Resource.open( identifier )  # :yield: resource
&lt;br /&gt;     resource = Resource.new( identifier )
&lt;br /&gt;     if block_given?
&lt;br /&gt;         begin
&lt;br /&gt;             yield resource
&lt;br /&gt;         ensure
&lt;br /&gt;             resource.close
&lt;br /&gt;         end
&lt;br /&gt;     else
&lt;br /&gt;         return resource
&lt;br /&gt;     end
&lt;br /&gt; end
&lt;br /&gt;&lt;/pre&gt;
&lt;br /&gt;Thus:
&lt;br /&gt;&lt;pre&gt;
&lt;br /&gt; r = Resource.open( x )             # -&amp;gt; some Resource object&lt;/p&gt;

&lt;p&gt; Resource.open( x ) do |r|
&lt;br /&gt;   # use 'r' ...
&lt;br /&gt; end
&lt;br /&gt;&lt;/pre&gt;&lt;/p&gt;

&lt;p&gt;&lt;h2&gt;Hashes of lists&lt;/h2&gt;&lt;/p&gt;

&lt;p&gt;Sometimes, I want to fill out a hash with keys mapped to lists of arbitrary elements. Instead of the cumbersome approach:&lt;/p&gt;

&lt;p&gt;&lt;pre&gt;
&lt;br /&gt;    h[key] = [] unless h.has_key? key
&lt;br /&gt;    h[key] &amp;lt;&amp;lt; val
&lt;br /&gt;&lt;/pre&gt;
&lt;br /&gt;which requires three key lookups, you can turn it into
&lt;br /&gt;&lt;pre&gt;
&lt;br /&gt;    ary = (h[key] &lt;code&gt;||&lt;/code&gt;= [])
&lt;br /&gt;    ary &amp;lt;&amp;lt; val
&lt;br /&gt;&lt;/pre&gt;
&lt;br /&gt;or the even shorter, but less readable:&lt;/p&gt;

&lt;p&gt;&lt;pre&gt;
&lt;br /&gt;    (h[key] &lt;code&gt;||&lt;/code&gt;= []) &amp;lt;&amp;lt; val
&lt;br /&gt;&lt;/pre&gt;&lt;/p&gt;

&lt;p&gt;This code still takes two lookups, as &lt;code&gt;h[key] ||= []&lt;/code&gt; is equivalent to &lt;code&gt;h[key] = h[key] || []&lt;/code&gt;. An alternative is:
&lt;br /&gt;&lt;pre&gt;
&lt;br /&gt;    a = h.fetch(key) { h[key] = [] }
&lt;br /&gt;    a &amp;lt;&amp;lt; val
&lt;br /&gt;&lt;/pre&gt;&lt;/p&gt;

&lt;p&gt;This involves one lookup in the common case, and two lookups when creating the sub-list.&lt;/p&gt;

&lt;p&gt;You could also consider using the following:
&lt;br /&gt;&lt;pre&gt;
&lt;br /&gt;    h = Hash.new []
&lt;br /&gt;    h[key] &amp;lt;&amp;lt;= val
&lt;br /&gt;&lt;/pre&gt;
&lt;br /&gt;but note that this doesn't quite work:
&lt;br /&gt;&lt;pre&gt;
&lt;br /&gt;    irb(main):001:0&amp;gt; h = Hash.new []
&lt;br /&gt;    {}
&lt;br /&gt;    irb(main):002:0&amp;gt; h[1] &amp;lt;&amp;lt;= 2; h[2] &amp;lt;&amp;lt;= 3
&lt;br /&gt;    [2, 3]
&lt;br /&gt;    irb(main):003:0&amp;gt; h
&lt;br /&gt;    {1=&amp;gt;[2, 3], 2=&amp;gt;[2, 3]}
&lt;br /&gt;&lt;/pre&gt;&lt;/p&gt;

&lt;p&gt;Only one array is ever created; we want a new array for every key.  So instead we write:&lt;/p&gt;

&lt;p&gt;&lt;pre&gt;
&lt;br /&gt;    h = Hash.new { [] }
&lt;br /&gt;    h[key] &amp;lt;&amp;lt;= val
&lt;br /&gt;&lt;/pre&gt;&lt;/p&gt;

&lt;p&gt;You can also use this form:&lt;/p&gt;

&lt;p&gt;&lt;pre&gt;
&lt;br /&gt;    h = Hash.new{ |h,v| h[v]=[] }
&lt;br /&gt;    h[key] &amp;lt;&amp;lt; val
&lt;br /&gt;&lt;/pre&gt;&lt;/p&gt;

&lt;p&gt;&lt;h2&gt;Jump to first non-false expression&lt;/h2&gt;&lt;/p&gt;

&lt;p&gt;Blame Hal Fulton for this (see &lt;a href="http://www.google.com/url?sa=D&amp;q=HTTP%3A%2F%2Fwww.ruby-talk.org%2F58095"&gt;[ruby-talk:58095]&lt;/a&gt;).  His toy language at grad school included a &lt;tt&gt;test&lt;/tt&gt; statement.  It's similar enough to the &lt;tt&gt;case&lt;/tt&gt; statement that ...&lt;/p&gt;

&lt;p&gt;For example:
&lt;br /&gt;&lt;pre&gt;
&lt;br /&gt;   case true
&lt;br /&gt;   when x &amp;lt; 0;             puts "x &amp;lt; 0"
&lt;br /&gt;   when 0 &amp;lt;= x &amp;amp;&amp;amp; x &amp;lt; 1;   puts "x in interval [0,1)"
&lt;br /&gt;   when 1 &amp;lt;= x &amp;amp;&amp;amp; x &amp;lt; 2;   puts "x in interval [1,2)"
&lt;br /&gt;   when 2 &amp;lt;= x;            puts "x &amp;gt;= 2"
&lt;br /&gt;   end
&lt;br /&gt;&lt;/pre&gt;&lt;/p&gt;

&lt;p&gt;In fact, you can even omit the "true" and have the same semantics:&lt;/p&gt;

&lt;p&gt;&lt;pre&gt;
&lt;br /&gt;   case
&lt;br /&gt;   when x &amp;lt; 0;             puts "x &amp;lt; 0"
&lt;br /&gt;   when 0 &amp;lt;= x &amp;amp;&amp;amp; x &amp;lt; 1;   puts "x in interval [0,1)"
&lt;br /&gt;   when 1 &amp;lt;= x &amp;amp;&amp;amp; x &amp;lt; 2;   puts "x in interval [1,2)"
&lt;br /&gt;   when 2 &amp;lt;= x;            puts "x &amp;gt;= 2"
&lt;br /&gt;   end
&lt;br /&gt;&lt;/pre&gt;
&lt;br /&gt;Further, you can take advantage of Ruby's functional nature:
&lt;br /&gt;&lt;pre&gt;
&lt;br /&gt;   puts case
&lt;br /&gt;   when x &amp;lt; 0;            "x &amp;lt; 0"
&lt;br /&gt;   when 0 &amp;lt;= x &amp;amp;&amp;amp; x &amp;lt; 1;  "x in interval [0,1)"
&lt;br /&gt;   when 1 &amp;lt;= x &amp;amp;&amp;amp; x &amp;lt; 2;  "x in interval [1,2)"
&lt;br /&gt;   when 2 &amp;lt;= x;           "x &amp;gt;= 2"
&lt;br /&gt;   end
&lt;br /&gt;&lt;/pre&gt;&lt;/p&gt;

&lt;p&gt;&lt;h2&gt;easy to access elements in array&lt;/h2&gt;&lt;/p&gt;

&lt;p&gt;&lt;pre&gt;
&lt;br /&gt;   val = [1, 2, 3]
&lt;br /&gt;   a, b, c = val      # Observe this particular line
&lt;br /&gt;   p a    #=&amp;gt; 1
&lt;br /&gt;   p b    #=&amp;gt; 2
&lt;br /&gt;   p c    #=&amp;gt; 3
&lt;br /&gt;&lt;/pre&gt;&lt;/p&gt;

&lt;p&gt;This can be useful when you want a method to return multiple values.&lt;/p&gt;

&lt;p&gt;&lt;pre&gt;
&lt;br /&gt;   def test
&lt;br /&gt;      [42, "ruby"]
&lt;br /&gt;   end
&lt;br /&gt;   val, str = test
&lt;br /&gt;&lt;/pre&gt;
&lt;br /&gt;Or, if you want just the second value:
&lt;br /&gt;&lt;pre&gt;
&lt;br /&gt;   val = test[1]
&lt;br /&gt;&lt;/pre&gt;&lt;h2&gt;from my_hash['foo'] to my_hash.foo&lt;/h2&gt;
&lt;br /&gt;&lt;p&gt;
&lt;br /&gt;&lt;p&gt;
&lt;br /&gt;If you want to reference a value corresponding to a key in hashes you may use a &lt;em&gt;dotted&lt;/em&gt; notation by several means (take a look at &lt;a href="http://www.google.com/url?sa=D&amp;q=HTTP%3A%2F%2Fblade.nagaokaut.ac.jp%2Fcgi-bin%2Fscat.rb%2Fruby%2Fruby-talk%2F92897"&gt;[ruby-talk:92897]&lt;/a&gt; too).&lt;/p&gt;

&lt;p&gt;&lt;p&gt;
&lt;br /&gt;&lt;ul&gt;
&lt;br /&gt;&lt;li&gt; Hash could be extended to act like a Struct
&lt;br /&gt;&lt;/ul&gt;&lt;p&gt;
&lt;br /&gt;&lt;pre&gt;
&lt;br /&gt;   class Hash
&lt;br /&gt;     def method_missing(meth,*args)
&lt;br /&gt;       if /=$/=~(meth=meth.id2name) then
&lt;br /&gt;         self[meth[0...-1]] = (args.length&amp;lt;2 ? args[0] : args)
&lt;br /&gt;       else
&lt;br /&gt;         self[meth]
&lt;br /&gt;       end
&lt;br /&gt;     end
&lt;br /&gt;   end
&lt;br /&gt;   x = { 'name'=&amp;gt;'Gavin', 'age'=&amp;gt;31 }
&lt;br /&gt;   x.weight = 171
&lt;br /&gt;   x.feet = ['left','right']
&lt;br /&gt;   puts x.name , x.weight , x.foo , x.inspect
&lt;br /&gt;   #=&amp;gt;Gavin
&lt;br /&gt;   #=&amp;gt;171
&lt;br /&gt;   #=&amp;gt;nil
&lt;br /&gt;   #=&amp;gt;{"name"=&amp;gt;"Gavin", "weight"=&amp;gt;171, "feet"=&amp;gt;["left", "right"], "age"=&amp;gt;31}&lt;/p&gt;

&lt;p&gt;&lt;/pre&gt;&lt;p&gt;
&lt;br /&gt;&lt;ul&gt;
&lt;br /&gt;&lt;li&gt; You may get a Struct from a Hash using &lt;a href="HTTP://rubyforge.org/snippet/detail.php?type=snippet&amp;amp;id=1"&gt;[Dan Berger's code snippet on RubyForge]&lt;/a&gt; &lt;i&gt;Is this worthwhile, given that &lt;a href="http://rubygarden.org/Ruby/page/show/OpenStruct" class="wikipagelink"&gt;OpenStruct&lt;/a&gt; is in the standard library?&lt;/i&gt;
&lt;br /&gt;&lt;/ul&gt;&lt;p&gt;
&lt;br /&gt;&lt;pre&gt;
&lt;br /&gt;   class Hash
&lt;br /&gt;      def to_struct(struct_name)
&lt;br /&gt;         Struct.new(struct_name,*keys).new(*values)
&lt;br /&gt;      end
&lt;br /&gt;   end
&lt;br /&gt;   h = {:name=&amp;gt;"Dan"}
&lt;br /&gt;   s = h.to_struct("Foo")
&lt;br /&gt;   puts "name: " + s.name
&lt;br /&gt;&lt;/pre&gt;&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;p&gt;
&lt;br /&gt;&lt;ul&gt;
&lt;br /&gt;&lt;li&gt; &lt;a href="http://rubygarden.org/Ruby/page/show/OpenStruct" class="wikipagelink"&gt;OpenStruct&lt;/a&gt; merges Hash and Struct behaviours together.  It is like a hash, but it uses the &lt;code&gt;obj.key&lt;/code&gt; notation.
&lt;br /&gt;&lt;/ul&gt;&lt;p&gt;
&lt;br /&gt;&lt;pre&gt;
&lt;br /&gt;   require 'ostruct'
&lt;br /&gt;   x = OpenStruct.new('name' =&amp;gt; 'Gavin', 'age' =&amp;gt; 31)
&lt;br /&gt;   x.weight = 171
&lt;br /&gt;   x.feet = ['left', 'right']&lt;/p&gt;

&lt;p&gt;   puts x.name       # -&amp;gt; "Gavin"
&lt;br /&gt;   puts x.weight     # -&amp;gt; 171
&lt;br /&gt;   puts x.foo        # -&amp;gt; nil
&lt;br /&gt;   puts x.feet       # -&amp;gt; ['left', 'right']&lt;/p&gt;

&lt;p&gt;&lt;/pre&gt;
&lt;br /&gt;&lt;h2&gt;Automagical Variable Instantiation with a Hash&lt;/h2&gt;&lt;/p&gt;

&lt;p&gt;If you create a hash with a default value, you can get automagical instantiation for that type.  For instance:
&lt;br /&gt;&lt;p&gt;
&lt;br /&gt;&lt;pre&gt;
&lt;br /&gt;  mlock = Hash.new {|h,k| h[k] = Mutex.new }
&lt;br /&gt;&lt;/pre&gt;&lt;p&gt;
&lt;br /&gt;then later on throughout my code when I need a mutex I say
&lt;br /&gt;&lt;p&gt;
&lt;br /&gt;&lt;pre&gt;
&lt;br /&gt;  mlock["entry"].synchronize do
&lt;br /&gt;    # blah
&lt;br /&gt;  end
&lt;br /&gt;&lt;/pre&gt;&lt;p&gt;&lt;/p&gt;

&lt;p&gt;and then later on when I need another I use
&lt;br /&gt;&lt;p&gt;
&lt;br /&gt;&lt;pre&gt;
&lt;br /&gt;  mlock["exit"].synchronize do
&lt;br /&gt;    # exit blah
&lt;br /&gt;  end
&lt;br /&gt;&lt;/pre&gt;
&lt;br /&gt;This prevents me from needing to declare a new mutex in &lt;code&gt;initialize&lt;/code&gt; every time I need another mutex in that class.&lt;/p&gt;

&lt;p&gt;-- &lt;a href="http://rubygarden.org/Ruby/page/show/CharlesComstock" class="wikipagelink"&gt;CharlesComstock&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;h2&gt;Simulating Java's synchronized keyword&lt;/h2&gt;&lt;/p&gt;

&lt;p&gt;First thing we do, is add the synchronized method to object:
&lt;br /&gt;&lt;pre&gt;class Object
&lt;br /&gt;  def synchronized(obj)
&lt;br /&gt;     @_class_locker ||= Hash.new {|h,k| h[k] = Mutex.new }
&lt;br /&gt;     @_class_locker[obj.id].synchronize { yield }
&lt;br /&gt;  end
&lt;br /&gt;end&lt;/pre&gt;&lt;/p&gt;

&lt;p&gt;Once we have this, we can use synchronized(variable_name), just like you can in java!
&lt;br /&gt;&lt;p&gt;
&lt;br /&gt;Example:
&lt;br /&gt;&lt;pre&gt;require 'thread'&lt;/p&gt;

&lt;p&gt;mytest = "hello world"&lt;/p&gt;

&lt;p&gt;thread1 = Thread.new do
&lt;br /&gt;  # java like syntax
&lt;br /&gt;  synchronized(mytest) {
&lt;br /&gt;    puts "1: mytest: #{mytest}"
&lt;br /&gt;    sleep 1
&lt;br /&gt;    mytest = 'goodbye world'
&lt;br /&gt;  }
&lt;br /&gt;end&lt;/p&gt;

&lt;p&gt;thread2 = Thread.new do
&lt;br /&gt;  # ruby syntax
&lt;br /&gt;  synchronized mytest do
&lt;br /&gt;    puts "2: mytest: #{mytest}"
&lt;br /&gt;  end  
&lt;br /&gt;end&lt;/p&gt;

&lt;p&gt;thread1.join
&lt;br /&gt;thread2.join&lt;/pre&gt;
&lt;br /&gt;&lt;p&gt;
&lt;br /&gt;Results in:
&lt;br /&gt;&lt;pre&gt;1: mytest: hello world
&lt;br /&gt;2: mytest: goodbye world
&lt;br /&gt;&lt;/pre&gt;
&lt;br /&gt;&lt;p&gt;
&lt;br /&gt;&lt;hr noshade class="wikiline" size="1"&gt;
&lt;br /&gt;&lt;p&gt;&lt;/p&gt;

&lt;p&gt;Or, for a more ruby'ish example that does not leave any dependencies on the Object class:
&lt;br /&gt;&lt;p&gt;
&lt;br /&gt;&lt;pre&gt;require 'thread'
&lt;br /&gt;module JavaEmulation
&lt;br /&gt;  def synchronized(on)
&lt;br /&gt;     @_class_locker ||= Hash.new {|h,k| h[k] = Mutex.new }
&lt;br /&gt;     @_class_locker[on.id].synchronize { yield }
&lt;br /&gt;  end
&lt;br /&gt;end&lt;/p&gt;

&lt;p&gt;class MyClass
&lt;br /&gt;  include JavaEmulation
&lt;br /&gt;  def start_threads
&lt;br /&gt;    mytest = "hello world"&lt;/p&gt;

&lt;p&gt;    thread1 = Thread.new do
&lt;br /&gt;      synchronized(mytest) {
&lt;br /&gt;        puts "1: mytest: #{mytest}"
&lt;br /&gt;        sleep 1
&lt;br /&gt;        mytest = 'goodbye world'
&lt;br /&gt;      }
&lt;br /&gt;    end&lt;/p&gt;

&lt;p&gt;    thread2 = Thread.new do
&lt;br /&gt;      synchronized mytest do
&lt;br /&gt;        puts "2: mytest: #{mytest}"
&lt;br /&gt;      end  
&lt;br /&gt;    end&lt;/p&gt;

&lt;p&gt;    thread1.join
&lt;br /&gt;    thread2.join
&lt;br /&gt;  end
&lt;br /&gt;end&lt;/p&gt;

&lt;p&gt;MyClass.new.start_threads&lt;/pre&gt;
&lt;br /&gt;&lt;h2&gt;Initializing complex objects&lt;/h2&gt;
&lt;br /&gt;&lt;p&gt;
&lt;br /&gt;&lt;p&gt;
&lt;br /&gt;If an object has a lot of data that needs to be provided on initialization, the traditional method call arrangement is unsuitable, because you need to remember the order of arguments, and you can't omit arguments that you don't care about.  Ruby offers some relief with variable argument lists (&lt;code&gt;def foo(x, y, *args)&lt;/code&gt;) and default values (&lt;code&gt;def foo(x, y=5, *args)&lt;/code&gt;, but for seriously complicated objects, these are typically not enough.
&lt;br /&gt;&lt;p&gt;
&lt;br /&gt;Passing a hash is one option, using keys of the hash to name the attributes you wish to set.  This allows arbitrary ordering, and selective omission (to allow default values).  See &lt;a href="http://rubygarden.org/Ruby/page/show/KeywordArguments" class="wikipagelink"&gt;KeywordArguments&lt;/a&gt; for a look at this technique.&lt;/p&gt;

&lt;p&gt;&lt;p&gt;
&lt;br /&gt;But I prefer the following:
&lt;br /&gt;&lt;pre&gt;
&lt;br /&gt;  complex_object = ComplexObject.new do |c|
&lt;br /&gt;    c.width = 15
&lt;br /&gt;    c.height = 20
&lt;br /&gt;    c.colour = :red
&lt;br /&gt;    c.position = [12, 4]
&lt;br /&gt;  end
&lt;br /&gt;&lt;/pre&gt;
&lt;br /&gt;&lt;p&gt;
&lt;br /&gt;The benefits:
&lt;br /&gt;&lt;ul&gt;
&lt;br /&gt;&lt;li&gt; it's easy to code for (see below)
&lt;br /&gt;&lt;li&gt; the syntax is arguably more natural than the hash approach (it's very Rubyesque)
&lt;br /&gt;&lt;li&gt; it's self-documenting; you can look at the documentation for the &lt;code&gt;ComplexObject&lt;/code&gt; class to see which attributes you can set&lt;/p&gt;

&lt;p&gt;&lt;/ul&gt;&lt;p&gt;
&lt;br /&gt;The disadvantages:
&lt;br /&gt;&lt;ul&gt;
&lt;br /&gt;&lt;li&gt; all attributes used must be public
&lt;br /&gt;&lt;/ul&gt;-Hey, just create a Struct to be passed to the block. Then this disadvantage disappears.
&lt;br /&gt;&lt;p&gt;
&lt;br /&gt;The implementation:
&lt;br /&gt;&lt;p&gt;
&lt;br /&gt;&lt;pre&gt;
&lt;br /&gt;  class ComplexObject
&lt;br /&gt;    attr_accessor :colour, :position, :width, :height, :depth, :label&lt;/p&gt;

&lt;p&gt;      #
&lt;br /&gt;      # Width and height _must_ be specified; all others are optional.
&lt;br /&gt;      #
&lt;br /&gt;    def initialize  # :yield: c
&lt;br /&gt;        # Set default values.
&lt;br /&gt;      @colour = :white
&lt;br /&gt;      @position = [0,0]
&lt;br /&gt;      @depth = 0
&lt;br /&gt;      @label = ""
&lt;br /&gt;      yield self
&lt;br /&gt;        # If all attributes are optional, we would code:
&lt;br /&gt;        #   yield self if block_given?
&lt;br /&gt;      if @width.nil? or @height.nil?
&lt;br /&gt;        raise TypeError, "Incompletely specified ComplexObject"
&lt;br /&gt;      end
&lt;br /&gt;    end
&lt;br /&gt;  end
&lt;br /&gt;&lt;/pre&gt;
&lt;br /&gt;&lt;p&gt;
&lt;br /&gt;This, to me, is a very natural coding style.  The default values are easily seen in the code (usually browsable via RDoc).  The post-condition is easily specified.  All in eight clear lines of code for a class with six attributes.
&lt;br /&gt;&lt;p&gt;
&lt;br /&gt;Morover, this style is extremely useful when creating hierarchies of composite objects. 
&lt;br /&gt;i.e.: FXRuby implements this idiom to support well structured GUI code.&lt;/p&gt;

&lt;p&gt;&lt;pre&gt;
&lt;br /&gt;FXMainWindow.new($app, ...){|mainwindow|
&lt;br /&gt;   FXHorizontalFrame.new(mainwindow){|hf|
&lt;br /&gt;      FXLabel.new(hf){|label| ... }
&lt;br /&gt;      FXButton.new(hf){|button| .. }
&lt;br /&gt;   }
&lt;br /&gt;}
&lt;br /&gt;&lt;/pre&gt;
&lt;br /&gt;As you can see, the hierarchic composition of the GUI is reflected by the structure of the code which increases readability and maintainability. I use this idiom whenever I need to build complex composite objects. --henon
&lt;br /&gt;&lt;h2&gt;Initializer alternatives&lt;/h2&gt;
&lt;br /&gt;&lt;p&gt;
&lt;br /&gt;&lt;p&gt;
&lt;br /&gt;Ruby allows only one initializer method for a class, which is a problem if you want initialization to be done in different ways, or with different arguments.
&lt;br /&gt;&lt;p&gt;
&lt;br /&gt;One of the dynamic invocation features of Ruby can be used to relieve this, by writing a class in the following idiom:
&lt;br /&gt;&lt;p&gt;
&lt;br /&gt;&lt;pre&gt;
&lt;br /&gt;  class Example&lt;/p&gt;

&lt;p&gt;    def initialize( method_name, *parameters_array )
&lt;br /&gt;      send(method_name, parameters_array)
&lt;br /&gt;    end&lt;/p&gt;

&lt;p&gt;  private
&lt;br /&gt;    def initialize1( parameters_array )
&lt;br /&gt;      # construct in one way
&lt;br /&gt;    end&lt;/p&gt;

&lt;p&gt;    def initialize2( parameters_array )
&lt;br /&gt;      # construct in another way
&lt;br /&gt;    end&lt;/p&gt;

&lt;p&gt;  end
&lt;br /&gt;&lt;/pre&gt;
&lt;br /&gt;&lt;p&gt;&lt;/p&gt;

&lt;p&gt;where the arguments are collected into an array, and a name string is used to delegate to a particular method.
&lt;br /&gt;&lt;p&gt;
&lt;br /&gt;Such a class can then be instantiated variously by the client like this:
&lt;br /&gt;&lt;p&gt;
&lt;br /&gt;&lt;pre&gt;
&lt;br /&gt;  e1 = Example.new( 'initialize1', 1, 2, 3 )
&lt;br /&gt;  e2 = Example.new( 'initialize2', 'a', 'b' )
&lt;br /&gt;&lt;/pre&gt;
&lt;br /&gt;&lt;p&gt;
&lt;br /&gt;which practically gives overloaded initializer methods.
&lt;br /&gt;&lt;p&gt;
&lt;br /&gt;(Ideally, it would be preferable to be able to instantiate like this:
&lt;br /&gt;&lt;p&gt;
&lt;br /&gt;&lt;pre&gt;
&lt;br /&gt;  e = Example.new_alternative1( 1, 2, 3 )
&lt;br /&gt;&lt;/pre&gt;&lt;/p&gt;

&lt;p&gt;&lt;p&gt;
&lt;br /&gt;There is a hack for that:
&lt;br /&gt;&lt;p&gt;
&lt;br /&gt;&lt;pre&gt;
&lt;br /&gt;  class Example&lt;/p&gt;

&lt;p&gt;    def initialize( method_name, *parameters_array )
&lt;br /&gt;      send(method_name, parameters_array)
&lt;br /&gt;    end&lt;/p&gt;

&lt;p&gt;    def self.new_alternative1(*parameters_array)
&lt;br /&gt;      return Example.new('initialize1', parameters_array)
&lt;br /&gt;    end&lt;/p&gt;

&lt;p&gt;  private
&lt;br /&gt;    def initialize1( parameters_array )
&lt;br /&gt;      # construct in one way
&lt;br /&gt;    end&lt;/p&gt;

&lt;p&gt;    def initialize2( parameters_array )
&lt;br /&gt;      # construct in another way
&lt;br /&gt;    end&lt;/p&gt;

&lt;p&gt;  end
&lt;br /&gt;&lt;/pre&gt;
&lt;br /&gt;&lt;p&gt;
&lt;br /&gt;It would work better through a mixin that metaclass-hacks to allow for easily flagging a method "initializer", though.)
&lt;br /&gt;&lt;h2&gt;Wonder of the When-be-splat&lt;/h2&gt;
&lt;br /&gt;&lt;p&gt;
&lt;br /&gt;On &lt;a href="http://www.google.com/url?sa=D&amp;q=http%3A%2F%2Fredhanded.hobix.com%2Fbits%2FwonderOfTheWhenBeFlat.html"&gt;[redhanded]&lt;/a&gt;  _Why pointed out that * (the splat) can be used to unpack an array for a branch of a case statement.
&lt;br /&gt;&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;pre&gt;
&lt;br /&gt;  BOARD_MEMBERS = ['Jan', 'Julie', 'Archie', 'Stewick']
&lt;br /&gt;  HISTORIANS = ['Braith', 'Dewey', 'Eduardo']
&lt;br /&gt;&lt;/pre&gt;&lt;p&gt;
&lt;br /&gt;&lt;pre&gt;
&lt;br /&gt;  case name
&lt;br /&gt;  when *BOARD_MEMBERS
&lt;br /&gt;   "You're on the board!  A congratulations is in order." 
&lt;br /&gt;  when *HISTORIANS
&lt;br /&gt;   "You are busy chronicling every deft play." 
&lt;br /&gt;  end
&lt;br /&gt;&lt;/pre&gt;
&lt;/p&gt;
&lt;p&gt;Tags: published ruby&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/AngelbobsBlog/~4/bxbMwAyM4MI" height="1" width="1"/&gt;</content>
    <author>
      <name>Noah Gibbs</name>
    </author>
  <feedburner:origLink>http://blog.angelbob.com/posts/244-Repost-from-long-gone-RubyGarden-List-of-Ruby-Idioms---published-ruby</feedburner:origLink></entry>
  <entry>
    <id>tag:blog.angelbob.com,2005:Post/243</id>
    <published>2009-11-04T23:22:09Z</published>
    <updated>2009-11-04T23:22:09Z</updated>
    <link type="text/html" href="http://feedproxy.google.com/~r/AngelbobsBlog/~3/3aKx63jRiqw/243-Time-to-Slow-Down---blog-published" rel="alternate" />
    <title>Time to Slow Down</title>
    <content type="html">&lt;p&gt;I've had great luck with &lt;a href="/posts/11"&gt;six jabs a week&lt;/a&gt; since April.  In fact, I've posted that way about 200 times, which is hard to complain about, though many of those posts are tiny things.&lt;/p&gt;

&lt;p&gt;But I've recently started a new job, and it's time to slow down for a bit.&lt;/p&gt;

&lt;p&gt;So expect around three a week, and one or more of them may be stuff I'm doing at the job.&lt;/p&gt;

&lt;p&gt;I'm not planning to stop, just slow down a fair bit.  And when I've got my equilibrium back and I'm not feverishly learning stuff for the job, the rate will likely increase again.
&lt;/p&gt;
&lt;p&gt;Tags: blog published&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/AngelbobsBlog/~4/3aKx63jRiqw" height="1" width="1"/&gt;</content>
    <author>
      <name>Noah Gibbs</name>
    </author>
  <feedburner:origLink>http://blog.angelbob.com/posts/243-Time-to-Slow-Down---blog-published</feedburner:origLink></entry>
  <entry>
    <id>tag:blog.angelbob.com,2005:Post/240</id>
    <published>2009-11-02T00:49:14Z</published>
    <updated>2009-11-02T00:49:14Z</updated>
    <link type="text/html" href="http://feedproxy.google.com/~r/AngelbobsBlog/~3/9NJoMbnxva0/240-Professional-Rails-database-files---published-rails" rel="alternate" />
    <title>Professional: Rails database files</title>
    <content type="html">&lt;p&gt;A neat thing about Rails is that you're not limited to the three built-in environments (test, development, production).  You can create your own and write configuration files for them, and everything just works as you'd hope it would.&lt;/p&gt;

&lt;p&gt;You *do* have to write the configuration files for them, but that's a pretty small price to pay.&lt;/p&gt;

&lt;p&gt;Luckily, &lt;a href="http://blog.lathi.net/articles/2006/03/02/config-database-yml-goodness-for-teams"&gt;database.yml supports some excellent nifty syntaxes&lt;/a&gt; that probably aren't obvious from the default file contents.
&lt;/p&gt;
&lt;p&gt;Tags: published rails&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/AngelbobsBlog/~4/9NJoMbnxva0" height="1" width="1"/&gt;</content>
    <author>
      <name>Noah Gibbs</name>
    </author>
  <feedburner:origLink>http://blog.angelbob.com/posts/240-Professional-Rails-database-files---published-rails</feedburner:origLink></entry>
</feed>
