<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<feed xmlns="http://www.w3.org/2005/Atom">

	<title>Planet Merb</title>
	<link rel="self" href="http://planet.merbivore.com/atom.xml"/>
	<link href="http://planet.merbivore.com/"/>
	<id>http://planet.merbivore.com/atom.xml</id>
	<updated>2010-03-13T12:20:29+00:00</updated>
	<generator uri="http://www.planetplanet.org/">Planet/2.0 +http://www.planetplanet.org</generator>

	<entry>
		<title type="html">MerbAdmin Installation and Overview</title>
		<link href="http://merbunity.com/screencasts/4"/>
		<id>http://merbunity.com/screencasts/4</id>
		<updated>2010-03-13T07:34:32+00:00</updated>
		<content type="html">&lt;div&gt;
        &lt;p&gt;Erik Michaels-Ober demonstrates the installation process and some of the functionality of MerbAdmin.&lt;/p&gt;
        &lt;br /&gt;
        &lt;a href=&quot;http://merbunity.com/screencasts/4&quot;&gt;Watch it...&lt;/a&gt;
      &lt;/div&gt;</content>
		<author>
			<name>sferik</name>
			<uri></uri>
		</author>
		<source>
			<title type="html">Merbunity - Recent Content Feed</title>
			<subtitle type="html">Recent Content from the Merb Community</subtitle>
			<link rel="self" href="http://feeds.feedburner.com/Merbunity-Recent"/>
			<id>http://merbunity.com/welcome/index.atom</id>
			<updated>2010-03-13T08:20:03+00:00</updated>
		</source>
	</entry>

	<entry>
		<title type="html">Learn About Merb Dependencies and the new Bundler!</title>
		<link href="http://merbunity.com/screencasts/2"/>
		<id>http://merbunity.com/screencasts/2</id>
		<updated>2010-03-12T22:01:14+00:00</updated>
		<content type="html">&lt;div&gt;
        &lt;p&gt;Carl Lerch and Yehuda Katz released this helpful screencast with Merb 1.0.6. It deals with how to utilize all the new features the new gem dependency bundler brings.&lt;/p&gt;
        &lt;br /&gt;
        &lt;a href=&quot;http://merbunity.com/screencasts/2&quot;&gt;Watch it...&lt;/a&gt;
      &lt;/div&gt;</content>
		<author>
			<name>wifelette</name>
			<uri></uri>
		</author>
		<source>
			<title type="html">Merbunity - Recent Content Feed</title>
			<subtitle type="html">Recent Content from the Merb Community</subtitle>
			<link rel="self" href="http://feeds.feedburner.com/Merbunity-Recent"/>
			<id>http://merbunity.com/welcome/index.atom</id>
			<updated>2010-03-13T08:20:03+00:00</updated>
		</source>
	</entry>

	<entry xml:lang="en">
		<title type="html">Ruby&amp;#8217;s Implementation Does Not Define its Semantics</title>
		<link href="http://feedproxy.google.com/~r/KatzGotYourTongue/~3/0LSTN7rHg_o/"/>
		<id>http://yehudakatz.com/?p=443</id>
		<updated>2010-02-25T06:22:34+00:00</updated>
		<content type="html">&lt;p&gt;When I was first getting started with Ruby, I heard a lot of talk about blocks, and how you could &amp;#8220;cast&amp;#8221; them to Procs by using the &amp;#038; operator when calling methods. Last week, in comments about my last post (&lt;a href=&quot;http://yehudakatz.com/2010/02/21/ruby-is-not-a-callable-oriented-language/&quot;&gt;Ruby is NOT a Callable Oriented Language (It’s Object Oriented)&lt;/a&gt;), I heard that claim again.&lt;/p&gt;
&lt;p&gt;To be honest, I never really thought that much about it, and the idea of &amp;#8220;casting&amp;#8221; a block to a Proc never took hold in my mental model, but when discussing my post with a number of people, I realized that a lot of people have this concept in their mental model of Ruby.&lt;/p&gt;
&lt;p&gt;It is not part of Ruby&amp;#8217;s semantics.&lt;/p&gt;
&lt;p&gt;In some cases, Ruby&amp;#8217;s internal implementation performs optimizations by eliminating the creation of objects until you specifically ask for them. Those optimizations are completely invisible, and again, not part of Ruby&amp;#8217;s semantics.&lt;/p&gt;
&lt;p&gt;Let&amp;#8217;s look at a few examples.&lt;/p&gt;
&lt;h2&gt;Blocks vs. Procs&lt;/h2&gt;
&lt;p&gt;Consider the following scenarios:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;def&lt;/span&gt; foo
  &lt;span&gt;yield&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
&lt;span&gt;def&lt;/span&gt; bar&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;block&lt;span&gt;&amp;#41;&lt;/span&gt;
  &lt;span&gt;puts&lt;/span&gt; block.&lt;span&gt;object_id&lt;/span&gt;
  baz&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;block&lt;span&gt;&amp;#41;&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
&lt;span&gt;def&lt;/span&gt; baz&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;block&lt;span&gt;&amp;#41;&lt;/span&gt;
  &lt;span&gt;puts&lt;/span&gt; block.&lt;span&gt;object_id&lt;/span&gt;
  &lt;span&gt;yield&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
foo &lt;span&gt;&amp;#123;&lt;/span&gt; &lt;span&gt;puts&lt;/span&gt; &lt;span&gt;&amp;quot;HELLO&amp;quot;&lt;/span&gt; &lt;span&gt;&amp;#125;&lt;/span&gt; &lt;span&gt;#=&amp;gt; &amp;quot;HELLO&amp;quot;&lt;/span&gt;
bar &lt;span&gt;&amp;#123;&lt;/span&gt; &lt;span&gt;puts&lt;/span&gt; &lt;span&gt;&amp;quot;HELLO&amp;quot;&lt;/span&gt; &lt;span&gt;&amp;#125;&lt;/span&gt; &lt;span&gt;#=&amp;gt; &amp;quot;2148083200\n2148083200\nHELLO&amp;quot;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Here, I have three methods using blocks in different ways. In the first method (&lt;code&gt;foo&lt;/code&gt;), I don&amp;#8217;t specify the block at all, yielding to the implicit block. In the second case, I specify a block parameter, print its &lt;code&gt;object_id&lt;/code&gt;, and then send it on to the &lt;code&gt;baz&lt;/code&gt; method. In the third case, I specify the block, print out its &lt;code&gt;object_id&lt;/code&gt; and yield to the block.&lt;/p&gt;
&lt;p&gt;In Ruby&amp;#8217;s semantics, these three uses are identical. In the first case, yield calls an implicit Proc object. In the second case, it takes an explicit Proc, then sends it on to the next method. In the last case, it takes an explicit Proc, but yields to the implicit copy of the same object.&lt;/p&gt;
&lt;h3&gt;So what&amp;#8217;s the &lt;code&gt;&amp;amp;&lt;/code&gt; thing for?&lt;/h3&gt;
&lt;p&gt;In Ruby, in addition to normal arguments, methods can receive a Proc in a special slot. All methods can receive such an argument, and Procs passed in that slot are silently ignored if not yielded to:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;def&lt;/span&gt; foo
  &lt;span&gt;puts&lt;/span&gt; &lt;span&gt;&amp;quot;HELLO&amp;quot;&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
foo &lt;span&gt;&amp;#123;&lt;/span&gt; something_crazy &lt;span&gt;&amp;#125;&lt;/span&gt; &lt;span&gt;#=&amp;gt; &amp;quot;HELLO&amp;quot;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;On the other hand, if you want a method to receive a Proc in that slot, and thus be able to yield to it, you specify that by prefixing it with an &lt;code&gt;&amp;amp;&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;def&lt;/span&gt; foo
  &lt;span&gt;yield&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
my_proc = &lt;span&gt;Proc&lt;/span&gt;.&lt;span&gt;new&lt;/span&gt; &lt;span&gt;&amp;#123;&lt;/span&gt; &lt;span&gt;puts&lt;/span&gt; &lt;span&gt;&amp;quot;HELLO&amp;quot;&lt;/span&gt; &lt;span&gt;&amp;#125;&lt;/span&gt;
foo&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;my_proc&lt;span&gt;&amp;#41;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Here, you&amp;#8217;re telling the &lt;code&gt;foo&lt;/code&gt; method that &lt;code&gt;my_proc&lt;/code&gt; is not a normal argument; it should be placed into the proc slot and made available to yield.&lt;/p&gt;
&lt;p&gt;Additionally, if you want access to the &lt;code&gt;Proc&lt;/code&gt; object, you can give it a name:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;def&lt;/span&gt; foo&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;block&lt;span&gt;&amp;#41;&lt;/span&gt;
  &lt;span&gt;puts&lt;/span&gt; block.&lt;span&gt;object_id&lt;/span&gt;
  &lt;span&gt;yield&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
foo &lt;span&gt;&amp;#123;&lt;/span&gt; &lt;span&gt;puts&lt;/span&gt; &lt;span&gt;&amp;quot;HELLO&amp;quot;&lt;/span&gt; &lt;span&gt;&amp;#125;&lt;/span&gt; &lt;span&gt;#=&amp;gt; &amp;quot;2148084320\nHELLO&amp;quot;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This simply means that you want access to the implicit &lt;code&gt;Proc&lt;/code&gt; in a variable named &lt;code&gt;block&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Because, in most cases, you&amp;#8217;re passing in a block (using &lt;code&gt;do/end&lt;/code&gt; or &lt;code&gt;{}&lt;/code&gt;), and calling it using &lt;code&gt;yield&lt;/code&gt;, Ruby provides some syntax sugar to make that simple case more pleasing. That does not, however, mean that there is a special &lt;code&gt;block&lt;/code&gt; construct in Ruby&amp;#8217;s semantics, nor does it mean that the &lt;code&gt;&amp;amp;&lt;/code&gt; is &lt;em&gt;casting&lt;/em&gt; the a block to a Proc.&lt;/p&gt;
&lt;p&gt;You can tell that blocks are not being &lt;strong&gt;semantically&lt;/strong&gt; wrapped and unwrapped because blocks passed along via &lt;code&gt;&amp;amp;&lt;/code&gt; share the same &lt;code&gt;object_id&lt;/code&gt; across methods.&lt;/p&gt;
&lt;h3&gt;Mental Models&lt;/h3&gt;
&lt;p&gt;For the following code there are two possible mental models.&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;def&lt;/span&gt; foo&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;block&lt;span&gt;&amp;#41;&lt;/span&gt;
  &lt;span&gt;puts&lt;/span&gt; block.&lt;span&gt;object_id&lt;/span&gt;
  &lt;span&gt;yield&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
b = &lt;span&gt;Proc&lt;/span&gt;.&lt;span&gt;new&lt;/span&gt; &lt;span&gt;&amp;#123;&lt;/span&gt; &lt;span&gt;puts&lt;/span&gt; &lt;span&gt;&amp;quot;OMG&amp;quot;&lt;/span&gt; &lt;span&gt;&amp;#125;&lt;/span&gt;
&lt;span&gt;puts&lt;/span&gt; b.&lt;span&gt;object_id&lt;/span&gt;
foo&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;b&lt;span&gt;&amp;#41;&lt;/span&gt; &lt;span&gt;#=&amp;gt; 2148084040\n2148084040\nOMG&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In the first, the &lt;code&gt;&amp;amp;b&lt;/code&gt; unwraps the Proc object, and the &lt;code&gt;&amp;amp;block&lt;/code&gt; recasts it into a Proc. However, it somehow also wraps it back into the same wrapper that it came from into the first place.&lt;/p&gt;
&lt;p&gt;In the second, the &lt;code&gt;&amp;amp;b&lt;/code&gt; puts the &lt;code&gt;b&lt;/code&gt; Proc into the block slot in &lt;code&gt;foo&lt;/code&gt;&amp;#8217;s argument list, and the &lt;code&gt;&amp;amp;block&lt;/code&gt; gives the implicit Proc a name. There is no need to explain why the Proc has the same &lt;code&gt;object_id&lt;/code&gt;; it is the same Object!&lt;/p&gt;
&lt;p&gt;These two mental models are perfectly valid (the first actually reflects Ruby&amp;#8217;s internal implementation). I claim that those who want to use the first mental model have the heavy burden of introducing the new concept to the Ruby language of a non-object block, and that as a result, it should be generally rejected.&lt;/p&gt;
&lt;h2&gt;Metaclasses&lt;/h2&gt;
&lt;p&gt;Similarly, in Ruby&amp;#8217;s internal implementation, an Object does not get a metaclass until you ask for one.&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;obj = &lt;span&gt;Object&lt;/span&gt;.&lt;span&gt;new&lt;/span&gt;
&lt;span&gt;# internally, obj does not have a metaclass here&lt;/span&gt;
&amp;nbsp;
obj.&lt;span&gt;to_s&lt;/span&gt;
&lt;span&gt;# internally, Ruby skips right up to Object when searching for #to_s, since&lt;/span&gt;
&lt;span&gt;# it knows that no metaclass exists&lt;/span&gt;
&amp;nbsp;
&lt;span&gt;def&lt;/span&gt; obj.&lt;span&gt;hello&lt;/span&gt;
  &lt;span&gt;puts&lt;/span&gt; &lt;span&gt;&amp;quot;HELLO&amp;quot;&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;
&lt;span&gt;# now, Ruby internally rewrites obj's class pointer to point to a new internal&lt;/span&gt;
&lt;span&gt;# metaclass which has the hello method on it&lt;/span&gt;
&amp;nbsp;
obj.&lt;span&gt;to_s&lt;/span&gt;
&lt;span&gt;# Now, Ruby searches obj's metaclass before jumping up to Object&lt;/span&gt;
&amp;nbsp;
obj.&lt;span&gt;to_s&lt;/span&gt;
&lt;span&gt;# Now, Ruby skips the search because it's already cached the method&lt;/span&gt;
&lt;span&gt;# lookup&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;All of the comments in the above snippet are correct, but semantically, none of them are important. In all cases, Ruby is &lt;strong&gt;semantically&lt;/strong&gt; looking for methods in &lt;code&gt;obj&lt;/code&gt;&amp;#8217;s metaclass, and when it doesn&amp;#8217;t find any, it searches higher up. In order to improve performance, Ruby skips creating a metaclass if no methods or modules are added to an object&amp;#8217;s metaclass, but that doesn&amp;#8217;t change Ruby&amp;#8217;s semantics. It&amp;#8217;s just an optimization.&lt;/p&gt;
&lt;p&gt;By thinking in terms of Ruby&amp;#8217;s implementation, instead of Ruby&amp;#8217;s semantics, you are forced to think about a mutable class pointer and consider the possibility that an object has no metaclass.&lt;/p&gt;
&lt;h3&gt;Mental Models&lt;/h3&gt;
&lt;p&gt;Again, there are two possible mental models. In the first, Ruby objects have a class pointer, which they manipulate to point to new metaclass objects which are created only when methods or modules are added to an object. Additionally, Ruby objects have a method cache, which they use to store method lookups. When a method is looked up twice, in this mental model, some classes are skipped because Ruby already knows that they don&amp;#8217;t have the method.&lt;/p&gt;
&lt;p&gt;In the second mental model, all Ruby objects have a metaclass, and method lookup always goes through the metaclass and up the superclass chain until a method is found.&lt;/p&gt;
&lt;p&gt;As before, I claim that those who want to impose the first mental model on Ruby programmers have the heavy burden of introducing the new concepts of &amp;#8220;class pointer&amp;#8221; and &amp;#8220;method cache&amp;#8221;, which are not Ruby objects and have no visible implications on Ruby semantics.&lt;/p&gt;
&lt;h2&gt;Regular Expression Matches&lt;/h2&gt;
&lt;p&gt;In Ruby, certain regular expression operations create implicit local variables that reflect parts of the match:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;def&lt;/span&gt; foo&lt;span&gt;&amp;#40;&lt;/span&gt;str&lt;span&gt;&amp;#41;&lt;/span&gt;
  str =~ &lt;span&gt;/&lt;/span&gt;e&lt;span&gt;&amp;#40;&lt;/span&gt;.&lt;span&gt;&amp;#41;&lt;/span&gt;l&lt;span&gt;/&lt;/span&gt;
  &lt;span&gt;p&lt;/span&gt; $~
  &lt;span&gt;p&lt;/span&gt; $&lt;span&gt;`
  p $'
  p $1
  p $2
end
&amp;nbsp;
foo(&amp;quot;hello&amp;quot;) #=&amp;gt; #&amp;lt;MatchData &amp;quot;ell&amp;quot; 1:&amp;quot;l&amp;quot;&amp;gt;&lt;span&gt;\n&lt;/span&gt;&amp;quot;h&amp;quot;&lt;span&gt;\n&lt;/span&gt;&amp;quot;o&amp;quot;&lt;span&gt;\n&lt;/span&gt;&amp;quot;l&amp;quot;&lt;span&gt;\n&lt;/span&gt;nil&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This behavior is mostly inherited from Perl, and Matz has said a few times that he would not support Perl&amp;#8217;s backrefs if he had it to do over again. However, the provide another opportune example of implicit objects in Ruby.&lt;/p&gt;
&lt;h3&gt;Mental Models&lt;/h3&gt;
&lt;p&gt;In this case, there are three possible mental models.&lt;/p&gt;
&lt;p&gt;In the first mental model, if you don&amp;#8217;t use any &lt;code&gt;$&lt;/code&gt; local variables, they don&amp;#8217;t exist. When you use a specific one, it springs into existence. For instance, when using &lt;code&gt;$1&lt;/code&gt;, Ruby looks at some internal representation of the last match and retrieves the last capture. If you use &lt;code&gt;$~&lt;/code&gt;, Ruby creates a MatchData object out of it.&lt;/p&gt;
&lt;p&gt;In the second mental model, when you call a method that uses regular expressions, Ruby walks back up the stack frames, and inserts the &lt;code&gt;$&lt;/code&gt; local variables on it when it finds the original caller. If you later use the variables, they are already there. Ruby must be a little bit clever, because the most recent frame on the stack (which might include C frames, Java frames, or internal Ruby frames in Rubinius) is not always the calling frame.&lt;/p&gt;
&lt;p&gt;In the last mental model, when you call a method that uses regular expressions, there is an implicit match object available (similar to the implicit Proc object that is available in methods). The &lt;code&gt;$~&lt;/code&gt; variable is mapped to that implicit object, while the &lt;code&gt;$1&lt;/code&gt; variable is the equivalent of &lt;code&gt;$~[1]&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Again, this last mental model introduces the least burdensome ideas into Ruby. The first mental model introduces the idea of an internal representation of the last match, while the second mental model (which again, has the upside of being how most implementations actually do it) introduces the concept of stack frames, which are not Ruby objects.&lt;/p&gt;
&lt;p&gt;The last mental model uses an actual Ruby object, and does not introduce new concepts. Again, I prefer it.&lt;/p&gt;
&lt;h2&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;In a number of places, it is possible to imbue Ruby semantics with mental models that reflect the actual Ruby implementation, or the fact that it&amp;#8217;s possible to imagine that a Ruby object only springs into existence when it is asked for. &lt;/p&gt;
&lt;p&gt;However, these mental models require that Ruby programmers add non-objects to the semantics of Ruby, and requiring contortions to explain away Ruby&amp;#8217;s own efforts to hide these internals from the higher-level constructs of the language. For instance, while Ruby internally wraps and unwraps Procs when passing them to methods, it makes sure that the Proc object attached to a block is always the same, in an effort to hide the internal details from programmers.&lt;/p&gt;
&lt;p&gt;As a result, explaining Ruby&amp;#8217;s semantics in terms of these internals requires contortions and new constructs that are not natively part of Ruby&amp;#8217;s object model, and those explanations should be avoided.&lt;/p&gt;
&lt;p&gt;To be clear, I am not arguing that it&amp;#8217;s not useful to understand Ruby&amp;#8217;s implementation. It can, in fact, be quite useful, just as it&amp;#8217;s useful to understand how C&amp;#8217;s calling convention works under the covers. However, just as day-to-day programmers in C don&amp;#8217;t need to think about the emitted Assembler, day-to-day Ruby programmers don&amp;#8217;t need to think about the implementation. And finally, just as C implementations are free to use different calling conventions without breaking existing processor-agnostic C (or the mental model that C programmers use), Ruby implementations are free to change the internal implementation of these constructs without breaking pure-Ruby code or the mental model Ruby programmers use.&lt;/p&gt;

&lt;div class=&quot;feedflare&quot;&gt;
&lt;a href=&quot;http://feeds.feedburner.com/~ff/KatzGotYourTongue?a=0LSTN7rHg_o:fcqLqZHYiDc:yIl2AUoC8zA&quot;&gt;&lt;img src=&quot;http://feeds.feedburner.com/~ff/KatzGotYourTongue?d=yIl2AUoC8zA&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src=&quot;http://feeds.feedburner.com/~r/KatzGotYourTongue/~4/0LSTN7rHg_o&quot; height=&quot;1&quot; width=&quot;1&quot; /&gt;</content>
		<author>
			<name>wycats</name>
			<uri>http://yehudakatz.com</uri>
		</author>
		<source>
			<title type="html">Katz Got Your Tongue?</title>
			<subtitle type="html">Random Geek-Related Thoughts</subtitle>
			<link rel="self" href="http://yehudakatz.com/feed/atom/"/>
			<id>http://yehudakatz.com/feed/atom/</id>
			<updated>2010-03-13T08:20:07+00:00</updated>
		</source>
	</entry>

	<entry xml:lang="en">
		<title type="html">Ruby is NOT a Callable Oriented Language (It&amp;#8217;s Object Oriented)</title>
		<link href="http://feedproxy.google.com/~r/KatzGotYourTongue/~3/8IhrIYNVOSc/"/>
		<id>http://yehudakatz.com/?p=439</id>
		<updated>2010-02-21T23:19:49+00:00</updated>
		<content type="html">&lt;p&gt;I recently ran across a presentation entitled &lt;a href=&quot;http://blog.extracheese.org/2010/02/python-vs-ruby-a-battle-to-the-death.html&quot;&gt;Python vs. Ruby: A Battle to the Death&lt;/a&gt;. I didn&amp;#8217;t consider it to be a particularly fair battle, and may well reply in more detail in a later post.&lt;/p&gt;
&lt;p&gt;However, what struck me as most worthy of explanation was the presenter&amp;#8217;s concern about the fact that Procs are not callable via parens.&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;x = &lt;span&gt;Proc&lt;/span&gt;.&lt;span&gt;new&lt;/span&gt; &lt;span&gt;&amp;#123;&lt;/span&gt; &lt;span&gt;puts&lt;/span&gt; &lt;span&gt;&amp;quot;HELLO&amp;quot;&lt;/span&gt; &lt;span&gt;&amp;#125;&lt;/span&gt;
x&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt; &lt;span&gt;#=&amp;gt; undefined method `x' for #&amp;lt;Object:0x1001bd298&amp;gt;&amp;lt;&lt;/span&gt;
x.&lt;span&gt;call&lt;/span&gt; &lt;span&gt;#=&amp;gt; &amp;quot;HELLO&amp;quot;&lt;/span&gt;
x&lt;span&gt;&amp;#91;&lt;/span&gt;&lt;span&gt;&amp;#93;&lt;/span&gt;    &lt;span&gt;#=&amp;gt; &amp;quot;HELLO&amp;quot;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;For those coming from a callable-oriented language, like Python, this seems horribly inconsistent. Why are methods called with &lt;code&gt;()&lt;/code&gt;, while Procs are called with &lt;code&gt;[]&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;But what&amp;#8217;s going on here is that Ruby doesn&amp;#8217;t have a notion of &amp;#8220;callable&amp;#8221;, like Python. Instead, it has a pervasive notion of &lt;strong&gt;Object&lt;/strong&gt;. Here, &lt;code&gt;x&lt;/code&gt; is an instance of the Proc class. Both &lt;code&gt;call&lt;/code&gt; and &lt;code&gt;[]&lt;/code&gt; are methods on the Proc class.&lt;/p&gt;
&lt;h2&gt;Designing for the Common Case: Calling Methods&lt;/h2&gt;
&lt;p&gt;Coming from a callable-oriented language, this might seem jarring. But Ruby is designed around Objects and the common cases of working with objects.&lt;/p&gt;
&lt;p&gt;Calling methods is far more common than wanting to get an instance of Method, so Ruby optimizes the case of calling methods, with a slightly less elegant form to access an instance:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;class&lt;/span&gt; Greeter
  &lt;span&gt;def&lt;/span&gt; say
    &lt;span&gt;puts&lt;/span&gt; &lt;span&gt;&amp;quot;Hello world!&amp;quot;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
Greeter.&lt;span&gt;new&lt;/span&gt;.&lt;span&gt;say&lt;/span&gt; &lt;span&gt;#=&amp;gt; &amp;quot;Hello world!&amp;quot;&lt;/span&gt;
&amp;nbsp;
Greeter.&lt;span&gt;method&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;:new&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt; &lt;span&gt;#=&amp;gt; #&amp;lt;Method: Class#new&amp;gt;&lt;/span&gt;
Greeter.&lt;span&gt;new&lt;/span&gt;.&lt;span&gt;method&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;:say&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt; &lt;span&gt;#=&amp;gt; #&amp;lt;Method: Greeter#say&amp;gt;&lt;/span&gt;
&amp;nbsp;
&lt;span&gt;# This is so that you don't have to say:&lt;/span&gt;
&amp;nbsp;
Greeter.&lt;span&gt;new&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;.&lt;span&gt;say&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Ruby considers the common case of calling methods, and optimizes that case while still making the less common case possible.&lt;/p&gt;
&lt;h2&gt;Designing for the Common Case: How Blocks Are &lt;strong&gt;Really&lt;/strong&gt; Used&lt;/h2&gt;
&lt;p&gt;One of the reasons that the &lt;code&gt;Proc.new&lt;/code&gt; case throws off Rubyists in debates is that Rubyists literally never call &lt;code&gt;Proc&lt;/code&gt; objects using &lt;code&gt;[]&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;In Ruby, Procs are the object passed to methods when using block syntax. Here is how &lt;code&gt;Proc&lt;/code&gt;s are &lt;strong&gt;actually&lt;/strong&gt; used:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;def&lt;/span&gt; hello
  &lt;span&gt;puts&lt;/span&gt; &lt;span&gt;&amp;quot;Hello world!&amp;quot;&lt;/span&gt;
  &lt;span&gt;yield&lt;/span&gt;
  &lt;span&gt;puts&lt;/span&gt; &lt;span&gt;&amp;quot;Goodbye cruel world!&amp;quot;&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
hello &lt;span&gt;&amp;#123;&lt;/span&gt; &lt;span&gt;puts&lt;/span&gt; &lt;span&gt;&amp;quot;I am in the world!&amp;quot;&lt;/span&gt; &lt;span&gt;&amp;#125;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;When examining languages that support passing anonymous functions to functions (like JavaScript), it turns out that the vast majority of such cases involve a single anonymous function. As a result, Matz (inspired by Smalltalk) built in the idea of a block as a core construct in Ruby. In the vast majority of cases, blocks are created using lightweight syntax (&lt;code&gt;{}&lt;/code&gt; or &lt;code&gt;do/end&lt;/code&gt;) and called using &lt;code&gt;yield&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;In some cases, blocks are passed from one method to the next, before they are finally called using yield:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;def&lt;/span&gt; step1&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;block&lt;span&gt;&amp;#41;&lt;/span&gt;
  &lt;span&gt;puts&lt;/span&gt; &lt;span&gt;&amp;quot;Step 1&amp;quot;&lt;/span&gt;
  step2&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;block&lt;span&gt;&amp;#41;&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
&lt;span&gt;def&lt;/span&gt; step2
  &lt;span&gt;puts&lt;/span&gt; &lt;span&gt;&amp;quot;Step 2&amp;quot;&lt;/span&gt;
  &lt;span&gt;yield&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
step1 &lt;span&gt;&amp;#123;&lt;/span&gt; &lt;span&gt;puts&lt;/span&gt; &lt;span&gt;&amp;quot;Do the action!&amp;quot;&lt;/span&gt; &lt;span&gt;&amp;#125;&lt;/span&gt; &lt;span&gt;#=&amp;gt; &amp;quot;Step 1\nStep 2\nDo the action!&amp;quot;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;As you can see, Ruby builds in the idea of calling a block into the core language. I searched through Rails (a fairly large codebase) for instances of using &lt;code&gt;[]&lt;/code&gt; to call a Proc and while we use blocks &lt;strong&gt;extremely&lt;/strong&gt; commonly, we don&amp;#8217;t use &lt;code&gt;[]&lt;/code&gt; to call them.&lt;/p&gt;
&lt;p&gt;I suspect that the reason this comes up is that people who are used to having to define standalone functions, pass them around, and then call them are looking for the analogous constructs in Ruby, but are missing the different paradigm used by Ruby.&lt;/p&gt;
&lt;h2&gt;Consistent Method Execution&lt;/h2&gt;
&lt;p&gt;Fundamentally, the issue here comes down to this:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;def&lt;/span&gt; foo
  &lt;span&gt;proc&lt;/span&gt; &lt;span&gt;&amp;#123;&lt;/span&gt;&lt;span&gt;&amp;#125;&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
foo&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In Ruby, methods are invoked with our without parentheses. All methods return values, which are always Objects. All Objects have methods. So &lt;code&gt;foo()&lt;/code&gt; is a method call that returns a Proc object. It&amp;#8217;s extremely consistent, with very few axioms. The fact that the axioms aren&amp;#8217;t the same as those in a callable-oriented language doesn&amp;#8217;t make them &amp;#8220;weird&amp;#8221;.&lt;/p&gt;

&lt;div class=&quot;feedflare&quot;&gt;
&lt;a href=&quot;http://feeds.feedburner.com/~ff/KatzGotYourTongue?a=8IhrIYNVOSc:lsodJHfdpQE:yIl2AUoC8zA&quot;&gt;&lt;img src=&quot;http://feeds.feedburner.com/~ff/KatzGotYourTongue?d=yIl2AUoC8zA&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src=&quot;http://feeds.feedburner.com/~r/KatzGotYourTongue/~4/8IhrIYNVOSc&quot; height=&quot;1&quot; width=&quot;1&quot; /&gt;</content>
		<author>
			<name>wycats</name>
			<uri>http://yehudakatz.com</uri>
		</author>
		<source>
			<title type="html">Katz Got Your Tongue?</title>
			<subtitle type="html">Random Geek-Related Thoughts</subtitle>
			<link rel="self" href="http://yehudakatz.com/feed/atom/"/>
			<id>http://yehudakatz.com/feed/atom/</id>
			<updated>2010-03-13T08:20:07+00:00</updated>
		</source>
	</entry>

	<entry xml:lang="en">
		<title type="html">AbstractQueryFactoryFactories and alias_method_chain: The Ruby Way</title>
		<link href="http://feedproxy.google.com/~r/KatzGotYourTongue/~3/Z_Mze9ZSYoQ/"/>
		<id>http://yehudakatz.com/?p=437</id>
		<updated>2010-02-15T12:58:41+00:00</updated>
		<content type="html">&lt;p&gt;In the past week, I read a couple of posts that made me really want to respond with a coherent explanation of how I build modular Ruby code.&lt;/p&gt;
&lt;p&gt;The first post, &lt;a href=&quot;http://magicscalingsprinkles.wordpress.com/2010/02/08/why-i-love-everything-you-hate-about-java/&quot;&gt;by Nick Kallen of Twitter&lt;/a&gt;, gushed about the benefits of &lt;code&gt;PerQueryTimingOutQueryFactory&lt;/code&gt; and called out Ruby (and a slew of other &amp;#8220;hipster&amp;#8221; languages) for using language features (like my &amp;#8220;favorite&amp;#8221; &lt;code&gt;alias_method_chain&lt;/code&gt;) and leveraging dynamicism to solve problems that he argues are more appropriately solved with laugh-inducing pattern names:&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;In a very dynamic language like Ruby, open classes and method aliasing (e.g., alias_method_chain) mitigate this problem, but they don’t solve it. If you manipulate a class to add logging, all instances of that class will have logging; you can’t take a surgical approach and say “just objects instantiated in this context”.&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;If you haven&amp;#8217;t read it yet, you should probably read it now (at least skim it).&lt;/p&gt;
&lt;p&gt;As if on cue, &lt;a href=&quot;http://pivotallabs.com/users/rolson/blog/articles/1162-redefine-a-method-from-a-module-like-a-gentleman&quot;&gt;a post by Pivot Rob Olson&lt;/a&gt; demonstrated the lengths some Rubyists will go to torture alias_method_chain to solve essentially the same problem that Nick addressed.&lt;/p&gt;
&lt;p&gt;In short, while I agree in principle with Nick, his examples and the jargon he used demonstrated exactly why so few Rubyists take his point seriously. It is possible to write modular code in Ruby with the same level of flexibility but with far less code and fewer concept hoops to jump through.&lt;/p&gt;
&lt;p&gt;Let&amp;#8217;s take a look at the problem Rob was trying to solve:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;module&lt;/span&gt; Teacher
  &lt;span&gt;def&lt;/span&gt; initialize
    &lt;span&gt;puts&lt;/span&gt; &lt;span&gt;&amp;quot;initializing teacher&amp;quot;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
&lt;span&gt;class&lt;/span&gt; Person
  &lt;span&gt;include&lt;/span&gt; Teacher
&amp;nbsp;
  &lt;span&gt;def&lt;/span&gt; initialize
    &lt;span&gt;puts&lt;/span&gt; &lt;span&gt;&amp;quot;initializing person&amp;quot;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
&lt;span&gt;# Desired output:&lt;/span&gt;
&lt;span&gt;# &amp;gt; Person.new&lt;/span&gt;
&lt;span&gt;# initializing teacher&lt;/span&gt;
&lt;span&gt;# initializing person&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This is a classic problem involving modularity. In essence, Rob wants to be able to &amp;#8220;decorate&amp;#8221; the Person class to include teacher traits.&lt;/p&gt;
&lt;p&gt;Nick&amp;#8217;s response would have been to create a &lt;strong&gt;factory&lt;/strong&gt; that creates a Person &lt;strong&gt;proxy&lt;/strong&gt; &lt;strong&gt;decorated&lt;/strong&gt; with Teacher properties. And he would have been technically correct, but that description obscures the Ruby implementation, and makes it sound like we need new &amp;#8220;Factory&amp;#8221; and &amp;#8220;Decorator&amp;#8221; objects, as we do, in fact, need when programming in Java.&lt;/p&gt;
&lt;p&gt;In Ruby, you&amp;#8217;d solve this problem thusly:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;# The base person implementation. Never instantiate this.&lt;/span&gt;
&lt;span&gt;# Instead, create a subclass that mixes in appropriate modules.&lt;/span&gt;
&lt;span&gt;class&lt;/span&gt; AbstractPerson
  &lt;span&gt;def&lt;/span&gt; initialize
    &lt;span&gt;puts&lt;/span&gt; &lt;span&gt;&amp;quot;Initializing person&amp;quot;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
&lt;span&gt;# Provide additional &amp;quot;teacher&amp;quot; functionality as a module. This can be&lt;/span&gt;
&lt;span&gt;# mixed into subclasses of AbstractPerson, giving super access to&lt;/span&gt;
&lt;span&gt;# methods on AbstractPerson&lt;/span&gt;
&lt;span&gt;module&lt;/span&gt; Teacher
  &lt;span&gt;def&lt;/span&gt; initialize
    &lt;span&gt;puts&lt;/span&gt; &lt;span&gt;&amp;quot;Initializing teacher&amp;quot;&lt;/span&gt;
    &lt;span&gt;super&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
&lt;span&gt;# Our actual Person class. Mix in whatever modules you want to&lt;/span&gt;
&lt;span&gt;# add new functionality.&lt;/span&gt;
&lt;span&gt;class&lt;/span&gt; Person &lt;span&gt;&amp;lt;&lt;/span&gt; AbstractPerson
  &lt;span&gt;include&lt;/span&gt; Teacher
&lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
&lt;span&gt;# &amp;gt; Person.new&lt;/span&gt;
&lt;span&gt;# Initializing teacher&lt;/span&gt;
&lt;span&gt;# Initializing person&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Including modules essentially &lt;strong&gt;decorates&lt;/strong&gt; existing classes with additional functionality. You can include multiple modules to layer on existing functionality, but you don&amp;#8217;t need to create special factory or decorator objects to make this work.&lt;/p&gt;
&lt;p&gt;For those following along, the &lt;strong&gt;class&lt;/strong&gt;es used here are &amp;#8220;factories&amp;#8221;, and the &lt;strong&gt;module&lt;/strong&gt;s are &amp;#8220;decorators&amp;#8221;. But just as it&amp;#8217;s not useful to constantly think about classes as &amp;#8220;structs with function pointers&amp;#8221; because that&amp;#8217;s historically how they were implemented, I&amp;#8217;d argue it&amp;#8217;s not useful to constantly think about classes and modules as factories and decorators, simply because they&amp;#8217;re analogous to those concepts in languages like Java.&lt;/p&gt;
&lt;h2&gt;The Case of the PerQueryTimingFactoryFactory&lt;/h2&gt;
&lt;p&gt;Nick&amp;#8217;s example is actually a great example of a case where modularity is important. In this case, he has a base Query class that he wants to extend to add support for timeouts. He wrote his solution in Scala; I&amp;#8217;ll transcode it into Ruby.&lt;/p&gt;
&lt;p&gt;Feel free to skim the examples that follow. I&amp;#8217;m transcoding the Scala into Ruby to demonstrate something which you will be able to understand without fully understanding the examples.&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;class&lt;/span&gt; QueryProxy
  &lt;span&gt;def&lt;/span&gt; initialize&lt;span&gt;&amp;#40;&lt;/span&gt;query&lt;span&gt;&amp;#41;&lt;/span&gt;
    &lt;span&gt;@query&lt;/span&gt; = query
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;def&lt;/span&gt; &lt;span&gt;select&lt;/span&gt;
    delegate &lt;span&gt;&amp;#123;&lt;/span&gt; &lt;span&gt;@query&lt;/span&gt;.&lt;span&gt;select&lt;/span&gt; &lt;span&gt;&amp;#123;&lt;/span&gt; &lt;span&gt;yield&lt;/span&gt; &lt;span&gt;&amp;#125;&lt;/span&gt; &lt;span&gt;&amp;#125;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;def&lt;/span&gt; execute
    delegate &lt;span&gt;&amp;#123;&lt;/span&gt; &lt;span&gt;@query&lt;/span&gt;.&lt;span&gt;execute&lt;/span&gt; &lt;span&gt;&amp;#125;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;def&lt;/span&gt; cancel
    &lt;span&gt;@query&lt;/span&gt;.&lt;span&gt;cancel&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;  
&amp;nbsp;
  &lt;span&gt;def&lt;/span&gt; delegate
    &lt;span&gt;yield&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Then, in order to add support for Timeouts, he creates a new subclass of QueryProxy:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;class&lt;/span&gt; TimingOutQuery &lt;span&gt;&amp;lt;&lt;/span&gt; QueryProxy
  &lt;span&gt;def&lt;/span&gt; initialize&lt;span&gt;&amp;#40;&lt;/span&gt;query, timeout&lt;span&gt;&amp;#41;&lt;/span&gt;
    &lt;span&gt;@timeout&lt;/span&gt; = timeout
    &lt;span&gt;@query&lt;/span&gt;   = query
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;def&lt;/span&gt; delegate
    &lt;span&gt;begin&lt;/span&gt;
      Timeout.&lt;span&gt;timeout&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;@timeout&lt;span&gt;&amp;#41;&lt;/span&gt; &lt;span&gt;do&lt;/span&gt;
        &lt;span&gt;yield&lt;/span&gt;
      &lt;span&gt;end&lt;/span&gt;
    &lt;span&gt;rescue&lt;/span&gt; &lt;span&gt;Timeout::Error&lt;/span&gt;
      cancel
      &lt;span&gt;raise&lt;/span&gt; SqlTimeoutException
    &lt;span&gt;end&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Next, in order to instantiate a TimingOutQuery, he creates a TimingOutQueryFactory:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;class&lt;/span&gt; TimingOutQueryFactory
  &lt;span&gt;def&lt;/span&gt; initialize&lt;span&gt;&amp;#40;&lt;/span&gt;query_factory, timeout&lt;span&gt;&amp;#41;&lt;/span&gt;
    &lt;span&gt;@query_factory&lt;/span&gt; = query_factory
    &lt;span&gt;@timeout&lt;/span&gt; = timeout
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;def&lt;/span&gt; &lt;span&gt;self&lt;/span&gt;.&lt;span&gt;call&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;connection, query, &lt;span&gt;*&lt;/span&gt;args&lt;span&gt;&amp;#41;&lt;/span&gt;
    TimingOutQuery.&lt;span&gt;new&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;@query_factory.&lt;span&gt;call&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;connection, query, &lt;span&gt;*&lt;/span&gt;args&lt;span&gt;&amp;#41;&lt;/span&gt;, timeout&lt;span&gt;&amp;#41;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;As his coup de grâce, he shows how, now that everything is so modular, it is trivial to extend this system to support timeouts that were per-query.&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;class&lt;/span&gt; PerQueryTimingOutQueryFactory
  &lt;span&gt;def&lt;/span&gt; initialize&lt;span&gt;&amp;#40;&lt;/span&gt;query_factory, timeouts&lt;span&gt;&amp;#41;&lt;/span&gt;
    &lt;span&gt;@query_factory&lt;/span&gt; = query_factory
    &lt;span&gt;@timeouts&lt;/span&gt; = timeouts
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;def&lt;/span&gt; &lt;span&gt;self&lt;/span&gt;.&lt;span&gt;call&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;connection, query, &lt;span&gt;*&lt;/span&gt;args&lt;span&gt;&amp;#41;&lt;/span&gt;
    TimingOutQuery.&lt;span&gt;new&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;@query_factory.&lt;span&gt;call&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;connection, query, &lt;span&gt;*&lt;/span&gt;args&lt;span&gt;&amp;#41;&lt;/span&gt;, &lt;span&gt;@timeouts&lt;/span&gt;&lt;span&gt;&amp;#91;&lt;/span&gt;query&lt;span&gt;&amp;#93;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This is all true. By using factories and proxies, as you would in Java, this Ruby code &lt;strong&gt;is&lt;/strong&gt; modular. It is possible to create a new kind of &lt;code&gt;QueryFactory&lt;/code&gt; trivially.&lt;/p&gt;
&lt;p&gt;However, this code, by tacking close to vocabulary created to describe Java patterns, rebuilds functionality that exists natively in Ruby. It would be equivalent to creating a Hash of Procs in Ruby when a Class would do.&lt;/p&gt;
&lt;h2&gt;The Case: Solved&lt;/h2&gt;
&lt;p&gt;Ruby natively provides factories, proxies and decorators via language features. In fact, that vocabulary obscures the obvious solution to Nick&amp;#8217;s problem.&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;# No need for a proxy at all, so we skip it&lt;/span&gt;
&amp;nbsp;
&lt;span&gt;module&lt;/span&gt; Timeout
  &lt;span&gt;# super allows us to delegate to the Query this&lt;/span&gt;
  &lt;span&gt;# module is included into, even inside a block&lt;/span&gt;
  &lt;span&gt;def&lt;/span&gt; &lt;span&gt;select&lt;/span&gt;
    timeout &lt;span&gt;&amp;#123;&lt;/span&gt; &lt;span&gt;super&lt;/span&gt; &lt;span&gt;&amp;#125;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;def&lt;/span&gt; execute
    timeout &lt;span&gt;&amp;#123;&lt;/span&gt; &lt;span&gt;super&lt;/span&gt; &lt;span&gt;&amp;#125;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;# We get the cancel delegation natively, because&lt;/span&gt;
  &lt;span&gt;# we can use subclasses, rather than separate&lt;/span&gt;
  &lt;span&gt;# proxy object, to implement the proxy&lt;/span&gt;
&amp;nbsp;
private
  &lt;span&gt;# Since we're not using a proxy, we'll just implement&lt;/span&gt;
  &lt;span&gt;# the timeout method directly, and skip &amp;quot;delegate&amp;quot;&lt;/span&gt;
  &lt;span&gt;def&lt;/span&gt; timeout
    &lt;span&gt;# The Timeout module expects a duration method&lt;/span&gt;
    &lt;span&gt;# which classes that include Timeout should provide&lt;/span&gt;
    Timeout.&lt;span&gt;timeout&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;duration&lt;span&gt;&amp;#41;&lt;/span&gt; &lt;span&gt;do&lt;/span&gt;
      &lt;span&gt;yield&lt;/span&gt;
    &lt;span&gt;end&lt;/span&gt;
  &lt;span&gt;rescue&lt;/span&gt; &lt;span&gt;Timeout::Error&lt;/span&gt;
    cancel
    &lt;span&gt;raise&lt;/span&gt; SqlTimeoutException
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
&lt;span&gt;# Classes in Ruby serve double duty as &amp;quot;proxies&amp;quot; and&lt;/span&gt;
&lt;span&gt;# &amp;quot;factories&amp;quot;. This behavior is part of Ruby semantics.&lt;/span&gt;
&lt;span&gt;class&lt;/span&gt; TimingOutQuery &lt;span&gt;&amp;lt;&lt;/span&gt; Query
  &lt;span&gt;include&lt;/span&gt; Timeout
&amp;nbsp;
private
  &lt;span&gt;# implement duration to hardcode the value of 1&lt;/span&gt;
  &lt;span&gt;def&lt;/span&gt; duration
    &lt;span&gt;1&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
&lt;span&gt;# Creating a second subclass of Query, this time with&lt;/span&gt;
&lt;span&gt;# per-query timeout semantics.&lt;/span&gt;
&lt;span&gt;class&lt;/span&gt; PerQueryTimingOutQuery &lt;span&gt;&amp;lt;&lt;/span&gt; Query
  TIMEOUTS = &lt;span&gt;Hash&lt;/span&gt;.&lt;span&gt;new&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;0.5&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;.&lt;span&gt;merge&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;&amp;quot;query1&amp;quot;&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; &lt;span&gt;1&lt;/span&gt;, &lt;span&gt;&amp;quot;query2&amp;quot;&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; &lt;span&gt;3&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;include&lt;/span&gt; Timeout
&amp;nbsp;
private
  &lt;span&gt;def&lt;/span&gt; duration
    TIMEOUTS&lt;span&gt;&amp;#91;&lt;/span&gt;query&lt;span&gt;&amp;#93;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;As Nick would point out, what we&amp;#8217;re doing here, from a very abstract perspective, isn&amp;#8217;t all that different from his example. Our subclasses are proxies, our modules are decorators, and our classes are serving as factories. However, forcing that verbiage on built-in Ruby language features, in my opinion, only serves to complicate matters. More importantly, by starting to think about the problem in terms of the Java-inspired patterns, it&amp;#8217;s easy to end up building code that looks more like Nick&amp;#8217;s example than my solution above.&lt;/p&gt;
&lt;p&gt;For the record, I think that designing modularly is very important, and while Ruby provides built-in support for these modular patterns, we don&amp;#8217;t see enough usage of them. However, we should not assume that the reason for the overuse of poor modularity patterns (like alias_method_chain) result from a lack of discussion around proxies, decorators, and factories.&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;By the way, ActionController in Rails 3 provides an abstract superclass called ActionController::Metal, a series of modules that users can mix in to subclasses however they like, and a pre-built ActionController::Base with all the modules mixed in (to provide the convenient &amp;#8220;default&amp;#8221; experience). Additionally, users or extensions can easily provide additional modules to mix in to ActionController::Metal subclasses. This is precisely the pattern I am describing here, and I strongly recommend that Rubyists use it more when writing code they wish to be modular.&lt;/p&gt;&lt;/blockquote&gt;
&lt;h2&gt;Postscript: Scala&lt;/h2&gt;
&lt;p&gt;When researching for this article, I wondered why Nick hadn&amp;#8217;t used Ruby&amp;#8217;s equivalent to modules (traits) in his examples. It would be possible to write Scala code that was extremely similar to my preferred solution to the problem. I asked both Nick and the guys in #scala. Both said that while traits could solve this problem in Scala, they could not be used flexibly enough at runtime. &lt;/p&gt;
&lt;p&gt;In particular, Nick wanted to be able to read the list of &amp;#8220;decorators&amp;#8221; to use at runtime, and compose something that could create queries with the appropriate elements. According to the guys in #scala, it&amp;#8217;s a well-understood issue, and &lt;a href=&quot;http://www.artima.com/weblogs/viewpost.jsp?thread=275135&quot;&gt;Kevin Wright has a compiler plugin to solve this exact problem&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Finally, the guys there seemed to generally agree with my central thesis: that thinking about problems in terms of patterns originally devised for Java can leave a better, more implementation-appropriate solution sitting on the table, even when the better solution can be thought of in terms of the older pattern (with some contortions).&lt;/p&gt;

&lt;div class=&quot;feedflare&quot;&gt;
&lt;a href=&quot;http://feeds.feedburner.com/~ff/KatzGotYourTongue?a=Z_Mze9ZSYoQ:Vm2lscqbJ9A:yIl2AUoC8zA&quot;&gt;&lt;img src=&quot;http://feeds.feedburner.com/~ff/KatzGotYourTongue?d=yIl2AUoC8zA&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src=&quot;http://feeds.feedburner.com/~r/KatzGotYourTongue/~4/Z_Mze9ZSYoQ&quot; height=&quot;1&quot; width=&quot;1&quot; /&gt;</content>
		<author>
			<name>wycats</name>
			<uri>http://yehudakatz.com</uri>
		</author>
		<source>
			<title type="html">Katz Got Your Tongue?</title>
			<subtitle type="html">Random Geek-Related Thoughts</subtitle>
			<link rel="self" href="http://yehudakatz.com/feed/atom/"/>
			<id>http://yehudakatz.com/feed/atom/</id>
			<updated>2010-03-13T08:20:07+00:00</updated>
		</source>
	</entry>

	<entry xml:lang="en">
		<title type="html">The Blind Men and the Elephant: A Story of Noobs</title>
		<link href="http://feedproxy.google.com/~r/KatzGotYourTongue/~3/Iva0rld0aVQ/"/>
		<id>http://yehudakatz.com/?p=433</id>
		<updated>2010-02-10T02:19:39+00:00</updated>
		<content type="html">&lt;p&gt;If you will indulge me, I&amp;#8217;d like to paraphrase a familiar tale:&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;
Once upon a time, deep in the forest, there was a tribe of elephant curators. The elders of this tribe kept sophisticated, detailed notes about the proper care and feeding of elephants, and the villagers tended to follow along.&lt;/p&gt;
&lt;p&gt;Eventually, they dedicated a large section of the local library to books and articles on the care and feeding of elephants.&lt;/p&gt;
&lt;p&gt;One day, a group of blind nomads appeared in the village. Each of the blind men went to greet the villagers, and were met with welcomes. Wanting to be helpful, they walked over to one of the elephants and tried to learn about it.&lt;/p&gt;
&lt;p&gt;The first man, who stood next to the elephant&amp;#8217;s tail said, &amp;#8220;I feel a snake&amp;#8221;.&lt;/p&gt;
&lt;p&gt;The second, who stood next to the elephant&amp;#8217;s leg said, &amp;#8220;I feel a tree trunk&amp;#8221;.&lt;/p&gt;
&lt;p&gt;And so on.&lt;/p&gt;
&lt;p&gt;One of the group of nomads, who thought he felt a snake, went to the elders of the village and asked, &amp;#8220;I would like to help. How can I feed this creature?&amp;#8221;&lt;/p&gt;
&lt;p&gt;The elder replied: &amp;#8220;Sir, if you can&amp;#8217;t be bothered to search the library for information on the care and feeding of elephants, surely you are wasting our time&amp;#8221;.&lt;/p&gt;
&lt;p&gt;Years passed and after a grueling series of trials, the blind men became integrated into the culture, becoming some of the most successful at caring for the elephants.&lt;/p&gt;
&lt;p&gt;Eventually, another group of blind nomads appeared. The entire village, including the original group, proceeded to berate the new travelers. &amp;#8220;We&amp;#8217;ve spent quite a bit of time putting together a section of the library about how to care for these animals. You are wasting our time&amp;#8221;.&lt;/p&gt;
&lt;p&gt;Of course, the travelers did not know the creatures were elephants, and so they entered the library, searching for books on feeding snakes and caring for trees.&lt;/p&gt;
&lt;p&gt;Eventually, an elderly blind man, of the original group stood up and said: &amp;#8220;Have we forgotten than we, too, started in this confused state. We should help these travelers and perhaps they will become as wise and helpful as we became&amp;#8221;.
&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;To many participants in open source communities, this is a familiar tale. When a developer first comes across an open source project, either to use it in a project or to help, he is like a blind man feeling an elephant.&lt;/p&gt;
&lt;p&gt;It&amp;#8217;s easy to spit out &amp;#8220;lmgtfy.com&amp;#8221; or RTFM, but in truth, these beginners barely know where to look. All too often, we (open source leaders) assume that if someone couldn&amp;#8217;t figure out the right search term on Google, they can never become a viable community member.&lt;/p&gt;
&lt;p&gt;When I first started working on Rails, I distinctly remember not knowing what the &lt;code&gt;request&lt;/code&gt; method in Rails controllers was. To some degree, this could be attributed to its exclusion in api.rubyonrails.org, but some judicious Googling turned up the &lt;a href=&quot;http://api.rubyonrails.org/classes/ActionController/Request.html&quot;&gt;ActionController::Request&lt;/a&gt; class. Writing this post years later, the &lt;code&gt;request&lt;/code&gt; method still does not reside in the API docs, but I found the Request documentation in seconds.&lt;/p&gt;
&lt;p&gt;The problem is that a new developer simply has no conceptual model for the problem at all. In most cases, the &amp;#8220;noob&amp;#8221; can stare at &amp;#8220;the f***ing manual&amp;#8221; all day and simply fail to find something staring him in the face. Importantly, this does &lt;strong&gt;not&lt;/strong&gt; reflect a failing on the part of the new developer. Virtually everyone I know who worked their way from noob to senior Rails developer starting feeling around the elephant.&lt;/p&gt;
&lt;p&gt;As open source leaders, if we are interested in growing our communities, we should treat new developers as confused people with real potential. That&amp;#8217;s not to say that sinking dozens of hours down a black hole is a good use of time. On the other hand, the mismatch between how we think about problems once we become experienced and the way we feel around like a blind man when getting started makes the experience of getting started with an open source project far more painful than it needs to be.&lt;/p&gt;

&lt;div class=&quot;feedflare&quot;&gt;
&lt;a href=&quot;http://feeds.feedburner.com/~ff/KatzGotYourTongue?a=Iva0rld0aVQ:Gnl8EATYHrk:yIl2AUoC8zA&quot;&gt;&lt;img src=&quot;http://feeds.feedburner.com/~ff/KatzGotYourTongue?d=yIl2AUoC8zA&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src=&quot;http://feeds.feedburner.com/~r/KatzGotYourTongue/~4/Iva0rld0aVQ&quot; height=&quot;1&quot; width=&quot;1&quot; /&gt;</content>
		<author>
			<name>wycats</name>
			<uri>http://yehudakatz.com</uri>
		</author>
		<source>
			<title type="html">Katz Got Your Tongue?</title>
			<subtitle type="html">Random Geek-Related Thoughts</subtitle>
			<link rel="self" href="http://yehudakatz.com/feed/atom/"/>
			<id>http://yehudakatz.com/feed/atom/</id>
			<updated>2010-03-13T08:20:07+00:00</updated>
		</source>
	</entry>

	<entry xml:lang="en">
		<title type="html">Using Bundler in Real Life</title>
		<link href="http://feedproxy.google.com/~r/KatzGotYourTongue/~3/rsg0Ymkcoj4/"/>
		<id>http://yehudakatz.com/?p=431</id>
		<updated>2010-02-09T11:12:00+00:00</updated>
		<content type="html">&lt;p&gt;A lot of people have asked me what the recommended workflows for bundler are. Turns out, they&amp;#8217;re quite simple.&lt;/p&gt;
&lt;p&gt;Let&amp;#8217;s step through a few use-cases.&lt;/p&gt;
&lt;h2&gt;You Get a Repo for the First Time&lt;/h2&gt;
&lt;p&gt;You&amp;#8217;ve just checked out a git (or other) repository for an application that uses bundler. Regardless of any other features of bundler in use, just run:&lt;/p&gt;
&lt;pre&gt;bundle install&lt;/pre&gt;
&lt;p&gt;This will resolve all dependencies and install the ones that aren&amp;#8217;t already installed in system gems or in your system&amp;#8217;s bundler cache.&lt;/p&gt;
&lt;h2&gt;You Update a Repo Using Bundler&lt;/h2&gt;
&lt;p&gt;If you update a repository using bundler, and it has updated its dependencies in the Gemfile, regardless of any other features in use, just run:&lt;/p&gt;
&lt;pre&gt;bundle install&lt;/pre&gt;
&lt;p&gt;As above, this will resolve all dependencies and install any gems that are not already installed.&lt;/p&gt;
&lt;h2&gt;You have created a new Rails application&lt;/h2&gt;
&lt;p&gt;If you&amp;#8217;ve created a new Rails application, go inside it and run:&lt;/p&gt;
&lt;pre&gt;bundle install&lt;/pre&gt;
&lt;p&gt;This will make sure that Rails&amp;#8217; dependencies (such as SQLite3) are available in system gems or your system&amp;#8217;s bundler cache. In most cases, this will not need to install anything.&lt;/p&gt;
&lt;p&gt;To check whether your system already satisfied the application&amp;#8217;s dependencies, run:&lt;/p&gt;
&lt;pre&gt;bundle check&lt;/pre&gt;
&lt;p&gt;As you work in the application, you may wish to add dependencies. To do so, update the Gemfile, and run:&lt;/p&gt;
&lt;pre&gt;bundle install&lt;/pre&gt;
&lt;h2&gt;You are ready to share your new Rails application&lt;/h2&gt;
&lt;p&gt;Now that you have a working Rails application, and wish to share, you might wish to ensure that your collaborators get the same versions of the gems as you have. For instance, if webrat specified &amp;#8220;nokogiri &gt;= 1.4&amp;#8243; as a dependency, you might want to ensure that an update to nokogiri does not change the actual gems that &lt;code&gt;bundle install&lt;/code&gt; will install.&lt;/p&gt;
&lt;p&gt;To achieve this, run:&lt;/p&gt;
&lt;pre&gt;bundle lock&lt;/pre&gt;
&lt;p&gt;This will create a new file called Gemfile.lock in your root directory that contains the dependencies that you specified, as well as the fully resolved dependency graph. Check that file into source control.&lt;/p&gt;
&lt;p&gt;When your collaborators receive the repository and run &lt;code&gt;bundle install&lt;/code&gt;, bundler will use the resolved dependencies in Gemfile.lock.&lt;/p&gt;
&lt;h2&gt;You have a locked application, and wish to add a new dependency&lt;/h2&gt;
&lt;p&gt;If you add a new dependency to your Gemfile in a locked application, Bundler will give you an error if you try to perform any operations.&lt;/p&gt;
&lt;p&gt;You will want to run &lt;code&gt;bundle unlock&lt;/code&gt; to remove the lock, then &lt;code&gt;bundle install&lt;/code&gt; to ensure that the new dependencies are installed on your system, and finally &lt;code&gt;bundle lock&lt;/code&gt; again to relock your application to the new dependencies.&lt;/p&gt;
&lt;p&gt;We will add a command in a near-future version to perform all these steps for you (something like bundle install &amp;#8211;relock).&lt;/p&gt;
&lt;h2&gt;You want a self-contained application&lt;/h2&gt;
&lt;p&gt;In many cases, it is desirable to be able to have a self-contained application that you can share with others which contains all of the required gems.&lt;/p&gt;
&lt;p&gt;In addition to a general desire to remove a dependency on Gemcutter, you might have dependencies on gems that are not on a publicly accessible gem repository.&lt;/p&gt;
&lt;p&gt;To collect up all gems and place them into your app, run:&lt;/p&gt;
&lt;pre&gt;bundle pack&lt;/pre&gt;
&lt;p&gt;When running &lt;code&gt;bundle install&lt;/code&gt; in the future, Bundler will use packed gems, if available, in preference to gems available in other sources.&lt;/p&gt;
&lt;h2&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;I hope these workflows have clarified the intent of Bundler 0.9 (and 1.0). During our work on earlier versions, the lack of these workflows came up again and again as a source of frustration. This was the primary reason for the big changes in 0.9, so I hope you find them useful.&lt;/p&gt;

&lt;div class=&quot;feedflare&quot;&gt;
&lt;a href=&quot;http://feeds.feedburner.com/~ff/KatzGotYourTongue?a=rsg0Ymkcoj4:Dqc9t-xPYok:yIl2AUoC8zA&quot;&gt;&lt;img src=&quot;http://feeds.feedburner.com/~ff/KatzGotYourTongue?d=yIl2AUoC8zA&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src=&quot;http://feeds.feedburner.com/~r/KatzGotYourTongue/~4/rsg0Ymkcoj4&quot; height=&quot;1&quot; width=&quot;1&quot; /&gt;</content>
		<author>
			<name>wycats</name>
			<uri>http://yehudakatz.com</uri>
		</author>
		<source>
			<title type="html">Katz Got Your Tongue?</title>
			<subtitle type="html">Random Geek-Related Thoughts</subtitle>
			<link rel="self" href="http://yehudakatz.com/feed/atom/"/>
			<id>http://yehudakatz.com/feed/atom/</id>
			<updated>2010-03-13T08:20:07+00:00</updated>
		</source>
	</entry>

	<entry xml:lang="en">
		<title type="html">The Building Blocks of Ruby</title>
		<link href="http://feedproxy.google.com/~r/KatzGotYourTongue/~3/LGc60nYpojs/"/>
		<id>http://yehudakatz.com/?p=429</id>
		<updated>2010-02-07T14:53:40+00:00</updated>
		<content type="html">&lt;p&gt;When showing off cool features of Ruby to the uninitiated (&lt;a href=&quot;http://stackoverflow.com/questions/1099305/why-is-ruby-more-suitable-for-rails-than-python/1099367&quot;&gt;or to a language sparring partner&lt;/a&gt;), the excited Rubyist often shows off Ruby&amp;#8217;s &amp;#8220;powerful block syntax&amp;#8221;. Unfortunately, the Rubyist uses &amp;#8220;powerful block syntax&amp;#8221; as shorthand for a number of features that the Pythonista or Javaist simply has no context for.&lt;/p&gt;
&lt;p&gt;To start, &lt;a href=&quot;http://news.ycombinator.com/item?id=1097901&quot;&gt;we usually point at Rake, Rspec or Sinatra&lt;/a&gt; as examples of awesome usage of block syntax:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;get &lt;span&gt;&amp;quot;/hello&amp;quot;&lt;/span&gt; &lt;span&gt;do&lt;/span&gt;
  &lt;span&gt;&amp;quot;Hello World&amp;quot;&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;a href=&quot;http://news.ycombinator.com/item?id=1097928&quot;&gt;In response&lt;/a&gt;, Pythonistas usually point to these syntaxes as roughly equivalent:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;python&quot;&gt;@get&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;'/hi'&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;
&lt;span&gt;def&lt;/span&gt; hello&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;:
  &lt;span&gt;return&lt;/span&gt; &lt;span&gt;&amp;quot;Hello World&amp;quot;&lt;/span&gt;
&amp;nbsp;
&lt;span&gt;def&lt;/span&gt; hello&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt; -&lt;span&gt;&amp;gt;&lt;/span&gt; &lt;span&gt;&amp;quot;/hi&amp;quot;&lt;/span&gt;:
  &lt;span&gt;return&lt;/span&gt; &lt;span&gt;&amp;quot;Hello World&amp;quot;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;While the Python version may not be quite as pretty, nothing about them screams &amp;#8220;Ruby has much stronger capabilities here&amp;#8221;. Instead, by using examples like Sinatra, Rubyists trade in an argument about great semantic power for one about superficial beauty.&lt;/p&gt;
&lt;p&gt;Rubyists, Pythonistas and others working on web development share a common language in JavaScript. When describing blocks to &amp;#8220;outsiders&amp;#8221; who share a common knowledge of JavaScript, we tend to point at &lt;a href=&quot;https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Functions_and_function_scope&quot;&gt;JavaScript&lt;/a&gt; functions as a close analogue. Unfortunately, this only furthers the confusion.&lt;/p&gt;
&lt;p&gt;On the Ruby side, when &lt;a href=&quot;http://php.net/manual/en/functions.anonymous.php&quot;&gt;PHP&lt;/a&gt; or &lt;a href=&quot;http://www.javac.info/consensus-closures-jsr.html&quot;&gt;Java&lt;/a&gt; announces that they&amp;#8217;re &amp;#8220;adding closures&amp;#8221;, many of us don&amp;#8217;t stop to ask &amp;#8220;what kind of closures?&amp;#8221;&lt;/p&gt;
&lt;h2&gt;Cut to the Chase&lt;/h2&gt;
&lt;p&gt;Let&amp;#8217;s cut to the chase and use a better example of the utility of Ruby blocks.&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;def&lt;/span&gt; append&lt;span&gt;&amp;#40;&lt;/span&gt;location, data&lt;span&gt;&amp;#41;&lt;/span&gt;
  path = &lt;span&gt;Pathname&lt;/span&gt;.&lt;span&gt;new&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;location&lt;span&gt;&amp;#41;&lt;/span&gt;
  &lt;span&gt;raise&lt;/span&gt; &lt;span&gt;&amp;quot;Location does not exist&amp;quot;&lt;/span&gt; &lt;span&gt;unless&lt;/span&gt; path.&lt;span&gt;exist&lt;/span&gt;?
&amp;nbsp;
  &lt;span&gt;File&lt;/span&gt;.&lt;span&gt;open&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;path, &lt;span&gt;&amp;quot;a&amp;quot;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt; &lt;span&gt;do&lt;/span&gt; &lt;span&gt;|&lt;/span&gt;file&lt;span&gt;|&lt;/span&gt;
    file.&lt;span&gt;puts&lt;/span&gt; &lt;span&gt;YAML&lt;/span&gt;.&lt;span&gt;dump&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;data&lt;span&gt;&amp;#41;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;return&lt;/span&gt; data
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Here, the &lt;a href=&quot;http://ruby-doc.org/core/classes/IO.html#M002239&quot;&gt;&lt;code&gt;File.open&lt;/code&gt;&lt;/a&gt; method takes a block. It then opens a new file (in &amp;#8220;append&amp;#8221; mode), and yields the open file into the block. When the block completes, Ruby closes the file. Except that Ruby doesn&amp;#8217;t just close the file when the block completes; it &lt;strong&gt;guarantees&lt;/strong&gt; that the File will be closed, even if executing the block results in a raise. Let&amp;#8217;s take a look at &lt;a href=&quot;http://github.com/evanphx/rubinius/blob/master/kernel/common/io.rb#L234-248&quot;&gt;the implementation of File in Rubinius&lt;/a&gt;:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;def&lt;/span&gt; &lt;span&gt;self&lt;/span&gt;.&lt;span&gt;open&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;*&lt;/span&gt;args&lt;span&gt;&amp;#41;&lt;/span&gt;
  io = new &lt;span&gt;*&lt;/span&gt;args
&amp;nbsp;
  &lt;span&gt;return&lt;/span&gt; io &lt;span&gt;unless&lt;/span&gt; block_given?
&amp;nbsp;
  &lt;span&gt;begin&lt;/span&gt;
    &lt;span&gt;yield&lt;/span&gt; io
  &lt;span&gt;ensure&lt;/span&gt;
    &lt;span&gt;begin&lt;/span&gt;
      io.&lt;span&gt;close&lt;/span&gt; &lt;span&gt;unless&lt;/span&gt; io.&lt;span&gt;closed&lt;/span&gt;?
    &lt;span&gt;rescue&lt;/span&gt; &lt;span&gt;StandardError&lt;/span&gt;
      &lt;span&gt;# nothing, just swallow them.&lt;/span&gt;
    &lt;span&gt;end&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This means that you can wrap up idioms like pervasive try/catch/finally in methods.&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;# Without blocks&lt;/span&gt;
&lt;span&gt;def&lt;/span&gt; append&lt;span&gt;&amp;#40;&lt;/span&gt;location, data&lt;span&gt;&amp;#41;&lt;/span&gt;
  path = &lt;span&gt;Pathname&lt;/span&gt;.&lt;span&gt;new&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;location&lt;span&gt;&amp;#41;&lt;/span&gt;
  &lt;span&gt;raise&lt;/span&gt; &lt;span&gt;&amp;quot;Location does not exist&amp;quot;&lt;/span&gt; &lt;span&gt;unless&lt;/span&gt; path.&lt;span&gt;exist&lt;/span&gt;?
&amp;nbsp;
  &lt;span&gt;begin&lt;/span&gt;
    file = &lt;span&gt;File&lt;/span&gt;.&lt;span&gt;open&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;path, &lt;span&gt;&amp;quot;a&amp;quot;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;
    file.&lt;span&gt;puts&lt;/span&gt; &lt;span&gt;YAML&lt;/span&gt;.&lt;span&gt;dump&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;data&lt;span&gt;&amp;#41;&lt;/span&gt;
  &lt;span&gt;ensure&lt;/span&gt;
    file.&lt;span&gt;close&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;return&lt;/span&gt; data
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Because Ruby runs ensure clauses even when the exception happened in a block, programmers can reliably ensure that Ruby executes teardown logic hidden away in abstractions.&lt;/p&gt;
&lt;p&gt;This example only demonstrates the power of well-designed lambdas. With the addition of one small additional feature, Ruby&amp;#8217;s blocks become something altogether different.&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;def&lt;/span&gt; write&lt;span&gt;&amp;#40;&lt;/span&gt;location, data&lt;span&gt;&amp;#41;&lt;/span&gt;
  path = &lt;span&gt;Pathname&lt;/span&gt;.&lt;span&gt;new&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;location&lt;span&gt;&amp;#41;&lt;/span&gt;
  &lt;span&gt;raise&lt;/span&gt; &lt;span&gt;&amp;quot;Location does not exist&amp;quot;&lt;/span&gt; &lt;span&gt;unless&lt;/span&gt; path.&lt;span&gt;exist&lt;/span&gt;?
&amp;nbsp;
  &lt;span&gt;File&lt;/span&gt;.&lt;span&gt;open&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;path, &lt;span&gt;&amp;quot;w&amp;quot;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt; &lt;span&gt;do&lt;/span&gt; &lt;span&gt;|&lt;/span&gt;file&lt;span&gt;|&lt;/span&gt;
    &lt;span&gt;return&lt;/span&gt; &lt;span&gt;false&lt;/span&gt; &lt;span&gt;if&lt;/span&gt; &lt;span&gt;Digest::MD5&lt;/span&gt;.&lt;span&gt;hexdigest&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;file.&lt;span&gt;read&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt; == data.&lt;span&gt;hash&lt;/span&gt;
    file.&lt;span&gt;puts&lt;/span&gt; &lt;span&gt;YAML&lt;/span&gt;.&lt;span&gt;dump&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;data&lt;span&gt;&amp;#41;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;return&lt;/span&gt; &lt;span&gt;true&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In the above case, imagine that writing the data to disk is quite expensive, and we can skip writing if the MD5 hash of the file&amp;#8217;s contents match a &lt;code&gt;hash&lt;/code&gt; method on the data. Here, we&amp;#8217;ll return false if the method did not write to disk, and true if the method did.&lt;/p&gt;
&lt;p&gt;Ruby&amp;#8217;s blocks support non-local-return (&lt;a href=&quot;http://docs.selflanguage.org/langref.html#returns&quot;&gt;some&lt;/a&gt; &lt;a href=&quot;http://gafter.blogspot.com/2006/08/tennents-correspondence-principle-and.html&quot;&gt;references&lt;/a&gt;), which means that a return from the block behaves identically to returning from the block&amp;#8217;s original context. In this case, returning from inside the block returns from the &lt;code&gt;write&lt;/code&gt; method, but Ruby will still run the &lt;code&gt;ensure&lt;/code&gt; block closing the file.&lt;/p&gt;
&lt;p&gt;You can think of non-local-return as behaving something like:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;def&lt;/span&gt; write&lt;span&gt;&amp;#40;&lt;/span&gt;location, data&lt;span&gt;&amp;#41;&lt;/span&gt;
  path = &lt;span&gt;Pathname&lt;/span&gt;.&lt;span&gt;new&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;location&lt;span&gt;&amp;#41;&lt;/span&gt;
  &lt;span&gt;raise&lt;/span&gt; &lt;span&gt;&amp;quot;Location does not exist&amp;quot;&lt;/span&gt; &lt;span&gt;unless&lt;/span&gt; path.&lt;span&gt;exist&lt;/span&gt;?
&amp;nbsp;
  &lt;span&gt;File&lt;/span&gt;.&lt;span&gt;open&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;path, &lt;span&gt;&amp;quot;w&amp;quot;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt; &lt;span&gt;do&lt;/span&gt; &lt;span&gt;|&lt;/span&gt;file&lt;span&gt;|&lt;/span&gt;
    &lt;span&gt;raise&lt;/span&gt; &lt;span&gt;Return&lt;/span&gt;.&lt;span&gt;new&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;false&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt; &lt;span&gt;if&lt;/span&gt; &lt;span&gt;Digest::MD5&lt;/span&gt;.&lt;span&gt;hexdigest&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;file.&lt;span&gt;read&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt; == data.&lt;span&gt;hash&lt;/span&gt;
    file.&lt;span&gt;puts&lt;/span&gt; &lt;span&gt;YAML&lt;/span&gt;.&lt;span&gt;dump&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;data&lt;span&gt;&amp;#41;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;return&lt;/span&gt; &lt;span&gt;true&lt;/span&gt;
&lt;span&gt;rescue&lt;/span&gt; &lt;span&gt;Return&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; e
  &lt;span&gt;return&lt;/span&gt; e.&lt;span&gt;object&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;where Return is &lt;code&gt;Return = Struct.new(:object)&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Of course, any reasonable lambda implementation will support this, but Ruby&amp;#8217;s version has the benefit of feeling just like a normal return, and requiring much less chrome to achieve it. It also behaves well in scenarios that already use &lt;code&gt;rescue&lt;/code&gt; or &lt;code&gt;ensure&lt;/code&gt;, avoiding mind-warping combinations.&lt;/p&gt;
&lt;p&gt;Further, Ruby also supports super inside of blocks. Imagine the &lt;code&gt;write&lt;/code&gt; method was defined on a subclass of a simpler class whose &lt;code&gt;write&lt;/code&gt; method took the raw data from the file and printed it to a log.&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;def&lt;/span&gt; write&lt;span&gt;&amp;#40;&lt;/span&gt;location, data&lt;span&gt;&amp;#41;&lt;/span&gt;
  path = &lt;span&gt;Pathname&lt;/span&gt;.&lt;span&gt;new&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;location&lt;span&gt;&amp;#41;&lt;/span&gt;
  &lt;span&gt;raise&lt;/span&gt; &lt;span&gt;&amp;quot;Location does not exist&amp;quot;&lt;/span&gt; &lt;span&gt;unless&lt;/span&gt; path.&lt;span&gt;exist&lt;/span&gt;?
&amp;nbsp;
  &lt;span&gt;File&lt;/span&gt;.&lt;span&gt;open&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;path, &lt;span&gt;&amp;quot;w&amp;quot;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt; &lt;span&gt;do&lt;/span&gt; &lt;span&gt;|&lt;/span&gt;file&lt;span&gt;|&lt;/span&gt;
    file_data = file.&lt;span&gt;read&lt;/span&gt;
    &lt;span&gt;super&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;location, file_data&lt;span&gt;&amp;#41;&lt;/span&gt;
    &lt;span&gt;return&lt;/span&gt; &lt;span&gt;false&lt;/span&gt; &lt;span&gt;if&lt;/span&gt; &lt;span&gt;Digest::MD5&lt;/span&gt;.&lt;span&gt;hexdigest&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;file_data&lt;span&gt;&amp;#41;&lt;/span&gt; == data.&lt;span&gt;hash&lt;/span&gt;
    file.&lt;span&gt;puts&lt;/span&gt; &lt;span&gt;YAML&lt;/span&gt;.&lt;span&gt;dump&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;data&lt;span&gt;&amp;#41;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;return&lt;/span&gt; &lt;span&gt;true&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In a purer lambda scenario, we would need to store off a reference to the self, then use that reference inside the lambda:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;def&lt;/span&gt; write&lt;span&gt;&amp;#40;&lt;/span&gt;location, data&lt;span&gt;&amp;#41;&lt;/span&gt;
  path = &lt;span&gt;Pathname&lt;/span&gt;.&lt;span&gt;new&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;location&lt;span&gt;&amp;#41;&lt;/span&gt;
  &lt;span&gt;raise&lt;/span&gt; &lt;span&gt;&amp;quot;Location does not exist&amp;quot;&lt;/span&gt; &lt;span&gt;unless&lt;/span&gt; path.&lt;span&gt;exist&lt;/span&gt;?
&amp;nbsp;
  this = &lt;span&gt;self&lt;/span&gt;
  &lt;span&gt;File&lt;/span&gt;.&lt;span&gt;open&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;path, &lt;span&gt;&amp;quot;w&amp;quot;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt; &lt;span&gt;do&lt;/span&gt; &lt;span&gt;|&lt;/span&gt;file&lt;span&gt;|&lt;/span&gt;
    file_data = file.&lt;span&gt;read&lt;/span&gt;
&amp;nbsp;
    &lt;span&gt;# imaginary Ruby construct that would be needed without&lt;/span&gt;
    &lt;span&gt;# non-local-super&lt;/span&gt;
    this.&lt;span&gt;super&lt;/span&gt;.&lt;span&gt;write&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;location, file_data&lt;span&gt;&amp;#41;&lt;/span&gt;
    &lt;span&gt;raise&lt;/span&gt; &lt;span&gt;Return&lt;/span&gt;.&lt;span&gt;new&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;false&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt; &lt;span&gt;if&lt;/span&gt; &lt;span&gt;Digest::MD5&lt;/span&gt;.&lt;span&gt;hexdigest&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;file_data&lt;span&gt;&amp;#41;&lt;/span&gt; == data.&lt;span&gt;hash&lt;/span&gt;
    file.&lt;span&gt;puts&lt;/span&gt; &lt;span&gt;YAML&lt;/span&gt;.&lt;span&gt;dump&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;data&lt;span&gt;&amp;#41;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;return&lt;/span&gt; &lt;span&gt;true&lt;/span&gt;
&lt;span&gt;rescue&lt;/span&gt; &lt;span&gt;Return&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; e
  &lt;span&gt;return&lt;/span&gt; e.&lt;span&gt;object&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You can also yield to a method&amp;#8217;s block inside a block. Imagine that the write method is called with a block that chooses the correct data to use based on whether the file is executable:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;def&lt;/span&gt; write&lt;span&gt;&amp;#40;&lt;/span&gt;location&lt;span&gt;&amp;#41;&lt;/span&gt;
  path = &lt;span&gt;Pathname&lt;/span&gt;.&lt;span&gt;new&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;location&lt;span&gt;&amp;#41;&lt;/span&gt;
  &lt;span&gt;raise&lt;/span&gt; &lt;span&gt;&amp;quot;Location does not exist&amp;quot;&lt;/span&gt; &lt;span&gt;unless&lt;/span&gt; path.&lt;span&gt;exist&lt;/span&gt;?
&amp;nbsp;
  &lt;span&gt;File&lt;/span&gt;.&lt;span&gt;open&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;path, &lt;span&gt;&amp;quot;w&amp;quot;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt; &lt;span&gt;do&lt;/span&gt; &lt;span&gt;|&lt;/span&gt;file&lt;span&gt;|&lt;/span&gt;
    file_data = file.&lt;span&gt;read&lt;/span&gt;
    &lt;span&gt;super&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;location&lt;span&gt;&amp;#41;&lt;/span&gt;
    data = &lt;span&gt;yield&lt;/span&gt; file
    &lt;span&gt;return&lt;/span&gt; &lt;span&gt;false&lt;/span&gt; &lt;span&gt;if&lt;/span&gt; &lt;span&gt;Digest::MD5&lt;/span&gt;.&lt;span&gt;hexdigest&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;file_data&lt;span&gt;&amp;#41;&lt;/span&gt; == data.&lt;span&gt;hash&lt;/span&gt;
    file.&lt;span&gt;puts&lt;/span&gt; &lt;span&gt;YAML&lt;/span&gt;.&lt;span&gt;dump&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;data&lt;span&gt;&amp;#41;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;return&lt;/span&gt; &lt;span&gt;true&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This would be called via:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;write&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;&amp;quot;/path/to/file&amp;quot;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt; &lt;span&gt;do&lt;/span&gt; &lt;span&gt;|&lt;/span&gt;file&lt;span&gt;|&lt;/span&gt;
  &lt;span&gt;if&lt;/span&gt; file.&lt;span&gt;executable&lt;/span&gt;?
    &lt;span&gt;&amp;quot;#!/usr/bin/env ruby&lt;span&gt;\n&lt;/span&gt;puts 'Hello World!'&amp;quot;&lt;/span&gt;
  &lt;span&gt;else&lt;/span&gt;
    &lt;span&gt;&amp;quot;Hello World!&amp;quot;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In a pure-lambda language, we would take the block in as a normal argument to the function, then call it inside the closure:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;def&lt;/span&gt; write&lt;span&gt;&amp;#40;&lt;/span&gt;location, block&lt;span&gt;&amp;#41;&lt;/span&gt;
  path = &lt;span&gt;Pathname&lt;/span&gt;.&lt;span&gt;new&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;location&lt;span&gt;&amp;#41;&lt;/span&gt;
  &lt;span&gt;raise&lt;/span&gt; &lt;span&gt;&amp;quot;Location does not exist&amp;quot;&lt;/span&gt; &lt;span&gt;unless&lt;/span&gt; path.&lt;span&gt;exist&lt;/span&gt;?
&amp;nbsp;
  this = &lt;span&gt;self&lt;/span&gt;
  &lt;span&gt;File&lt;/span&gt;.&lt;span&gt;open&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;path, &lt;span&gt;&amp;quot;w&amp;quot;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt; &lt;span&gt;do&lt;/span&gt; &lt;span&gt;|&lt;/span&gt;file&lt;span&gt;|&lt;/span&gt;
    file_data = file.&lt;span&gt;read&lt;/span&gt;
&amp;nbsp;
    &lt;span&gt;# imaginary Ruby construct that would be needed without&lt;/span&gt;
    &lt;span&gt;# non-local-super&lt;/span&gt;
    this.&lt;span&gt;super&lt;/span&gt;.&lt;span&gt;write&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;location, file_data&lt;span&gt;&amp;#41;&lt;/span&gt;
    data = block.&lt;span&gt;call&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;file&lt;span&gt;&amp;#41;&lt;/span&gt;
    &lt;span&gt;raise&lt;/span&gt; &lt;span&gt;Return&lt;/span&gt;.&lt;span&gt;new&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;false&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt; &lt;span&gt;if&lt;/span&gt; &lt;span&gt;Digest::MD5&lt;/span&gt;.&lt;span&gt;hexdigest&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;file_data&lt;span&gt;&amp;#41;&lt;/span&gt; == data.&lt;span&gt;hash&lt;/span&gt;
    file.&lt;span&gt;puts&lt;/span&gt; &lt;span&gt;YAML&lt;/span&gt;.&lt;span&gt;dump&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;data&lt;span&gt;&amp;#41;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;return&lt;/span&gt; &lt;span&gt;true&lt;/span&gt;
&lt;span&gt;rescue&lt;/span&gt; &lt;span&gt;Return&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; e
  &lt;span&gt;return&lt;/span&gt; e.&lt;span&gt;object&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The real benefit of Ruby&amp;#8217;s approach comes from the fact that the code inside the block would be identical if the method did not take a block. Consider the identical method, except taking a File instead of a location:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;def&lt;/span&gt; write&lt;span&gt;&amp;#40;&lt;/span&gt;file&lt;span&gt;&amp;#41;&lt;/span&gt;
  file_data = file.&lt;span&gt;read&lt;/span&gt;
  &lt;span&gt;super&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;file&lt;span&gt;&amp;#41;&lt;/span&gt;
  data = &lt;span&gt;yield&lt;/span&gt; file
  &lt;span&gt;return&lt;/span&gt; &lt;span&gt;false&lt;/span&gt; &lt;span&gt;if&lt;/span&gt; &lt;span&gt;Digest::MD5&lt;/span&gt;.&lt;span&gt;hexdigest&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;file_data&lt;span&gt;&amp;#41;&lt;/span&gt; == data.&lt;span&gt;hash&lt;/span&gt;
  file.&lt;span&gt;puts&lt;/span&gt; &lt;span&gt;YAML&lt;/span&gt;.&lt;span&gt;dump&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;data&lt;span&gt;&amp;#41;&lt;/span&gt;
  &lt;span&gt;return&lt;/span&gt; &lt;span&gt;true&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Without the block, the Ruby code looks exactly the same. This means that Ruby programmers can more easily abstract out repeated patterns into methods that take blocks without having to rewrite a bunch of code. It also means that using a block does not interrupt the normal flow of code, and it&amp;#8217;s possible to create new &amp;#8220;control flow&amp;#8221; constructs that behave almost identically to built-in control flow constructs like &lt;code&gt;if&lt;/code&gt; and &lt;code&gt;while&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Rails uses this to good effect with &lt;a href=&quot;http://api.rubyonrails.org/classes/ActionController/MimeResponds/InstanceMethods.html&quot;&gt;respond_to&lt;/a&gt;, which provides convenient syntax for declaring content negotiation:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;def&lt;/span&gt; index
  &lt;span&gt;@people&lt;/span&gt; = Person.&lt;span&gt;find&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;:all&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;
&amp;nbsp;
  respond_to &lt;span&gt;do&lt;/span&gt; &lt;span&gt;|&lt;/span&gt;format&lt;span&gt;|&lt;/span&gt;
    &lt;span&gt;format&lt;/span&gt;.&lt;span&gt;html&lt;/span&gt; &lt;span&gt;# default action is render&lt;/span&gt;
    &lt;span&gt;format&lt;/span&gt;.&lt;span&gt;xml&lt;/span&gt; &lt;span&gt;&amp;#123;&lt;/span&gt; render &lt;span&gt;:xml&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; &lt;span&gt;@people&lt;/span&gt;.&lt;span&gt;xml&lt;/span&gt; &lt;span&gt;&amp;#125;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Because of the way Ruby blocks work, you can also return from any of the format blocks:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;def&lt;/span&gt; index
  &lt;span&gt;@people&lt;/span&gt; = Person.&lt;span&gt;find&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;:all&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;
&amp;nbsp;
  respond_to &lt;span&gt;do&lt;/span&gt; &lt;span&gt;|&lt;/span&gt;format&lt;span&gt;|&lt;/span&gt;
    &lt;span&gt;format&lt;/span&gt;.&lt;span&gt;html&lt;/span&gt; &lt;span&gt;&amp;#123;&lt;/span&gt; redirect_to&lt;span&gt;&amp;#40;&lt;/span&gt;person_path&lt;span&gt;&amp;#40;&lt;/span&gt;@people.&lt;span&gt;first&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt; &lt;span&gt;and&lt;/span&gt; &lt;span&gt;return&lt;/span&gt; &lt;span&gt;&amp;#125;&lt;/span&gt;
    &lt;span&gt;format&lt;/span&gt;.&lt;span&gt;xml&lt;/span&gt;  &lt;span&gt;&amp;#123;&lt;/span&gt; render &lt;span&gt;:xml&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; &lt;span&gt;@people&lt;/span&gt;.&lt;span&gt;xml&lt;/span&gt; &lt;span&gt;&amp;#125;&lt;/span&gt;
    &lt;span&gt;format&lt;/span&gt;.&lt;span&gt;json&lt;/span&gt; &lt;span&gt;&amp;#123;&lt;/span&gt; render &lt;span&gt;:json&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; &lt;span&gt;@people&lt;/span&gt;.&lt;span&gt;json&lt;/span&gt; &lt;span&gt;&amp;#125;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
  session&lt;span&gt;&amp;#91;&lt;/span&gt;&lt;span&gt;:web_service&lt;/span&gt;&lt;span&gt;&amp;#93;&lt;/span&gt; = &lt;span&gt;true&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Here, we returned from the HTML format after redirecting, allowing us to take additional action (setting a :web_service key on the session) for other cases (XML and JSON mime types).&lt;/p&gt;
&lt;p&gt;Keep in mind that the code above is a demonstration of a number of features of Ruby&amp;#8217;s blocks. It&amp;#8217;s very rare to see &lt;code&gt;return&lt;/code&gt;, &lt;code&gt;yield&lt;/code&gt; and &lt;code&gt;super&lt;/code&gt; all used in a single block. That said, Ruby programmers commonly use one or more of these constructs inside blocks, because their usage is seamless.&lt;/p&gt;
&lt;h2&gt;So Why Are Ruby&amp;#8217;s Blocks Better?&lt;/h2&gt;
&lt;p&gt;If you made it this far, let&amp;#8217;s take a look at another use of blocks in Ruby: mutex synchronization.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://java.sun.com/docs/books/tutorial/essential/concurrency/locksync.html&quot;&gt;Java supports synchronization&lt;/a&gt; via a special &lt;code&gt;synchronized&lt;/code&gt; keyword:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;java&quot;&gt;&lt;span&gt;class&lt;/span&gt; Example &lt;span&gt;&amp;#123;&lt;/span&gt;
  &lt;span&gt;final&lt;/span&gt; Lock lock &lt;span&gt;=&lt;/span&gt; &lt;span&gt;new&lt;/span&gt; Lock&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;void&lt;/span&gt; example&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt; &lt;span&gt;&amp;#123;&lt;/span&gt;
    &lt;span&gt;synchronized&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;lock&lt;span&gt;&amp;#41;&lt;/span&gt; &lt;span&gt;&amp;#123;&lt;/span&gt;
      &lt;span&gt;// do dangerous stuff here&lt;/span&gt;
    &lt;span&gt;&amp;#125;&lt;/span&gt;
  &lt;span&gt;&amp;#125;&lt;/span&gt;
&lt;span&gt;&amp;#125;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Essentially, Java provides a special construct for expressing the idea that it should run a block of code once at a time for a given instance of the synchronization object. Because Java provides a special construct, you can return from inside the synchronization block, and the Java runtime does the appropriate things.&lt;/p&gt;
&lt;p&gt;Similarly, Python required the use of try/finally until Python 2.5, when &lt;a href=&quot;http://www.python.org/dev/peps/pep-0343/&quot;&gt;they added a special language feature&lt;/a&gt; to handle the &lt;code&gt;try/finally&lt;/code&gt; idiom:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;python&quot;&gt;&lt;span&gt;class&lt;/span&gt; Example:
  &lt;span&gt;# old&lt;/span&gt;
  &lt;span&gt;def&lt;/span&gt; example&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;self&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;:
    lock.&lt;span&gt;acquire&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;
    &lt;span&gt;try&lt;/span&gt;:
      ... &lt;span&gt;access&lt;/span&gt; shared &lt;span&gt;resource&lt;/span&gt;
    &lt;span&gt;finally&lt;/span&gt;:
      lock.&lt;span&gt;release&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt; &lt;span&gt;# release lock, no matter what&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;# new&lt;/span&gt;
  &lt;span&gt;def&lt;/span&gt; example&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;self&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;:
    &lt;span&gt;with&lt;/span&gt; lock:
      ... &lt;span&gt;access&lt;/span&gt; shared &lt;span&gt;resource&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In Python&amp;#8217;s 2.5&amp;#8217;s case, the object passed to &lt;code&gt;with&lt;/code&gt; must implement a special protocol (including &lt;code&gt;__enter__&lt;/code&gt; and &lt;code&gt;__exit__&lt;/code&gt; methods), so the &lt;code&gt;with&lt;/code&gt; statement cannot be used like Ruby&amp;#8217;s general-purpose, lightweight blocks.&lt;/p&gt;
&lt;p&gt;Ruby &lt;a href=&quot;http://ruby-doc.org/core/classes/Mutex.html&quot;&gt;represents the same concept&lt;/a&gt; using a method that takes a block:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;class&lt;/span&gt; Example
  @@lock = &lt;span&gt;Mutex&lt;/span&gt;.&lt;span&gt;new&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;def&lt;/span&gt; example
    @@lock.&lt;span&gt;synchronize&lt;/span&gt; &lt;span&gt;do&lt;/span&gt;
      &lt;span&gt;# do dangerous stuff here&lt;/span&gt;
    &lt;span&gt;end&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Importantly, &lt;code&gt;synchronize&lt;/code&gt; is a normal Ruby method. The original version, written in pure Ruby, &lt;a href=&quot;http://ruby-doc.org/core/classes/Mutex.src/M000916.html&quot;&gt;looks like this&lt;/a&gt;:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;def&lt;/span&gt; synchronize
  lock
  &lt;span&gt;begin&lt;/span&gt;
    &lt;span&gt;yield&lt;/span&gt;
  &lt;span&gt;ensure&lt;/span&gt;
    unlock
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;It has all the hallmarks of what we&amp;#8217;ve discussed so far. It locks, yields to the block, and ensures that the lock will be released. This means that if a Ruby programmer &lt;code&gt;returns&lt;/code&gt; from inside the block, &lt;code&gt;synchronize&lt;/code&gt; will behave correctly.&lt;/p&gt;
&lt;p&gt;This example demonstrates the key power of Ruby&amp;#8217;s blocks: they can easily replace language constructs. In this case, a Ruby programmer can take unsafe code, plop it inside a synchronization block, and it will continue to work.&lt;/p&gt;
&lt;h3&gt;Postscript&lt;/h3&gt;
&lt;p&gt;I&amp;#8217;ve historically written my posts without very many links, mostly out of a fear of links going out of date. I&amp;#8217;ve received increasing requests for more annotations in my posts, so I&amp;#8217;ll start doing that. Let me know if you think my annotations in this post were useful, and feel free to give me any suggestions on that front that you find useful.&lt;/p&gt;

&lt;div class=&quot;feedflare&quot;&gt;
&lt;a href=&quot;http://feeds.feedburner.com/~ff/KatzGotYourTongue?a=LGc60nYpojs:wEPi4b6Xh8s:yIl2AUoC8zA&quot;&gt;&lt;img src=&quot;http://feeds.feedburner.com/~ff/KatzGotYourTongue?d=yIl2AUoC8zA&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src=&quot;http://feeds.feedburner.com/~r/KatzGotYourTongue/~4/LGc60nYpojs&quot; height=&quot;1&quot; width=&quot;1&quot; /&gt;</content>
		<author>
			<name>wycats</name>
			<uri>http://yehudakatz.com</uri>
		</author>
		<source>
			<title type="html">Katz Got Your Tongue?</title>
			<subtitle type="html">Random Geek-Related Thoughts</subtitle>
			<link rel="self" href="http://yehudakatz.com/feed/atom/"/>
			<id>http://yehudakatz.com/feed/atom/</id>
			<updated>2010-03-13T08:20:07+00:00</updated>
		</source>
	</entry>

	<entry>
		<title type="html">notes from yet another rails 3 upgrade</title>
		<link href="http://jackndempsey.blogspot.com/2010/02/notes-from-yet-another-rails-3-upgrade.html"/>
		<id>tag:blogger.com,1999:blog-4467999944823309015.post-4914097396958496479</id>
		<updated>2010-02-04T12:20:29+00:00</updated>
		<content type="html">These tips and tricks are valid as of today, February 3rd, 2010. Who knows what next week will bring, but for now, I thought compiling some of the techniques and hacks I've seen might help others. &lt;br /&gt;&lt;br /&gt;&lt;span&gt;Rails Related&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;If you see a &quot;no such file to require: rails/all&quot; you probably don't have Rails installed correctly. I was basing my upgrade on the great &lt;a href=&quot;http://peepcode.com/system/uploads/2010/peepcode-free-004-live-coding-rails-3-upgrade.mov&quot;&gt;screencast&lt;/a&gt; produced by Topfunky over at &lt;a href=&quot;http://peepcode.com&quot;&gt;peepcode.com&lt;/a&gt;, and his method didn't work for me. To get things running I added this to my Gemfile:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;path &quot;/Users/jack/git/rails&quot;, :glob =&gt; &quot;{*/,}*.gemspec&quot;&lt;br /&gt;&lt;del&gt;gem &quot;rails&quot;, &quot;3.0.pre&quot;&lt;/del&gt;&lt;br /&gt;gem &quot;rails&quot;, &quot;3.0.0.beta&quot; # Just changed today it seems&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Obviously, change the path to wherever you have rails checked out locally.&lt;br /&gt;&lt;br /&gt;&lt;span&gt;HAML&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;If you see something like this: Missing template articles/index with {:formats=&gt;[:html]} in view path &lt;br /&gt;&lt;br /&gt;you probably need to install haml. &lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;$ haml --rails .&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;and then you'll likely see an error about html_safe!&lt;br /&gt;&lt;br /&gt;Geoff has a nice little idea going with a config/initializers/rails3_hacks.rb file which I've also done. You can drop this in and get by the errors for now:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;projects/app master &gt; cat config/initializers/rails3_hacks.rb &lt;br /&gt;class String&lt;br /&gt;  def html_safe!() self end&lt;br /&gt;  def html_safe?() true end&lt;br /&gt;  def safe_concat(value) value+self end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;span&gt;WillPaginate&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Looks like Mislav and other's have been doing a lot of work in upgrading the plugin. If you see a &quot;no paginate method for Class....&quot; error, try cloning the source down locally, check out the rails 3 branch,build and install that gem and then update your Gemfile:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;...&lt;br /&gt;gem 'will_paginate', '3.0.pre'&lt;br /&gt;...&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;It also seemed that WillPaginate wasn't being required into my app. For now I've created a config/initializers/requires.rb file and dropped in random requires for attention later. Putting a &quot;require 'will_paginate'&quot; in there fixed that up nicely. &lt;br /&gt;&lt;br /&gt;&lt;span&gt;Update:&lt;/span&gt;&lt;br /&gt;Seems something's broken now. I'm getting this error:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;uninitialized constant ActiveRecord::Calculations::CALCULATIONS_OPTIONS&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Seems WP relies on that constant without checking for its existence, and it looks like its gone now. Easy enough to quick patch it if you need to get by it, but I imagine it'll be fixed in the rails3 branch soon enough. &lt;br /&gt;&lt;span&gt;Assorted Issues&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;HTMLEntities doesn't seem to be working, so I commented that out for now. For a great beginning list of what gems should be working, take a look here: &lt;a href=&quot;http://wiki.rubyonrails.org/rails/version3/plugins_and_gems&quot;&gt;http://wiki.rubyonrails.org/rails/version3/plugins_and_gems&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href=&quot;http://blog.plataformatec.com.br/tag/devise/&quot;&gt;Devise&lt;/a&gt;: not ready for rails 3 yet. I think they're trying to see what routing changes will settle and how that will affect their code. I'm going to see if I can pop over into that and help out a bit as I'd love to keep using it in this app. &lt;br /&gt;&lt;br /&gt;new_rails_defaults.rb: I had an error from this file and realized it's old anyway, so git rm and it's gone. &lt;br /&gt;&lt;br /&gt;&lt;a href=&quot;http://www.modrails.com/&quot;&gt;Passenger&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;I also overhauled a bunch of other ruby related items on my boxes recently, and one of those items was passenger. After updating to 2.2.9, reinstalling the module, changing RailsEnv to RackEnv, things are mostly good. Remember to &lt;a href=&quot;http://rvm.beginrescueend.com/passenger/&quot;&gt;point to the right ruby if you're using rvm&lt;/a&gt;. I also deleted the config.ru file per their &lt;a href=&quot;http://blog.phusion.nl/2010/01/08/phusion-passenger-2-2-9-released/&quot;&gt;recommendation&lt;/a&gt;: &quot;Smart spawning (the mechanism with which REE’s 33% memory reduction is implemented) is *not* supported for Rack apps. This means that if you want to utilize smart spawning with Rails 3, then you should remove your config.ru file.&quot;&lt;br /&gt;&lt;br /&gt;I think that's it for now. I imagine I'll update this page as I deal with new issues. Pretty excited about the new changes and starting to actually make use of them.&lt;br /&gt;&lt;br /&gt;&lt;span&gt;Update&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Previous notes were from my macbook pro. Now that I'm back on the iMac I'm finding other issues. Latest is:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;/Users/jack/git/rails/activesupport/lib/active_support/dependencies.rb:167:in `require': no such file to load -- rails/commands/rails (LoadError)&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;I'm actually not sure if that was something I did as it looks like its definitely just &quot;require 'rails/commands'&quot; in the latest source, but either way, if you're seeing that, just take off the second rails in that line. Correct line should be:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;require 'rails/commands'&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;at the end of your script/rails.&lt;br /&gt;&lt;br /&gt;&lt;span&gt;Bundler&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Somehow just found myself facing this:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;projects/app master &gt; bundle install&lt;br /&gt;/Users/jack/.rvm/gems/ree-1.8.7-2010.01/bin/bundle: line 1: require: command not found&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;A quick cat and I see something's changed, no #!...ruby line to see. Realized I had bundler 0.9.0 and 0.9.0.pre4, so I uninstalled them both, and reinstalled with:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;$ gem install bundler --pre&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;and now have bundler-0.9.0.pre5 installed. &lt;br /&gt;&lt;br /&gt;Still things are borked, but it seems its a result of github being down: &quot;Page did not respond in a timely fashion.&quot;&lt;br /&gt;&lt;br /&gt;Will try again in a few to make sure this gets me back on track.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Update&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;Ok, github's back, and just saw a tweet from Wycats. Looks like 0.9.0 final is out now, but it's recommended to wait til tomorrow and 0.9.1 + some docs. YMMV.&lt;br /&gt;&lt;br /&gt;&lt;span&gt;Application != ApplicationController&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;So, the app I'm converting was originally written in Merb, was partially converted to Rails 2, and is now on its way towards Rails 3. In this transition I've generally remembered what pieces to change from Merb to Rails, but I had a nice stumble earlier in forgetting to change a controller. So, if you find yourself in a place where you have a route that you know works, but is not recognized, make sure your controller is named whatever_controller.rb, and contains the code:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class WhateverController  ApplicationController&lt;br /&gt;...&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;not &quot; Application&quot;. You'll an error about a route missing and could easily spend a couple hours missing the obvious.&lt;br /&gt;&lt;br /&gt;&lt;span&gt;Routes&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;I keep tripping myself up with this.  Lets say you have a route defined as:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  match 'foobar/:foobar' =&gt; 'foobar#foobar', :as =&gt; :foobar&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;These calls will all fail:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;ree-1.8.7-2009.10 &gt; app.foobar_path&lt;br /&gt;ActionController::RoutingError: No route matches {:action=&gt;&quot;foobar&quot;, :controller=&gt;&quot;foobar&quot;}&lt;br /&gt;... from (irb):3&lt;br /&gt;ree-1.8.7-2009.10 &gt; app.foobar_path('')&lt;br /&gt;ActionController::RoutingError: No route matches {:use_defaults=&gt;false, :action=&gt;&quot;foobar&quot;, :foobar=&gt;&quot;&quot;, :controller=&gt;&quot;foobar&quot;}&lt;br /&gt;... from (irb):4&lt;br /&gt;ree-1.8.7-2009.10 &gt; app.foobar_path('test')&lt;br /&gt; =&gt; &quot;/foobar/test&quot; &lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;until that last one which passes in a value. This makes sense, as the route defines foobar as a requirement, and not passing in a value is an 'error'. It's a little less obvious that an empty string doesn't satisfy it to me, and definitely not obvious when you have this in a loop and an object just happens to be missing its value for foobar. &lt;br /&gt;&lt;br /&gt;One way around this is to make the :foobar piece optional, and handle that reality in your controller elsewhere. You could also protect against this by checking that object.foobar exists in your loop, and not linking unless it does.&lt;br /&gt;&lt;br /&gt;I've also just realized that for a certain route of mine, one that passes in domain names, sending in '&lt;a href=&quot;http://euraeka.com&quot;&gt;euraeka.com&lt;/a&gt;' will result in an error. I'm not sure if this is the recommended solution, but its letting me progress for now:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  match 'domain/:domain' =&gt; 'search#domain', :as =&gt; :domain, :domain =&gt; /.+/&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;I'm using domain here instead of host as I think host might be reserved somewhere. Couldn't seem to get it to work before, so if you're having that issue, let me know.&lt;div class=&quot;blogger-post-footer&quot;&gt;&lt;img width=&quot;1&quot; height=&quot;1&quot; src=&quot;https://blogger.googleusercontent.com/tracker/4467999944823309015-4914097396958496479?l=jackndempsey.blogspot.com&quot; alt=&quot;&quot; /&gt;&lt;/div&gt;</content>
		<author>
			<name>Jack Dempsey</name>
			<email>noreply@blogger.com</email>
			<uri>http://jackndempsey.blogspot.com/</uri>
		</author>
		<source>
			<title type="html">class &amp;lt;&amp;lt; self</title>
			<subtitle type="html">An increasingly random set of various topics including ruby &amp;amp; rails, merb, clojure, startups...</subtitle>
			<link rel="self" href="http://jackndempsey.blogspot.com/feeds/posts/default"/>
			<id>tag:blogger.com,1999:blog-4467999944823309015</id>
			<updated>2010-02-11T20:20:07+00:00</updated>
		</source>
	</entry>

	<entry xml:lang="en">
		<title type="html">How and why I joined the “suit people”</title>
		<link href="http://merbist.com/2010/02/02/how-and-why-i-joined-the-suit-people/"/>
		<id>http://merbist.com/?p=712</id>
		<updated>2010-02-02T19:19:26+00:00</updated>
		<content type="html">&lt;p&gt;It is now official: I have traded my freedom &amp;amp; home office for a job title, an Aeron chair in a cubicle and a 401K.&lt;/p&gt;
&lt;p&gt;I received my new employee package and, in less than a week, I will officially become a full-time employee at &lt;a title=&quot;SCEA&quot; href=&quot;http://en.wikipedia.org/wiki/Sony_Computer_Entertainment_of_America&quot; target=&quot;_blank&quot;&gt; SCEA&lt;/a&gt; (Sony Computer Entertainment America).&lt;/p&gt;
&lt;p&gt;I&amp;#8217;m going to work in the PlayStation department, working on PS3, PS2 and PSP game titles developed by &lt;a href=&quot;http://www.naughtydog.com/&quot; target=&quot;_blank&quot;&gt;various&lt;/a&gt; &lt;a href=&quot;http://en.wikipedia.org/wiki/SCE_Studio_San_Diego&quot; target=&quot;_blank&quot;&gt;game&lt;/a&gt; &lt;a href=&quot;http://en.wikipedia.org/wiki/Sony_Computer_Entertainment_Worldwide_Studios&quot; target=&quot;_blank&quot;&gt;studios&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://merbist.com/wp-content/uploads/2010/02/sony_playstation_32.jpg&quot;&gt;&lt;img class=&quot;aligncenter size-medium wp-image-714&quot; title=&quot;sony playstation PS3&quot; src=&quot;http://merbist.com/wp-content/uploads/2010/02/sony_playstation_32-300x206.jpg&quot; alt=&quot;&quot; width=&quot;240&quot; height=&quot;165&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;Why &amp;#8216;o why?&lt;/h3&gt;
&lt;p&gt;&lt;em&gt;Why leave behind a happy life of indie contracting to join corporate America?&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;For many reasons actually:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;A Team&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Being a consultant I have been working with other independent consultants and existing teams. Nonetheless, I really miss being part of a stable team which grows together and learns from each other as we go through new projects and maintain old ones.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Long term plan&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As a consultant, I usually start projects or &amp;#8220;rescue&amp;#8221; existing projects. I work on them for a little while and then move on. It&amp;#8217;s exciting and rewarding but you don&amp;#8217;t really pay the consequences of your mistakes. You usually don&amp;#8217;t have to maintain the code you wrote and you rarely deal with the mistakes &lt;em&gt;you&lt;/em&gt; made.&lt;/p&gt;
&lt;p&gt;It sounds good, nobody likes to maintain the &lt;span&gt;crappy&lt;/span&gt; awesome code they wrote 2 years ago and most developers love working on new stuff. But at the same time, to become a better engineer you need to learn from you mistakes and assuming responsibility for your bad decisions is part of the process.&lt;/p&gt;
&lt;p&gt;It might sound weird, but I&amp;#8217;m actually excited to work on long term projects and feel some sort of ownership over the projects. Having to support games for many years means that I&amp;#8217;d better not mess up the implementation. And if I do, I hope I&amp;#8217;ll quickly learn from my mistakes.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Avoiding burn out&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;There is no secret: when you are passionate about what you do, you have a hard time stopping and taking a break. I&amp;#8217;m a recovering workaholic and it&amp;#8217;s really hard for me to say no when I&amp;#8217;m presented the possibility to work on interesting projects. I love what I do and I keep writing code even after I&amp;#8217;m done with client work.&lt;/p&gt;
&lt;p&gt;The problem is that this can start me on the slippery slope to isolating myself from friends, family and people who don&amp;#8217;t share the same passion. I&amp;#8217;m really lucky that my wife is a geek and loves hanging out at conferences, looking at code and playing with my buggy prototypes. But still, I spend too much time &amp;#8220;playing&amp;#8221; with my computer and I just can&amp;#8217;t manage my free time wisely.&lt;/p&gt;
&lt;p&gt;Having a full time position will hopefully help me put boundaries and will hopefully teach me to disconnect from work.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Exciting projects&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Hey, let&amp;#8217;s be honest, how many geeks do you know don&amp;#8217;t want to work in the video game industry? By the way, if you don&amp;#8217;t have a PS3, they are now at $299 and on top of getting an awesome console you get a blue ray player! (And no, I do not receive any bonuses or commissions for mentioning the console or promoting it in my blog. I had to pay for my own like everyone else.)&lt;br /&gt;
&lt;br /&gt;&lt;/p&gt;
&lt;h3&gt;Corporate America? Are you going to write Java now?&lt;/h3&gt;
&lt;p&gt;&lt;img class=&quot;aligncenter&quot; title=&quot;corporate america&quot; src=&quot;http://farm2.static.flickr.com/1412/1264424156_24f4571b10.jpg&quot; alt=&quot;&quot; width=&quot;350&quot; height=&quot;233&quot; /&gt;&lt;/p&gt;
&lt;p&gt;No, I&amp;#8217;m mainly going to stick to the language I love: &lt;strong&gt;Ruby&lt;/strong&gt;.&lt;br /&gt;
From time to time I will probably use other languages here and there, but that usually makes me love Ruby even more. The reality is that Ruby&amp;#8217;s power and flexibility seem to be appreciated by SCEA, which makes sense when you have tight deadlines and a lot of new technologies to deal with. Ruby is a perfect match!&lt;/p&gt;
&lt;p&gt;As you can guess, I can&amp;#8217;t go into any detail about how and why Sony uses Ruby, but let me just say that while games are still usually written in C++, they are becoming more and more interactive and need to communicate with game servers where some logic operates. Game players also need to interact with other gamers as well as check their gaming progress online, as well as the progress of the players around them etc&amp;#8230; Basically, outside of the game engine and the console SDK, there is a lot of potential for Ruby.&lt;/p&gt;
&lt;p&gt;Coming back to Corporate America, I have to say that I&amp;#8217;ve known my future manager for a few years now. He&amp;#8217;s always been a fervent Ruby advocate and has introduced lots of teams to the happiness of Ruby &amp;amp; Rails development. He&amp;#8217;s also a great developer who&amp;#8217;s contributing patches to major projects and has a bunch of cool stuff on github. To give you an idea, my job description mentions Rails, Merb, Sinatra, CouchdB, MongoDB, Redis, AWS. All these Ruby technologies are actually already used in production or are being seriously evaluated.&lt;/p&gt;
&lt;p&gt;I&amp;#8217;m also really looking forward to join the existing team. I know I&amp;#8217;m going to love working with a bunch of awesome developers coming from various backgrounds.&lt;/p&gt;
&lt;p&gt;Those who know me, know that I&amp;#8217;m not a morning person. And while your typical office job is categorized as &amp;#8216;9-5&amp;#8242;, don&amp;#8217;t feel too bad for me. I will be joining the video game product department, and morning people are rather rare in these kinds of groups &lt;img src=&quot;http://merbist.com/wp-includes/images/smilies/icon_wink.gif&quot; alt=&quot;;)&quot; class=&quot;wp-smiley&quot; /&gt; &lt;/p&gt;
&lt;h3&gt;Conclusion&lt;/h3&gt;
&lt;p&gt;I&amp;#8217;m really excited about this opportunity. For me, it is proof again that the Ruby revolution took place and that the Enterprise is evolving. Of course, time will tell if I am right, but I am quite confident.&lt;/p&gt;
&lt;p&gt;Also, Sony is always looking for new, talented people who want to push the entertainment world to the next level. Feel free to keep in touch with me if you are interested in joining the fun.&lt;/p&gt;</content>
		<author>
			<name>Matt Aimonetti (mattetti)</name>
			<uri>http://merbist.com</uri>
		</author>
		<source>
			<title type="html">Merbist</title>
			<subtitle type="html">Rails 3, Ruby &amp;amp; MacRuby news - tutorials - consulting - training</subtitle>
			<link rel="self" href="http://merbist.com/feed/"/>
			<id>http://merbist.com/feed/</id>
			<updated>2010-03-13T12:20:05+00:00</updated>
		</source>
	</entry>

	<entry xml:lang="en">
		<title type="html">Undo/Redo in MacRuby</title>
		<link href="http://merbist.com/2010/02/02/undoredo-in-macruby/"/>
		<id>http://merbist.com/?p=719</id>
		<updated>2010-02-02T10:19:44+00:00</updated>
		<content type="html">&lt;p&gt;As I&amp;#8217;m working on my upcoming O&amp;#8217;Reilly MacRuby book, I&amp;#8217;m writing quite a lot of example code. I have spent the last few weeks digging through most of the Foundation framework classes to hopefully make Cocoa more accessible to Ruby developers.&lt;/p&gt;
&lt;p&gt;In some instances things might look quite weird to someone new to Cocoa in some cases, things seem almost too easy. Here is an example implementing a undo/redo functionality using Foundations&amp;#8217; &lt;em&gt;&lt;a href=&quot;http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSUndoManager_Class/Reference/Reference.html&quot;&gt;NSUndoManager&lt;/a&gt;&lt;/em&gt;.&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;framework &lt;span&gt;'Foundation'&lt;/span&gt;
&lt;span&gt;class&lt;/span&gt; Player
  attr_accessor &lt;span&gt;:x&lt;/span&gt;, &lt;span&gt;:y&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;def&lt;/span&gt; initialize
    &lt;span&gt;@x&lt;/span&gt; = &lt;span&gt;@y&lt;/span&gt; = &lt;span&gt;0&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;def&lt;/span&gt; undo_manager
    &lt;span&gt;@manager&lt;/span&gt; &lt;span&gt;||&lt;/span&gt;= NSUndoManager.&lt;span&gt;alloc&lt;/span&gt;.&lt;span&gt;init&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;def&lt;/span&gt; left
    undo_manager.&lt;span&gt;prepareWithInvocationTarget&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;self&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;.&lt;span&gt;right&lt;/span&gt;
    &lt;span&gt;@x&lt;/span&gt; &lt;span&gt;-&lt;/span&gt;= &lt;span&gt;1&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;def&lt;/span&gt; right
    undo_manager.&lt;span&gt;prepareWithInvocationTarget&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;self&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;.&lt;span&gt;left&lt;/span&gt;
    &lt;span&gt;@x&lt;/span&gt; &lt;span&gt;+&lt;/span&gt;= &lt;span&gt;1&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Which you can use as such:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&amp;nbsp;
&lt;span&gt;&amp;gt;&amp;gt;&lt;/span&gt; lara = Player.&lt;span&gt;new&lt;/span&gt;
&lt;span&gt;=&amp;gt;&lt;/span&gt; &lt;span&gt;&amp;lt;&lt;/span&gt;Player:0x200267c80 &lt;span&gt;@y&lt;/span&gt;=&lt;span&gt;0&lt;/span&gt; &lt;span&gt;@x&lt;/span&gt;=&lt;span&gt;0&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;
&lt;span&gt;&amp;gt;&amp;gt;&lt;/span&gt; lara.&lt;span&gt;undo_manager&lt;/span&gt;.&lt;span&gt;canUndo&lt;/span&gt;
&lt;span&gt;=&amp;gt;&lt;/span&gt; &lt;span&gt;false&lt;/span&gt; &lt;span&gt;# normal since we did not do anything yet&lt;/span&gt;
&lt;span&gt;&amp;gt;&amp;gt;&lt;/span&gt; lara.&lt;span&gt;left&lt;/span&gt;
&lt;span&gt;=&amp;gt;&lt;/span&gt; &lt;span&gt;-&lt;/span&gt;&lt;span&gt;1&lt;/span&gt;
&lt;span&gt;&amp;gt;&amp;gt;&lt;/span&gt; lara.&lt;span&gt;x&lt;/span&gt; &lt;span&gt;# -1&lt;/span&gt;
&lt;span&gt;=&amp;gt;&lt;/span&gt; &lt;span&gt;-&lt;/span&gt;&lt;span&gt;1&lt;/span&gt;
&lt;span&gt;&amp;gt;&amp;gt;&lt;/span&gt; lara.&lt;span&gt;undo_manager&lt;/span&gt;.&lt;span&gt;canUndo&lt;/span&gt;
&lt;span&gt;=&amp;gt;&lt;/span&gt; &lt;span&gt;true&lt;/span&gt; &lt;span&gt;# now we can undo, so let's try&lt;/span&gt;
&lt;span&gt;&amp;gt;&amp;gt;&lt;/span&gt; lara.&lt;span&gt;undo_manager&lt;/span&gt;.&lt;span&gt;undo&lt;/span&gt; &lt;span&gt;# undo back to initial position&lt;/span&gt;
&lt;span&gt;=&amp;gt;&lt;/span&gt; &lt;span&gt;#&amp;lt;NSUndoManager:0x200257560&amp;gt;&lt;/span&gt;
&lt;span&gt;&amp;gt;&amp;gt;&lt;/span&gt; lara.&lt;span&gt;x&lt;/span&gt;
&lt;span&gt;=&amp;gt;&lt;/span&gt; &lt;span&gt;0&lt;/span&gt;
&lt;span&gt;&amp;gt;&amp;gt;&lt;/span&gt; lara.&lt;span&gt;undo_manager&lt;/span&gt;.&lt;span&gt;canUndo&lt;/span&gt;
&lt;span&gt;=&amp;gt;&lt;/span&gt; &lt;span&gt;false&lt;/span&gt; &lt;span&gt;# we can't anymore which makes sense &lt;/span&gt;
&lt;span&gt;&amp;gt;&amp;gt;&lt;/span&gt; lara.&lt;span&gt;undo_manager&lt;/span&gt;.&lt;span&gt;canRedo&lt;/span&gt;
&lt;span&gt;=&amp;gt;&lt;/span&gt; &lt;span&gt;true&lt;/span&gt; &lt;span&gt;# however we can redo what we just undone&lt;/span&gt;
&lt;span&gt;&amp;gt;&amp;gt;&lt;/span&gt; lara.&lt;span&gt;undo_manager&lt;/span&gt;.&lt;span&gt;redo&lt;/span&gt; &lt;span&gt;# redo to before we called undo&lt;/span&gt;
&lt;span&gt;=&amp;gt;&lt;/span&gt; &lt;span&gt;#&amp;lt;NSUndoManager:0x200257560&amp;gt;&lt;/span&gt;
&lt;span&gt;&amp;gt;&amp;gt;&lt;/span&gt; lara.&lt;span&gt;x&lt;/span&gt;
&lt;span&gt;=&amp;gt;&lt;/span&gt; &lt;span&gt;-&lt;/span&gt;&lt;span&gt;1&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The above example was tested in macirb but as you can see, actions can be undone and redone very very easily. This is just a quick preview of what you can do using Ruby + Cocoa and hopefully it will give you some cool ideas to implement.&lt;/p&gt;</content>
		<author>
			<name>Matt Aimonetti (mattetti)</name>
			<uri>http://merbist.com</uri>
		</author>
		<source>
			<title type="html">Merbist</title>
			<subtitle type="html">Rails 3, Ruby &amp;amp; MacRuby news - tutorials - consulting - training</subtitle>
			<link rel="self" href="http://merbist.com/feed/"/>
			<id>http://merbist.com/feed/</id>
			<updated>2010-03-13T12:20:05+00:00</updated>
		</source>
	</entry>

	<entry xml:lang="en">
		<title type="html">Bundler 0.9: Heading Toward 1.0</title>
		<link href="http://feedproxy.google.com/~r/KatzGotYourTongue/~3/ftaop_o2H_M/"/>
		<id>http://yehudakatz.com/?p=427</id>
		<updated>2010-02-01T08:50:57+00:00</updated>
		<content type="html">&lt;p&gt;Over the past two years, Carl and I have been working on-again off-again on the problem of dependency resolution. Fabien Franzen implemented the first partial solution for Merb, with &lt;code&gt;thor merb:gem:install&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;When we started working on Rails, we knew we wanted to finally crack the nut, making it possible for Rails itself to have some of its own dependencies, and solving some persistent, long-term problems with gem plugins.&lt;/p&gt;
&lt;p&gt;Earlier this year, we released bundler, our first real attempt to solve this problem. Among other things, it shipped with the first true dependency resolver for Rubygems. It also modeled the entire dependency process on top of Rubygems, allowing us to support git repositories in the same dependency resolution process as Rubygems themselves.&lt;/p&gt;
&lt;p&gt;Over the next few months, we refined the bundler quite a bit. We&amp;#8217;re proud of the fact that individuals, web shops, and deployment companies have adopted bundler, and Gemfile has become a standard way of expressing gem dependencies for an app.&lt;/p&gt;
&lt;p&gt;During this time, we&amp;#8217;ve also received quite a bit of feedback. As we approach the release of Bundler 1.0, which we hope to ship along with Rails 3.0 final, we took the opportunity to take a look at all the feedback people have sent so far.&lt;/p&gt;
&lt;p&gt;Having done so, we&amp;#8217;re proud to announce Bundler 0.9 with radically improved workflows that fit our needs and the needs of those who have contributed feedback and patches.&lt;/p&gt;
&lt;h2&gt;The Cache&lt;/h2&gt;
&lt;p&gt;Before Bundler 0.9, Bundler installed gems and git repositories to a local application cache. When we looked at the deployment workflow, we found that while people liked the ability to cache their &lt;code&gt;.gem&lt;/code&gt; files in their application, they had a lot of trouble at deployment time.&lt;/p&gt;
&lt;p&gt;Essentially, people (and we!) wanted the ability to expand the gems to a central cache that they could reuse across deployments. People tried various symlinking approaches, but it became clear to us Bundler 1.0 needed to support this workflow natively.&lt;/p&gt;
&lt;p&gt;On a related note, people who developed a number of applications on a single machine wanted to be able to reuse a single system cache across their application, and not need to connect to remotes so often when they already had all the gems they needed on their systems.&lt;/p&gt;
&lt;p&gt;Finally, Rails itself grated against the default Bundler workflow. Most people installing a new Rails app run &lt;code&gt;gem install rails&lt;/code&gt;, then &lt;code&gt;rails my_app&lt;/code&gt;, and then want to go into their application and start working. But with Bundler 0.5 to 0.8, they needed to hit the remotes again even though, by definition, the system gem cache already had all the gems they needed.&lt;/p&gt;
&lt;h2&gt;Runtime&lt;/h2&gt;
&lt;p&gt;The fully-packaged application works fantastically for deployment, enabling a repeatable, reliable development to staging to production workflow. However, the need to explicitly bundle the application after added a new dependency in the very early stages of an application&amp;#8217;s life cycle feels more like a compile step than a lean mean agile machine.&lt;/p&gt;
&lt;p&gt;In Bundler 0.9, we&amp;#8217;re adding the ability to run your application against gems already on your system, with the same reliability that you&amp;#8217;ve grown to love from our dependency resolver.&lt;/p&gt;
&lt;p&gt;Once you&amp;#8217;ve gotten your application working, you can lock down the dependencies, so coworkers or production will use exactly the same environment that worked for you.&lt;/p&gt;
&lt;p&gt;This allows you to use your common system gem repository across multiple apps, with a fully resolved virtual environment for each application based on the app&amp;#8217;s dependencies specified in the Gemfile.&lt;/p&gt;
&lt;h2&gt;Locking&lt;/h2&gt;
&lt;p&gt;A lot of people (including me) love the idea of storing the &lt;code&gt;.gem&lt;/code&gt; files in the application to create a single deployable unit with no network dependencies.&lt;/p&gt;
&lt;p&gt;Storing the &lt;code&gt;.gem&lt;/code&gt; files in the repository also provides a record of the fully resolved dependencies for your application, so that a new released gem between testing and deployment cannot change the environment. In Bundler 0.9, we provide a mechanism for you to save that record without having to also store the &lt;code&gt;.gem&lt;/code&gt; files.&lt;/p&gt;
&lt;p&gt;This won&amp;#8217;t give you a single, dependency-less deployment package, but it will save you from unexpected environment changes.&lt;/p&gt;
&lt;h2&gt;New Commands&lt;/h2&gt;
&lt;p&gt;When we started Bundler, we had just one command: &lt;code&gt;gem bundle&lt;/code&gt;. Over the following months, the command took on a life of its own, with a slew of flags representing both options and verbs. Bundler 1.0 will have a small, streamlined list of commands, each with its own set of flags:&lt;/p&gt;
&lt;h3&gt;bundle pack&lt;/h3&gt;
&lt;p&gt;Take all &lt;code&gt;.gem&lt;/code&gt; files needed for the application and place them into the local application. By default, bundler places them in &lt;code&gt;vendor/gems&lt;/code&gt;.&lt;/p&gt;
&lt;h3&gt;bundle lock&lt;/h3&gt;
&lt;p&gt;Resolve all dependencies in the Gemfile, and store a record of that process. In the future, use that record, preventing any changes in the environment or in the remote sources from changing the gems used by the application.&lt;/p&gt;
&lt;h3&gt;bundle install&lt;/h3&gt;
&lt;p&gt;Install the bundle specified by the Gemfile to the cache. By default,  Bundler installs the gems (and git repositories) into system gems. You can override this to install into another location, or into your local application, just like in Bundler 0.8 and before.&lt;/p&gt;
&lt;p&gt;After cloning a new repository for the first time, you will probably want to run this command.&lt;/p&gt;
&lt;p&gt;If you &lt;code&gt;pack&lt;/code&gt;ed gems, Bundler will use the gems from your application. If you &lt;code&gt;pack&lt;/code&gt;ed and &lt;code&gt;lock&lt;/code&gt;ed gems, Bundler will install the gems immediately without updating the remote sources.&lt;/p&gt;
&lt;h3&gt;bundle check&lt;/h3&gt;
&lt;p&gt;Check to see whether your cache has all the required dependencies. If it does not, Bundler will print out the list of missing gems.&lt;/p&gt;
&lt;h3&gt;bundle exec &amp;#8230;&lt;/h3&gt;
&lt;p&gt;Run the specified command using the local environment. For instance, if you have Rails 2.3 and 3.0 installed, if you run &lt;code&gt;bundle exec rails .&lt;/code&gt; in a Rails 2.3 application, Bundler will run the &lt;code&gt;rails&lt;/code&gt; command using Rails 2.3 (and other gems in the current environment).&lt;/p&gt;
&lt;h2&gt;Environments&lt;/h2&gt;
&lt;p&gt;In Bundler 0.9, as in Bundler 0.8 and before, the Gemfile specifies an app-specific environment. New in Bundler 0.9, you may store the &lt;code&gt;.gem&lt;/code&gt; files and unpacked gems in a central location, as well as in the application itself.&lt;/p&gt;
&lt;p&gt;Because Bundler ships with a dependency resolver, you do not need to specify named environments and switch between them. Instead, the environments are virtual, based on the gems you specify as application dependencies in the Gemfile, and you still get all the benefits of a shared, system-wide cache.&lt;/p&gt;

&lt;div class=&quot;feedflare&quot;&gt;
&lt;a href=&quot;http://feeds.feedburner.com/~ff/KatzGotYourTongue?a=ftaop_o2H_M:8e0ctkASsIs:yIl2AUoC8zA&quot;&gt;&lt;img src=&quot;http://feeds.feedburner.com/~ff/KatzGotYourTongue?d=yIl2AUoC8zA&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src=&quot;http://feeds.feedburner.com/~r/KatzGotYourTongue/~4/ftaop_o2H_M&quot; height=&quot;1&quot; width=&quot;1&quot; /&gt;</content>
		<author>
			<name>wycats</name>
			<uri>http://yehudakatz.com</uri>
		</author>
		<source>
			<title type="html">Katz Got Your Tongue?</title>
			<subtitle type="html">Random Geek-Related Thoughts</subtitle>
			<link rel="self" href="http://yehudakatz.com/feed/atom/"/>
			<id>http://yehudakatz.com/feed/atom/</id>
			<updated>2010-03-13T08:20:07+00:00</updated>
		</source>
	</entry>

	<entry xml:lang="en">
		<title type="html">SafeBuffers and Rails 3.0</title>
		<link href="http://feedproxy.google.com/~r/KatzGotYourTongue/~3/PJ7bGS6Dc0U/"/>
		<id>http://yehudakatz.com/?p=421</id>
		<updated>2010-02-01T07:41:19+00:00</updated>
		<content type="html">&lt;p&gt;As you may have read, Rails adds XSS protection by default in Rails 3. This means that you no longer have to manually escape user input with the &lt;code&gt;h&lt;/code&gt; helper, because Rails will automatically escape it for you.&lt;/p&gt;
&lt;p&gt;However, it&amp;#8217;s not as simple as all that. Consider the following:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;erb&quot;&gt;Hello &amp;lt;strong&amp;gt;friends&amp;lt;/strong&amp;gt;!
&amp;nbsp;
&amp;lt;%= tag(:p, some_text) %&amp;gt;
&amp;lt;%= some_text %&amp;gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In the above example, we have a few different scenarios involving HTML tags. First off, Rails should not escape the &lt;code&gt;strong&lt;/code&gt; tag surrounding &amp;#8220;friends&amp;#8221;, because it is unambiguously not user input. Second, Rails should escape &lt;code&gt;some_text&lt;/code&gt; in the &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt; tag, but not the &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt; tag itself. Finally, the &lt;code&gt;some_text&lt;/code&gt; in the final tag should be escaped.&lt;/p&gt;
&lt;p&gt;If &lt;code&gt;some_text&lt;/code&gt; is &lt;code&gt;&amp;lt;script&amp;gt;evil_js&amp;lt;/script&amp;gt;&lt;/code&gt;, the above should output:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;html&quot;&gt;Hello &amp;lt;strong&amp;gt;friends&amp;lt;/strong&amp;gt;!
&amp;nbsp;
&amp;lt;p&amp;gt;&amp;amp;lt;script&amp;amp;gt;evil_js&amp;amp;lt;/script&amp;amp;gt;&amp;lt;/p&amp;gt;
&amp;amp;lt;script&amp;amp;gt;evil_js&amp;amp;lt;/script&amp;amp;gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In order to make this happen, we have introduced a new pervasive concept called &lt;code&gt;html_safe&lt;/code&gt; into Rails applications. If a String is &lt;code&gt;html_safe&lt;/code&gt; (which Rails determines by calling &lt;code&gt;html_safe?&lt;/code&gt; on the String), ERB may insert it unaltered into the output. If it is not safe, ERB must first escape it before inserting it into the output.&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;def&lt;/span&gt; tag&lt;span&gt;&amp;#40;&lt;/span&gt;name, options = &lt;span&gt;nil&lt;/span&gt;, &lt;span&gt;open&lt;/span&gt; = &lt;span&gt;false&lt;/span&gt;, escape = &lt;span&gt;true&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;
  &lt;span&gt;&amp;quot;&amp;lt;#{name}#{tag_options(options, escape) if options}#{open ? &amp;quot;&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;quot; : &amp;quot;&lt;/span&gt; &lt;span&gt;/&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;quot;}&amp;quot;&lt;/span&gt;.&lt;span&gt;html_safe&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Here, Rails creates the tag, telling &lt;code&gt;tag_options&lt;/code&gt; to escape the contents, and then marks the entire body as safe. As a result, the &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;/p&amp;gt;&lt;/code&gt; will emerge unaltered, while Rails will escape the user-supplied content.&lt;/p&gt;
&lt;p&gt;The first implementation of this, in Koz&amp;#8217;s &lt;code&gt;rails-xss&lt;/code&gt; plugin, accomplished the above requirements by adding a new flag to all Strings. Rails, or Rails applications, could mark any String as safe, and Rails overrode &lt;code&gt;+&lt;/code&gt; and &lt;code&gt;&amp;lt;&amp;lt;&lt;/code&gt; to mark the resulting String appropriately based on the input Strings.&lt;/p&gt;
&lt;p&gt;However, during my last performance pass of Rails, I noticed that overriding every String concatenation resulted in quite a bit of performance overhead. Worse, the performance overhead was linear with the number of &lt;code&gt;&amp;lt;%= %&amp;gt;&lt;/code&gt; in a template, so larger templates didn&amp;#8217;t absorb the cost (as they would if the problem was once-per-template).&lt;/p&gt;
&lt;p&gt;Thinking about the problem more, I realized (and confirmed with Koz, Jeremy, and Evan Phoenix of Rubinius), that we could implement roughly the same feature-set in a more performant way with a smaller API impact on Ruby. Because the problem itself is reasonably complex, I won&amp;#8217;t go into a lot of detail about the old implementation, but will explain how you should use the XSS protection with the new implementation. If you already used Koz&amp;#8217;s plugin or are working with the prereleases of Rails, you&amp;#8217;ll notice that today&amp;#8217;s commit changes very little.&lt;/p&gt;
&lt;h2&gt;SafeBuffer&lt;/h2&gt;
&lt;p&gt;In Rails 3, the ERB buffer is an instance of &lt;code&gt;ActiveSupport::SafeBuffer&lt;/code&gt;. &lt;code&gt;SafeBuffer&lt;/code&gt; inherits from String, overriding &lt;code&gt;+&lt;/code&gt;, &lt;code&gt;concat&lt;/code&gt; and &lt;code&gt;&amp;lt;&amp;lt;&lt;/code&gt; so that:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If the other String is safe (another SafeBuffer), the buffer concatenates it directly&lt;/li&gt;
&lt;li&gt;If the other String is unsafe (a plain String), the buffer escapes it first, then concatenates it&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Calling &lt;code&gt;html_safe&lt;/code&gt; on a plain String returns a &lt;code&gt;SafeBuffer&lt;/code&gt; wrapper. Because &lt;code&gt;SafeBuffer&lt;/code&gt; inherits from String, Ruby creates this wrapper extremely efficiently (just sharing the internal &lt;code&gt;char *&lt;/code&gt; storage).&lt;/p&gt;
&lt;p&gt;As a result of this implementation, I was starting to see a lot of the following idiom in the codebase:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;buffer &lt;span&gt;&amp;lt;&amp;lt;&lt;/span&gt; other_string.&lt;span&gt;html_safe&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Here, Rails is creating a new SafeBuffer for the &lt;code&gt;other_string&lt;/code&gt;, then passing it to the &lt;code&gt;&amp;lt;&amp;lt;&lt;/code&gt; method of the original &lt;code&gt;SafeBuffer&lt;/code&gt;, which then checks to see if it is safe. For cases like this, I created a new &lt;code&gt;safe_concat&lt;/code&gt; method on the buffer which uses the original, native &lt;code&gt;concat&lt;/code&gt; method, skipping both the need to create a new &lt;code&gt;SafeBuffer&lt;/code&gt; and the need to check it.&lt;/p&gt;
&lt;p&gt;Similarly, &lt;code&gt;concat&lt;/code&gt; and &lt;code&gt;safe_concat&lt;/code&gt; in ActionView proxy to the &lt;code&gt;concat&lt;/code&gt; and &lt;code&gt;safe_concat&lt;/code&gt; on the buffer itself, so you can use &lt;code&gt;safe_concat&lt;/code&gt; in a helper if you have some HTML you want to concatenate to the buffer with no checks and without escaping.&lt;/p&gt;
&lt;p&gt;ERB uses &lt;code&gt;safe_concat&lt;/code&gt; internally on the parts of the template outside of &lt;code&gt;&amp;lt;% %&amp;gt;&lt;/code&gt; tags, which means that with the changes I pushed today, the XSS protection code adds no performance impact to those cases (basically, all of the plain text in your templates).&lt;/p&gt;
&lt;p&gt;Finally, ERB can now detect the &lt;code&gt;raw&lt;/code&gt; helper at compile time, so if you do something like &lt;code&gt;&amp;lt;%= raw some_stuff %&amp;gt;&lt;/code&gt;, ERB will use &lt;code&gt;safe_concat&lt;/code&gt; internally, skipping the runtime creation of a &lt;code&gt;SafeBuffer&lt;/code&gt; and checks for &lt;code&gt;html_safe&lt;/code&gt;ty.&lt;/p&gt;
&lt;h2&gt;Summary&lt;/h2&gt;
&lt;p&gt;In summary, the XSS protection has the following characteristics:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If a plain String is passed into a &lt;code&gt;&amp;lt;%= %&amp;gt;&lt;/code&gt;, Rails always escapes it&lt;/li&gt;
&lt;li&gt;If a &lt;code&gt;SafeBuffer&lt;/code&gt; is passed into a &lt;code&gt;&amp;lt;%= %&amp;gt;&lt;/code&gt;, Rails does not escape it. To get a &lt;code&gt;SafeBuffer&lt;/code&gt; from a String, call &lt;code&gt;html_safe&lt;/code&gt; on it. The XSS system has a very small performance impact on this case, limited to a guard calling the &lt;code&gt;html_safe?&lt;/code&gt; method&lt;/li&gt;
&lt;li&gt;If you use the &lt;code&gt;raw&lt;/code&gt; helper in a &lt;code&gt;&amp;lt;%= %&amp;gt;&lt;/code&gt;, Rails detects it at &lt;strong&gt;compile-time&lt;/strong&gt; of the template, resulting in zero performance impact from the XSS system on that concatenation&lt;/li&gt;
&lt;li&gt;Rails does not escape any part of a template that is not in an ERB tag. Because Rails handles this at template compile-time, this results in zero performance impact from the XSS system on these concatenations&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In comparison, the initial implementation of XSS impacted each concatenation or &lt;code&gt;+&lt;/code&gt; of String, had impact even if the app used the &lt;code&gt;raw&lt;/code&gt; helper, and even on plain Strings in templates.&lt;/p&gt;
&lt;p&gt;That said, I want to extend personal thanks to Koz for getting the first draft out the door. It worked, demonstrated the concept, and let the community test it out. All in all, an excellent first pass.&lt;/p&gt;

&lt;div class=&quot;feedflare&quot;&gt;
&lt;a href=&quot;http://feeds.feedburner.com/~ff/KatzGotYourTongue?a=PJ7bGS6Dc0U:Gz27f8qZl8g:yIl2AUoC8zA&quot;&gt;&lt;img src=&quot;http://feeds.feedburner.com/~ff/KatzGotYourTongue?d=yIl2AUoC8zA&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src=&quot;http://feeds.feedburner.com/~r/KatzGotYourTongue/~4/PJ7bGS6Dc0U&quot; height=&quot;1&quot; width=&quot;1&quot; /&gt;</content>
		<author>
			<name>wycats</name>
			<uri>http://yehudakatz.com</uri>
		</author>
		<source>
			<title type="html">Katz Got Your Tongue?</title>
			<subtitle type="html">Random Geek-Related Thoughts</subtitle>
			<link rel="self" href="http://yehudakatz.com/feed/atom/"/>
			<id>http://yehudakatz.com/feed/atom/</id>
			<updated>2010-03-13T08:20:07+00:00</updated>
		</source>
	</entry>

	<entry xml:lang="en">
		<title type="html">Corporations Cannot Have Natural Rights&amp;#8230; Duh</title>
		<link href="http://feedproxy.google.com/~r/KatzGotYourTongue/~3/uXupXbSGY2o/"/>
		<id>http://yehudakatz.com/?p=417</id>
		<updated>2010-02-01T06:47:19+00:00</updated>
		<content type="html">&lt;p&gt;When I first read through the Citizens United decision that essentially made it illegal to &amp;#8220;discriminate&amp;#8221; against corporate political speech, I found myself very torn. On the one hand, I&amp;#8217;m an extremely avid supporter of free speech rights, and Kennedy wrote the majority decision to tug at my heartstrings:&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;Premised on mistrust of governmental power, the First Amendment stands against attempts to disfavor certain subjects or viewpoints or to distinguish among different speakers, which may be a means to control content.&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;On the other hand, something felt wrong.&lt;/p&gt;
&lt;p&gt;In the days that followed, I realized the the court had pulled a bait-and-switch, finding a way to split defenders of free speech, preventing a consistent response to the ruling, and resulting in a fair bit of liberal support for the ruling.&lt;/p&gt;
&lt;p&gt;If you read the above quote closely, you can see the lie hiding in plain sight. The court, without drawing much notice, calls corporations &amp;#8220;speakers&amp;#8221; in the context of the First Amendment.&lt;/p&gt;
&lt;p&gt;The problem is simple. The Bill of Rights exists to protect, for the long term, a set of natural rights. If you don&amp;#8217;t believe me, read the writings of the anti-federalists, the guys who argued against the constitution and successfully agitated for the addition of the Bill of Rights in the first place:&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;That insatiable thirst for unconditional controul over our fellow-creatures, and the facility of sounds to convey essentially different ideas, produced the first Bill of Rights ever prefixed to a Frame of Government. The people, although fully sensible that they reserved every tittle of power they did not expressly grant away, yet afraid that the words made use of, to express those rights so granted might convey more than they originally intended, they chose at the same moment to express in different language those rights which the agreement did not include, and which they never designed to part with, endeavoring thereby to prevent any cause for future altercation and the intrusion into society of that doctrine of tacit implication which has been the favorite theme of every tyrant from the origin of all governments to the present day.&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;The language is a big crufty, but the meaning is clear. Without government, people have certain rights. They choose to surrender some of them to the government. The Constitution outlines the powers they have surrendered, and the Bill of Rights outlines specific cases where those powers may not tread.&lt;/p&gt;
&lt;p&gt;However, corporations do not &lt;strong&gt;exist&lt;/strong&gt; outside of government. They are not &amp;#8220;creatures&amp;#8221;. Instead, corporations are creations of the government, and therefore &lt;strong&gt;cannot&lt;/strong&gt; reserve any rights via a bill of rights.&lt;/p&gt;
&lt;p&gt;Looking at the quote from the ruling again:&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;Premised on mistrust of governmental power, the First Amendment stands against attempts to disfavor certain subjects or viewpoints or to distinguish among different speakers, which may be a means to control content.&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;The ruling fundamentally subverts that actual purpose of the Bill of Rights, using language that appeals to defenders of free speech to apply to entities that, by their very definition, the First Amendment cannot cover.&lt;/p&gt;
&lt;p&gt;In short, the Bill of Rights protects the natural rights of people who have already surrendered a fair bit of their autonomy to the federal government. Corporations, by definition, cannot have natural rights, and since they don&amp;#8217;t exist outside of government, cannot have surrendered any autonomy to the government. Therefore, the Bill of Rights, and the First Amendment, cannot apply to them. QED.&lt;/p&gt;

&lt;div class=&quot;feedflare&quot;&gt;
&lt;a href=&quot;http://feeds.feedburner.com/~ff/KatzGotYourTongue?a=uXupXbSGY2o:DImjFxRD8_A:yIl2AUoC8zA&quot;&gt;&lt;img src=&quot;http://feeds.feedburner.com/~ff/KatzGotYourTongue?d=yIl2AUoC8zA&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src=&quot;http://feeds.feedburner.com/~r/KatzGotYourTongue/~4/uXupXbSGY2o&quot; height=&quot;1&quot; width=&quot;1&quot; /&gt;</content>
		<author>
			<name>wycats</name>
			<uri>http://yehudakatz.com</uri>
		</author>
		<source>
			<title type="html">Katz Got Your Tongue?</title>
			<subtitle type="html">Random Geek-Related Thoughts</subtitle>
			<link rel="self" href="http://yehudakatz.com/feed/atom/"/>
			<id>http://yehudakatz.com/feed/atom/</id>
			<updated>2010-03-13T08:20:07+00:00</updated>
		</source>
	</entry>

	<entry xml:lang="en">
		<title type="html">MacRuby 0.5 final is out</title>
		<link href="http://merbist.com/2010/01/31/macruby-0-5-final-is-out/"/>
		<id>http://merbist.com/?p=709</id>
		<updated>2010-02-01T03:13:36+00:00</updated>
		<content type="html">&lt;p&gt;After going through two betas, MacRuby 0.5 final is now released and can be downloaded by clicking on the icon below:&lt;/p&gt;
&lt;p&gt;&lt;a id=&quot;zip&quot; href=&quot;http://macruby.org/downloads.html&quot;&gt;&lt;img src=&quot;http://macruby.org/images/zip.png&quot; alt=&quot;&quot; width=&quot;100&quot; height=&quot;100&quot; /&gt;&lt;br /&gt;
MacRuby 0.5&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Don&amp;#8217;t worry about having MacRuby and Ruby 1.8.x or 1.9 installed, MacRuby is namespaced and won&amp;#8217;t affect your current Ruby installations, just download and launch the installer. (Note: The build was compiled for SnowLeopard only)&lt;/p&gt;
&lt;p&gt;You can read all the &lt;a title=&quot;MacRuby 0.5&quot; href=&quot;http://www.macruby.org/blog/2010/01/31/macruby05.html&quot; target=&quot;_blank&quot;&gt;details of the release on the MacRuby website&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;So what changed since 0.4? Too many things for me to list them here but basically 0.5 uses LLVM to compile code and make MacRuby faster and integrate better with the Obj-c runtime. However since the last beta, here is what changed:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;HotCocoa is now a separate gem&lt;/li&gt;
&lt;li&gt;improved AOT compilation&lt;/li&gt;
&lt;li&gt;Grand Central Dispatch support &amp;#8211; use all your cores without the pain of threads. Read &lt;a title=&quot;MacRuby GCD&quot; href=&quot;http://www.macruby.org/documentation/gcd.html&quot; target=&quot;_blank&quot;&gt;this post&lt;/a&gt; for more info.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;0.5 is a solid release which I consider production ready, I personally wrote a few of small Cocoa apps in MacRuby and everything has been working very well. Of course, I&amp;#8217;m also excited about the new stuff in 0.6 trunk like the debugger previewed a few weeks ago: &lt;a href=&quot;http://merbist.com/2010/01/18/how-to-detect-cylons-with-macruby/&quot; target=&quot;_blank&quot;&gt;http://merbist.com/2010/01/18/how-to-detect-cylons-with-macruby/&lt;/a&gt; but also some drastic changes in the primitive classes that I might cover later on.&lt;/p&gt;
&lt;p&gt;Finally, people are asking if the iPad will be able to run apps running in MacRuby. Unfortunately, the current answer is: no. The two issues with the IPhone/iP*d OS are the lack of Garbage Collector and support for BridgeSupport (needed to define CocoaTouch constants available from MacRuby). However, this matter is being discussed on the mailing list and progress is made by contributors (the core team primarily focusing on the desktop).&lt;/p&gt;
&lt;p&gt;That is going to be an exciting Ruby week as MacRuby 0.5 is now out and Rails 3 beta/RC0 is expected really soon.&lt;/p&gt;</content>
		<author>
			<name>Matt Aimonetti (mattetti)</name>
			<uri>http://merbist.com</uri>
		</author>
		<source>
			<title type="html">Merbist</title>
			<subtitle type="html">Rails 3, Ruby &amp;amp; MacRuby news - tutorials - consulting - training</subtitle>
			<link rel="self" href="http://merbist.com/feed/"/>
			<id>http://merbist.com/feed/</id>
			<updated>2010-03-13T12:20:05+00:00</updated>
		</source>
	</entry>

	<entry xml:lang="en-US">
		<title type="html">How I use Foursquare</title>
		<link href="http://gweezlebur.com/2010/01/31/how-i-use-foursquare.html"/>
		<id>http://gweezlebur.com/2010/01/31/how-i-use-foursquare</id>
		<updated>2010-01-31T06:00:00+00:00</updated>
		<content type="html">&lt;p&gt;There&amp;#8217;s been a little discussion around my Twitter circle about how or why Foursquare is useful. (I&amp;#8217;m glad &lt;a href=&quot;http://twitter.com/jeffhilimire/status/8433187095&quot; title=&quot;Almost all that haven&quot;&gt;I&amp;#8217;m not the only one&lt;/a&gt; who sees the irony of using Twitter to ask that question, since the exact same thing was - and still is - asked about Twitter.)&lt;/p&gt;

&lt;p&gt;I think Foursquare is fun, and has a decent chance of being a long-term player in the location game. Of course I thought that about Dodgeball, too. For the benefit of the Foursquare doubters, here&amp;#8217;s my tips on how to use it &amp;#8220;right&amp;#8221;. YMMV.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Be somewhere with other players&lt;/strong&gt;&lt;br /&gt;Foursquare is only fun with other people. If you&amp;#8217;re the only one checking in near you, you&amp;#8217;ll get bored. So unless you&amp;#8217;re a real early adopter, wait until Foursquare has traction in your city. I live in a town that has very little activity, but I travel to Atlanta a lot; Foursquare is much more fun when I&amp;#8217;m there.&lt;/p&gt;
&lt;/li&gt;

&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Have some friends&lt;/strong&gt;&lt;br /&gt;Not only do you need people near you playing, you need to be friends with some of them. Real friends. Friends you&amp;#8217;d share your cell number with. Friends you would enjoy having drop by your table when you&amp;#8217;re at the same restaurant. Friends you&amp;#8217;d like to fight for control of the neighborhood coffee shop.&lt;/p&gt;
&lt;/li&gt;

&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Don&amp;#8217;t have too many friends&lt;/strong&gt;&lt;br /&gt;Don&amp;#8217;t add everyone from Twitter and Facebook. Be selective. I have maybe 20 Foursquare friends, and all but one are people I know in person.&lt;/p&gt;
&lt;/li&gt;

&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Don&amp;#8217;t automatically post every check-in to Twitter&lt;/strong&gt;&lt;br /&gt;The whole point of Foursquare having a separate set of friends is that you can limit who you share your location with. If you post every update to Twitter, you&amp;#8217;re not only over-sharing to your followers, but you lose that intimacy.&lt;/p&gt;
&lt;/li&gt;

&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Post selective updates to Twitter&lt;/strong&gt;&lt;br /&gt;You can always choose to post a check-in to Twitter on an individual basis, like if you&amp;#8217;re at a cool restaurant you&amp;#8217;ve never been to before, or at a conference. The advantage over just tweeting about it is a 4sq link that includes tips from other people, Yelp links, a map, phone numbers&amp;#8230;much more info than just a geocoded location.&lt;/p&gt;
&lt;/li&gt;

&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Play the game&lt;/strong&gt;&lt;br /&gt;Foursquare&amp;#8217;s game mechanics are fun. Enjoy them. Battle it out for mayoral control of the taco stand. Stake your claim at every doughnut shop within 25 miles. Pick some badges and try to win them.&lt;/p&gt;
&lt;/li&gt;

&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Discover new places, find your friends&lt;/strong&gt;&lt;br /&gt;On the rare occasion that I find myself in a city where I have Foursqaure friends, no plans, and the time and energy for some, I&amp;#8217;ll pull up Foursquare and see if anyone is already out and about. If they are, I may join them, or at least click a button to send a SMS or a tweet and see what&amp;#8217;s going on.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That&amp;#8217;s how I use Foursquare. I get a reasonable amount of pleasure from it. It&amp;#8217;s amusing, and sometimes helps me find something to do. The best way to &amp;#8220;get&amp;#8221; Foursquare is to try it for a while.&lt;/p&gt;</content>
		<author>
			<name>Michael Ivey</name>
			<email>ivey@gweezlebur.com</email>
			<uri>http://gweezlebur.com/</uri>
		</author>
		<source>
			<title type="html">Michael Ivey: blogging since '99</title>
			<link rel="self" href="http://gweezlebur.com/atom.xml"/>
			<id>http://gweezlebur.com/</id>
			<updated>2010-01-31T16:20:06+00:00</updated>
		</source>
	</entry>

	<entry xml:lang="en">
		<title type="html">The Irony of the iPad: A GREAT Day for Open Technologies</title>
		<link href="http://feedproxy.google.com/~r/KatzGotYourTongue/~3/UXcb6GDE_vc/"/>
		<id>http://yehudakatz.com/?p=409</id>
		<updated>2010-01-28T00:03:49+00:00</updated>
		<content type="html">&lt;p&gt;With the announcement of the iPad, the usual suspects have come out decrying a closed, proprietary, fully locked down system.&lt;/p&gt;
&lt;p&gt;For instance, &lt;a href=&quot;http://createdigitalmusic.com/2010/01/27/how-a-great-product-can-be-bad-news-apple-ipad-and-the-closed-mac/&quot;&gt;a story on the top of Hacker News today&lt;/a&gt; says:&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;This is what I asked in January 2007 on this site, shortly after the original iPhone was launched:&lt;/p&gt;
&lt;p&gt;&amp;#8220;1. Will Apple lock down the iPhone, blocking Flash, Java, custom widgets, and open development from its new platform?&lt;/p&gt;
&lt;p&gt;2. Could Apple’s multi-touch patents actually stifle growth of new, interactive displays?&amp;#8221;&lt;/p&gt;
&lt;p&gt;Unfortunately, that turned out to prescient&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;And &lt;a href=&quot;http://www.fsf.org/news/ibad_launch&quot;&gt;the FSF&lt;/a&gt; is out there calling this an unprecedented march of DRM:&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt; With new tablet device, Apple&amp;#8217;s Steve Jobs pushes unprecedented extension of DRM to a new class of general purpose computers&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;It&amp;#8217;s a fair initial reaction. Apple didn&amp;#8217;t build a general-purpose computer as its next entry into the market. Instead, they built a heavily proprietary, locked down device. In order to install an application onto the device, Apple must approve the application.&lt;/p&gt;
&lt;p&gt;I don&amp;#8217;t need to address the merits of the argument against how Apple handles native applications, because it&amp;#8217;s irrelevant. A much, much larger force is at work here.&lt;/p&gt;
&lt;p&gt;With the iPad, Apple has created two platforms. First, they have produced a heavily proprietary, native platform that requires Apple approval and has significant Apple restrictions. But ironically, with their heavy focus on improving the quality of Safari and the HTML standard, they have shipped the iPad with a platform based on open, unencumbered technologies.&lt;/p&gt;
&lt;p&gt;If you haven&amp;#8217;t been paying attention, over the past couple of years, the web platform has gotten offline APIs, improved caching support, local storage (on Safari, that includes an on-device SQLite database accessible through JavaScript), CSS-based animations, and custom, downloadable fonts. Mobile Safari has support for gestures, Geolocation, and hardware-accelerated graphics.&lt;/p&gt;
&lt;p&gt;Additionally, Apple has remained at the forefront of these technologies, literally building some of them for mobile devices (hardware-accelerated animations were built for the iPhone, and by extension, the iPad). The Open Source Webkit project has remained extremely active, and in fact, has only accelerated progress since Apple first released its Native SDK, so Apple&amp;#8217;s &amp;#8220;locked down&amp;#8221; strategy has a very carefully carved out &lt;strong&gt;intentional&lt;/strong&gt; exception.&lt;/p&gt;
&lt;p&gt;Apple even makes it easy to take a web app and put it on the home screen amongst normal apps. When you do this, the iPhone downloads all the assets in the HTML5 cache manifest to make the work better as an offline app. This is how I use Gmail on my iPhone (because Google knows what&amp;#8217;s going on in this space, they leverage new tech in Safari quite well). When Apple rejected Google Voice, Google immediately built a Safari version of the app. &lt;a href=&quot;http://pogue.blogs.nytimes.com/2009/08/06/is-google-voice-a-threat-to-att/&quot;&gt;The way they tell it&lt;/a&gt;:&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt; Already, Google says it is readying a replacement for the Google Voice app that will offer exactly the same features as the rejected app—except that it will take the form of a specialized, iPhone-shaped Web page&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;Ironically, despite claims that not allowing Flash or Java represent a victory for proprietary technologies and a loss for open technologies, they represent quite the opposite. By restricting the web platform on the iPhone and iPad to open, patent-free, technologies, Apple has created a highly desirable market for pure-HTML5 apps. This is, frankly, a win for supporters of open technologies.&lt;/p&gt;

&lt;div class=&quot;feedflare&quot;&gt;
&lt;a href=&quot;http://feeds.feedburner.com/~ff/KatzGotYourTongue?a=UXcb6GDE_vc:-X1IM3n_yoc:yIl2AUoC8zA&quot;&gt;&lt;img src=&quot;http://feeds.feedburner.com/~ff/KatzGotYourTongue?d=yIl2AUoC8zA&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src=&quot;http://feeds.feedburner.com/~r/KatzGotYourTongue/~4/UXcb6GDE_vc&quot; height=&quot;1&quot; width=&quot;1&quot; /&gt;</content>
		<author>
			<name>wycats</name>
			<uri>http://yehudakatz.com</uri>
		</author>
		<source>
			<title type="html">Katz Got Your Tongue?</title>
			<subtitle type="html">Random Geek-Related Thoughts</subtitle>
			<link rel="self" href="http://yehudakatz.com/feed/atom/"/>
			<id>http://yehudakatz.com/feed/atom/</id>
			<updated>2010-03-13T08:20:07+00:00</updated>
		</source>
	</entry>

	<entry xml:lang="en">
		<title type="html">How to detect Cylons with MacRuby</title>
		<link href="http://merbist.com/2010/01/18/how-to-detect-cylons-with-macruby/"/>
		<id>http://merbist.com/?p=696</id>
		<updated>2010-01-19T00:13:37+00:00</updated>
		<content type="html">&lt;p&gt;Over the weekend, MacRuby&amp;#8217;s trunk became version 0.6 and the bug fixing is currently done in both the 0.5 branch and trunk. Based on MacRuby&amp;#8217;s usual release cycle I would expect a 0.5 beta3 or 0.5 final to be released soon so most of the work can be focused on trunk.&lt;/p&gt;
&lt;p&gt;I&amp;#8217;ll let you check on the &lt;a title=&quot;MacRuby's TODO list&quot; href=&quot;http://svn.macosforge.org/repository/ruby/MacRuby/trunk/TODO&quot; target=&quot;_blank&quot;&gt;TODO list&lt;/a&gt; to see what was done in 0.5 and what is in the plan for 0.6.&lt;/p&gt;
&lt;p&gt;However, there is one feature in 0.6 that I know lots of you will just love! The good news is that Laurent already committed a very early version of his work so I figured, I should share the good news with you:&lt;/p&gt;
&lt;h3&gt;Introducing MacRuby&amp;#8217;s debugger!&lt;/h3&gt;
&lt;p&gt;If you were expecting to read: &amp;#8220;a Cylon detector!&amp;#8221;, keep on reading.&lt;/p&gt;
&lt;p&gt;Again, this feature is in really early development since it&amp;#8217;s scheduled for 0.6 and 0.5 final is not out yet. But if you install MacRuby using the &lt;a href=&quot;http://macruby.icoretech.org/&quot; target=&quot;_blank&quot;&gt;nightly builds&lt;/a&gt; or build from trunk, you can already play with the debugger.&lt;/p&gt;
&lt;p&gt;Let me give you a really quick tour of the debugger.&lt;br /&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;http://img.skitch.com/20100118-qn7yuq9h2ce61yxt6kwag9pnbt.jpg&quot; alt=&quot;BSG75Logo posted by Matt Aimonetti&quot; title=&quot;BSG75Logo posted by Matt Aimonetti&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Let&amp;#8217;s imagine that we were given the task to debug the cylon detector written by Gaius Baltar which looks like that:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;characters = &lt;span&gt;%&lt;/span&gt;w&lt;span&gt;&amp;#123;&lt;/span&gt;Adama Apollo Baltar Roslin StarBuck Six&lt;span&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
&lt;span&gt;def&lt;/span&gt; cylon?&lt;span&gt;&amp;#40;&lt;/span&gt;character&lt;span&gt;&amp;#41;&lt;/span&gt;
  &lt;span&gt;false&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
characters.&lt;span&gt;each&lt;/span&gt; &lt;span&gt;do&lt;/span&gt; &lt;span&gt;|&lt;/span&gt;character&lt;span&gt;|&lt;/span&gt;
  &lt;span&gt;if&lt;/span&gt; cylon?&lt;span&gt;&amp;#40;&lt;/span&gt;character&lt;span&gt;&amp;#41;&lt;/span&gt;
    &lt;span&gt;puts&lt;/span&gt; &lt;span&gt;&amp;quot;#{character} is a Cylon!&amp;quot;&lt;/span&gt;
  &lt;span&gt;else&lt;/span&gt;
    &lt;span&gt;puts&lt;/span&gt; &lt;span&gt;&amp;quot;#{character} is not a cylon.&amp;quot;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Here is what happens when I execute the script:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;shell&quot;&gt;$ macruby cylon_detector.rb 
Adama is not a cylon.
Apollo is not a cylon.
Baltar is not a cylon.
Roslin is not a cylon.
StarBuck is not a cylon.
Six is not a cylon.&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The only problem is that we all know that Six is a Cylon, the detector isn&amp;#8217;t working right so let&amp;#8217;s debug it:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;shell&quot;&gt;$ macrubyd cylon_detector.rb
Starting program.
cylon_detector.rb:1&amp;gt; b cylon_detector.rb:8 if character == 'Six'
Added breakpoint 1.
cylon_detector.rb:1&amp;gt; c
Adama is not a cylon.
Apollo is not a cylon.
Baltar is not a cylon.
Roslin is not a cylon.
StarBuck is not a cylon.
cylon_detector.rb:8&amp;gt; p cylon?(character)
=&amp;gt; false
cylon_detector.rb:8&amp;gt; p &amp;quot;This detector is broken!&amp;quot;
=&amp;gt; &amp;quot;This detector is broken!&amp;quot;
cylon_detector.rb:8&amp;gt; p def cylon?(character); character == 'Six'; end
=&amp;gt; nil
cylon_detector.rb:8&amp;gt; p cylon?(character)
=&amp;gt; true
cylon_detector.rb:8&amp;gt; p cylon?('Matt')
=&amp;gt; false
cylon_detector.rb:8&amp;gt; c
Six is a Cylon!
Program exited.&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The first thing we do is to add a conditional breakpoint:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;b cylon_detector.&lt;span&gt;rb&lt;/span&gt;:&lt;span&gt;8&lt;/span&gt; &lt;span&gt;if&lt;/span&gt; character == &lt;span&gt;'Six'&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Basically, the debugger will add a breakpoint at line 8 which will only be active when the value of &amp;#8216;character&amp;#8217; is equal to &amp;#8216;Six&amp;#8217;.&lt;br /&gt;
Now that the breakpoint added, we can continue the program execution and just wait until we reach the defined condition.&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;cylon_detector.&lt;span&gt;rb&lt;/span&gt;:&lt;span&gt;1&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt; c&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Once we reach the breakpoint, we evaluate the result of &amp;#8220;cylon?(character)&amp;#8221; by using the p command. We see that the result is &amp;#8220;false&amp;#8221; when we know for sure that it should be true since the value of the character variable is &amp;#8216;Six&amp;#8217; and she is a cylon. At this point, you might have guessed that somewhat acted as a cylon agent and I pretended to fix the problem by overwriting the &amp;#8220;cylon?&amp;#8221; method:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;shell&quot;&gt;cylon_detector.rb:8&amp;gt; p def cylon?(character); character == 'Six'; end&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now that the method is overwritten, I can check that Six is recognized as being a cylon:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;shell&quot;&gt;cylon_detector.rb:8&amp;gt; p cylon?(character)
=&amp;gt; true&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;and also check that I am not detected a cylon:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;shell&quot;&gt;cylon_detector.rb:8&amp;gt; p cylon?('Matt')
=&amp;gt; false&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;I can now continue the execution of the program and see that Six is detected as a Cylon!&lt;/p&gt;

&lt;p&gt;Of course this is just a very early version of the debugger and we will see lots of improvement in the next few weeks. Who knows someone might even create a GUI for the debugger and/or a Xcode integration. &lt;/p&gt;
&lt;p&gt;Anyway, the point being that MacRuby developers should expect a lot of awesome stuff coming up their way soon. (also be careful about the skin jobs around you, cylon detectors can&amp;#8217;t be trusted!)&lt;/p&gt;</content>
		<author>
			<name>Matt Aimonetti (mattetti)</name>
			<uri>http://merbist.com</uri>
		</author>
		<source>
			<title type="html">Merbist</title>
			<subtitle type="html">Rails 3, Ruby &amp;amp; MacRuby news - tutorials - consulting - training</subtitle>
			<link rel="self" href="http://merbist.com/feed/"/>
			<id>http://merbist.com/feed/</id>
			<updated>2010-03-13T12:20:05+00:00</updated>
		</source>
	</entry>

	<entry xml:lang="en">
		<title type="html">Controlling iTunes with MacRuby</title>
		<link href="http://merbist.com/2010/01/17/controlling-itunes-with-macruby/"/>
		<id>http://merbist.com/?p=684</id>
		<updated>2010-01-18T03:54:19+00:00</updated>
		<content type="html">&lt;p&gt;Since Mac OS X v10.5, Apple added a technology called Scripting Bridge which allows to control and communicate with scriptable applications such as Mail, iChat or iTunes.&lt;/p&gt;
&lt;p&gt;A few weeks back, &lt;a href=&quot;http://merbist.com/2009/12/31/im-new-year-count-down-with-macruby/&quot; target=&quot;_blank&quot;&gt;I showed how to control iChat&lt;/a&gt; with MacRuby. This time I&amp;#8217;m going to show you how to control iTunes.&lt;/p&gt;
&lt;p&gt;Here is a small script that I wrote to wake me up in music every morning.&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;#!/usr/local/bin/macruby&lt;/span&gt;
framework &lt;span&gt;'Foundation'&lt;/span&gt;
framework &lt;span&gt;'ScriptingBridge'&lt;/span&gt;
&amp;nbsp;
itunes = SBApplication.&lt;span&gt;applicationWithBundleIdentifier&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;&amp;quot;com.apple.itunes&amp;quot;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;
load_bridge_support_file &lt;span&gt;'iTunes.bridgesupport'&lt;/span&gt;
itunes.&lt;span&gt;run&lt;/span&gt;
&amp;nbsp;
&lt;span&gt;class&lt;/span&gt; SBElementArray
  &lt;span&gt;def&lt;/span&gt; &lt;span&gt;&amp;#91;&lt;/span&gt;&lt;span&gt;&amp;#93;&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;value&lt;span&gt;&amp;#41;&lt;/span&gt;
    &lt;span&gt;self&lt;/span&gt;.&lt;span&gt;objectWithName&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;value&lt;span&gt;&amp;#41;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
itunes.&lt;span&gt;stop&lt;/span&gt;
playlist = itunes.&lt;span&gt;sources&lt;/span&gt;&lt;span&gt;&amp;#91;&lt;/span&gt;&lt;span&gt;&amp;quot;Library&amp;quot;&lt;/span&gt;&lt;span&gt;&amp;#93;&lt;/span&gt;.&lt;span&gt;userPlaylists&lt;/span&gt;&lt;span&gt;&amp;#91;&lt;/span&gt;&lt;span&gt;&amp;quot;morning&amp;quot;&lt;/span&gt;&lt;span&gt;&amp;#93;&lt;/span&gt;
playlist.&lt;span&gt;playOnce&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;false&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt; &lt;span&gt;if&lt;/span&gt; playlist&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The idea is that I have a Mac Mini streaming music through speakers connected to an AirportExpress in my bedroom.&lt;/p&gt;
&lt;p&gt;Let&amp;#8217;s go through the script quickly.&lt;/p&gt;
&lt;p&gt;We start by loading two frameworks, Foundation and ScriptingBridge.&lt;br /&gt;
Now that we have ScriptingBridge loaded, we can control iTunes. To do that, we use:&lt;br /&gt;
SBApplication.applicationWithBundleIdentifier(&amp;#8220;com.apple.itunes&amp;#8221;)&lt;br /&gt;
We then load a bridgesupport file that contains the enumerated constants from the iTunes scriptable dictionary.&lt;/p&gt;
&lt;p&gt;We make sure iTunes is running by calling #run on the application object.&lt;/p&gt;
&lt;p&gt;Before using iTune scriptable interface, we are making the API a bit nicer, it&amp;#8217;s totally unnecessary but it makes our code look better.&lt;/p&gt;
&lt;p&gt;itunes.sources returns an instance of &lt;a href=&quot;http://developer.apple.com/mac/library/documentation/cocoa/Reference/SBElementArray_Class/SBElementArray/SBElementArray.html &quot; target=&quot;_blank&quot;&gt;SBElementArray&lt;/a&gt; which is not really an array nor a hash.&lt;/p&gt;
&lt;p&gt;The rest of the code is pretty simple, we find the library, find the playlist called &amp;#8216;morning&amp;#8217; and play it if found.&lt;/p&gt;
&lt;p&gt;So you might wonder two things:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;What is the iTunes.bridgesupport file?&lt;/li&gt;
&lt;li&gt;How do you know what methods are available to control iTunes?&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;bridgesupport file&lt;/h4&gt;
&lt;p&gt;The bridgesupport file is important since it defines the required constants.&lt;br /&gt;
Apple provides a metadata generator called gen_bridge_metadata which generates a bridgesupport file.&lt;/p&gt;
&lt;p&gt;Here is what the documentation says:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;shell&quot;&gt;$ man gen_bridge_metadata&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;


&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;shell&quot;&gt;NAME
gen_bridge_metadata -- Objective-C Bridges Metadata Generator
&amp;nbsp;
SYNOPSIS
gen_bridge_metadata [options...] headers...
&amp;nbsp;
DESCRIPTION
gen_bridge_metadata is a tool that generates bridging metadata information for a given framework or set of head-
ers. The Objective-C bridges supported in Mac OS X, such as RubyCocoa (Ruby) and PyObjC (Python), read this
information at runtime.
&amp;nbsp;
Metadata files describe the parts of an Objective-C framework that the bridges cannot automatically handle. These
are primarily the ANSI C elements of the framework -- functions, constants, enumerations, and so on -- but also
include special cases such as functions or methods that accept pointer-like arguments. These special cases must
be manually specified in separate files called exceptions. The gen_bridge_metadata tool can then read in the
exceptions file when it generates the framework metadata.
&amp;nbsp;
The file extension used for metadata files should be .bridgesupport.
&amp;nbsp;
Certain elements, such as inline functions, cannot be described in the metadata files. It is therefore required
to generate a dynamic library in order to make the bridges use them. The gen_bridge_metadata tool can take care
of that for you.
&amp;nbsp;
The file extension for the dynamic libraries should be .dylib.
&amp;nbsp;
You should install metadata files in one of three filesystem locations. For example, for a framework named
MyFramework that is installed as /Library/Frameworks/MyFramework.framework, you can install the
MyFramework.bridgesupport and MyFramework.dylib files in one of the following possible locations, in order of
priority:
&amp;nbsp;
o   /Library/Frameworks/MyFramework/Resources/BridgeSupport
&amp;nbsp;
o   /Library/BridgeSupport
&amp;nbsp;
o   ~/Library/BridgeSupport&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The problem is that we don&amp;#8217;t have a framework or header file to generate a bridgesupport file for.&lt;br /&gt;
So, what we need a header file for iTunes, turns out we have a tool to do that:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;shell&quot;&gt;$ sdef /Applications/iTunes.app | sdp -fh --basename iTunes&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;I won&amp;#8217;t go in the details of what sdef and sdp do, just check their manual page.&lt;br /&gt;
Running the command above will create a iTunes.h which we can use to create a bridgesupport file.&lt;br /&gt;
Here is the generated header file: http://gist.github.com/279657&lt;/p&gt;
&lt;p&gt;Now, let&amp;#8217;s create a bidgesupport file:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;shell&quot;&gt;$ gen_bridge_metadata -c '-I.' iTunes.h &amp;amp;gt; iTunes.bridgesupport&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;An that&amp;#8217;s how we get the bridgesupport file. (see file: &lt;a href=&quot;http://gist.github.com/279698&quot;&gt;http://gist.github.com/279698&lt;/a&gt;)&lt;/p&gt;
&lt;h4&gt;iTunes Documentation&lt;/h4&gt;
&lt;p&gt;The easiest way to understand what&amp;#8217;s available to you is to open iTunes&amp;#8217; dictionary in the AppleScript Editor.&lt;/p&gt;
&lt;p&gt;&lt;img class=&quot;aligncenter&quot; title=&quot;itunes scriptable API&quot; src=&quot;http://img.skitch.com/20100118-fe5c3224jd8xfhciwqxpy4hptc.jpg&quot; alt=&quot;iTunes API by Matt Aimonetti&quot; title=&quot;iTunes API by Matt Aimonetti&quot; width=&quot;511&quot; height=&quot;295&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Otherwise you can study the iTunes.h file.&lt;/p&gt;
&lt;p&gt;I wrote a very dumb parser to give you an idea of the methods and properties available when controlling iTunes via ScriptingBridge, here is the  output:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;shell&quot;&gt;Class: iTunesPrintSettings
Properties:
copies (the number of copies of a document to be printed)
collating (Should printed copies be collated?)
startingPage (the first page of the document to be printed)
endingPage (the last page of the document to be printed)
pagesAcross (number of logical pages laid across a physical page)
pagesDown (number of logical pages laid out down a physical page)
errorHandling (how errors are handled)
requestedPrintTime (the time at which the desktop printer should print the document)
printerFeatures (printer specific options)
faxNumber (for fax number)
targetPrinter (for target printer)
&amp;nbsp;
Method: printPrintDialog:(BOOL)printDialog withProperties:(iTunesPrintSettings *)withProperties kind:(iTunesEKnd)kind theme:(NSString *)theme
Returned: void
Print the specified object(s)
----
Method: close
Returned: void
Close an object
----
Method: delete
Returned: void
Delete an element from an object
----
Method: duplicateTo:(SBObject *)to
Returned: SBObject
Duplicate one or more object(s)
----
Method: exists
Returned: BOOL
Verify if an object exists
----
Method: open
Returned: void
open the specified object(s)
----
Method: playOnce:(BOOL)once
Returned: void
play the current track or the specified track or file.
----
&amp;nbsp;
Class: iTunesApplication
Properties:
currentEncoder (the currently selected encoder (MP3, AIFF, WAV, etc.))
currentEQPreset (the currently selected equalizer preset)
currentPlaylist (the playlist containing the currently targeted track)
currentStreamTitle (the name of the current song in the playing stream (provided by streaming server))
currentStreamURL (the URL of the playing stream or streaming web site (provided by streaming server))
currentTrack (the current targeted track)
currentVisual (the currently selected visual plug-in)
EQEnabled (is the equalizer enabled?)
fixedIndexing (true if all AppleScript track indices should be independent of the play order of the owning playlist.)
frontmost (is iTunes the frontmost application?)
fullScreen (are visuals displayed using the entire screen?)
name (the name of the application)
mute (has the sound output been muted?)
playerPosition (the player’s position within the currently playing track in seconds.)
playerState (is iTunes stopped, paused, or playing?)
selection (the selection visible to the user)
soundVolume (the sound output volume (0 = minimum, 100 = maximum))
version (the version of iTunes)
visualsEnabled (are visuals currently being displayed?)
visualSize (the size of the displayed visual)
&amp;nbsp;
Method: browserWindows
Returned: SBElementArray
----
Method: encoders
Returned: SBElementArray
----
Method: EQPresets
Returned: SBElementArray
----
Method: EQWindows
Returned: SBElementArray
----
Method: playlistWindows
Returned: SBElementArray
----
Method: sources
Returned: SBElementArray
----
Method: visuals
Returned: SBElementArray
----
Method: windows
Returned: SBElementArray
----
Method: printPrintDialog:(BOOL)printDialog withProperties:(iTunesPrintSettings *)withProperties kind:(iTunesEKnd)kind theme:(NSString *)theme
Returned: void
Print the specified object(s)
----
Method: run
Returned: void
run iTunes
----
Method: quit
Returned: void
quit iTunes
----
Method: add:(NSArray *)x to:(SBObject *)to
Returned: iTunesTrack
add one or more files to a playlist
----
Method: backTrack
Returned: void
reposition to beginning of current track or go to previous track if already at start of current track
----
Method: convert:(NSArray *)x
Returned: iTunesTrack
convert one or more files or tracks
----
Method: fastForward
Returned: void
skip forward in a playing track
----
Method: nextTrack
Returned: void
advance to the next track in the current playlist
----
Method: pause
Returned: void
pause playback
----
Method: playOnce:(BOOL)once
Returned: void
play the current track or the specified track or file.
----
Method: playpause
Returned: void
toggle the playing/paused state of the current track
----
Method: previousTrack
Returned: void
return to the previous track in the current playlist
----
Method: resume
Returned: void
disable fast forward/rewind and resume playback, if playing.
----
Method: rewind
Returned: void
skip backwards in a playing track
----
Method: stop
Returned: void
stop playback
----
Method: update
Returned: void
update the specified iPod
----
Method: eject
Returned: void
eject the specified iPod
----
Method: subscribe:(NSString *)x
Returned: void
subscribe to a podcast feed
----
Method: updateAllPodcasts
Returned: void
update all subscribed podcast feeds
----
Method: updatePodcast
Returned: void
update podcast feed
----
Method: openLocation:(NSString *)x
Returned: void
Opens a Music Store or audio stream URL
----
&amp;nbsp;
Class: iTunesItem
Properties:
container (the container of the item)
index (The index of the item in internal application order.)
name (the name of the item)
persistentID (the id of the item as a hexidecimal string. This id does not change over time.)
&amp;nbsp;
Method: id
Returned: NSInteger
the id of the item
----
Method: printPrintDialog:(BOOL)printDialog withProperties:(iTunesPrintSettings *)withProperties kind:(iTunesEKnd)kind theme:(NSString *)theme
Returned: void
Print the specified object(s)
----
Method: close
Returned: void
Close an object
----
Method: delete
Returned: void
Delete an element from an object
----
Method: duplicateTo:(SBObject *)to
Returned: SBObject
Duplicate one or more object(s)
----
Method: exists
Returned: BOOL
Verify if an object exists
----
Method: open
Returned: void
open the specified object(s)
----
Method: playOnce:(BOOL)once
Returned: void
play the current track or the specified track or file.
----
Method: reveal
Returned: void
reveal and select a track or playlist
----
&amp;nbsp;
Class: iTunesPlaylist
Properties:
duration (the total length of all songs (in seconds))
name (the name of the playlist)
parent (folder which contains this playlist (if any))
shuffle (play the songs in this playlist in random order?)
size (the total size of all songs (in bytes))
songRepeat (playback repeat mode)
specialKind (special playlist kind)
time (the length of all songs in MM:SS format)
visible (is this playlist visible in the Source list?)
&amp;nbsp;
Method: tracks
Returned: SBElementArray
----
Method: moveTo:(SBObject *)to
Returned: void
Move playlist(s) to a new location
----
Method: searchFor:(NSString *)for_ only:(iTunesESrA)only
Returned: iTunesTrack
search a playlist for tracks matching the search string. Identical to entering search text in the Search field in iTunes.
----
&amp;nbsp;
Class: iTunesAudioCDPlaylist
Properties:
artist (the artist of the CD)
compilation (is this CD a compilation album?)
composer (the composer of the CD)
discCount (the total number of discs in this CD’s album)
discNumber (the index of this CD disc in the source album)
genre (the genre of the CD)
year (the year the album was recorded/released)
&amp;nbsp;
Method: audioCDTracks
Returned: SBElementArray
----
&amp;nbsp;
Class: iTunesDevicePlaylist
Method: deviceTracks
Returned: SBElementArray
----
&amp;nbsp;
Class: iTunesLibraryPlaylist
Method: fileTracks
Returned: SBElementArray
----
Method: URLTracks
Returned: SBElementArray
----
Method: sharedTracks
Returned: SBElementArray
----
&amp;nbsp;
Class: iTunesRadioTunerPlaylist
Method: URLTracks
Returned: SBElementArray
----
&amp;nbsp;
Class: iTunesSource
Properties:
capacity (the total size of the source if it has a fixed size)
freeSpace (the free space on the source if it has a fixed size)
kind ()
&amp;nbsp;
Method: audioCDPlaylists
Returned: SBElementArray
----
Method: devicePlaylists
Returned: SBElementArray
----
Method: libraryPlaylists
Returned: SBElementArray
----
Method: playlists
Returned: SBElementArray
----
Method: radioTunerPlaylists
Returned: SBElementArray
----
Method: userPlaylists
Returned: SBElementArray
----
Method: update
Returned: void
update the specified iPod
----
Method: eject
Returned: void
eject the specified iPod
----
&amp;nbsp;
Class: iTunesTrack
Properties:
album (the album name of the track)
albumArtist (the album artist of the track)
albumRating (the rating of the album for this track (0 to 100))
albumRatingKind (the rating kind of the album rating for this track)
artist (the artist/source of the track)
bitRate (the bit rate of the track (in kbps))
bookmark (the bookmark time of the track in seconds)
bookmarkable (is the playback position for this track remembered?)
bpm (the tempo of this track in beats per minute)
category (the category of the track)
comment (freeform notes about the track)
compilation (is this track from a compilation album?)
composer (the composer of the track)
databaseID (the common, unique ID for this track. If two tracks in different playlists have the same database ID, they are sharing the same data.)
dateAdded (the date the track was added to the playlist)
objectDescription (the description of the track)
discCount (the total number of discs in the source album)
discNumber (the index of the disc containing this track on the source album)
duration (the length of the track in seconds)
enabled (is this track checked for playback?)
episodeID (the episode ID of the track)
episodeNumber (the episode number of the track)
EQ (the name of the EQ preset of the track)
finish (the stop time of the track in seconds)
gapless (is this track from a gapless album?)
genre (the music/audio genre (category) of the track)
grouping (the grouping (piece) of the track. Generally used to denote movements within a classical work.)
kind (a text description of the track)
longDescription ()
lyrics (the lyrics of the track)
modificationDate (the modification date of the content of this track)
playedCount (number of times this track has been played)
playedDate (the date and time this track was last played)
podcast (is this track a podcast episode?)
rating (the rating of this track (0 to 100))
ratingKind (the rating kind of this track)
releaseDate (the release date of this track)
sampleRate (the sample rate of the track (in Hz))
seasonNumber (the season number of the track)
shufflable (is this track included when shuffling?)
skippedCount (number of times this track has been skipped)
skippedDate (the date and time this track was last skipped)
show (the show name of the track)
sortAlbum (override string to use for the track when sorting by album)
sortArtist (override string to use for the track when sorting by artist)
sortAlbumArtist (override string to use for the track when sorting by album artist)
sortName (override string to use for the track when sorting by name)
sortComposer (override string to use for the track when sorting by composer)
sortShow (override string to use for the track when sorting by show name)
size (the size of the track (in bytes))
start (the start time of the track in seconds)
time (the length of the track in MM:SS format)
trackCount (the total number of tracks on the source album)
trackNumber (the index of the track on the source album)
unplayed (is this track unplayed?)
videoKind (kind of video track)
volumeAdjustment (relative volume adjustment of the track (-100% to 100%))
year (the year the track was recorded/released)
&amp;nbsp;
Method: artworks
Returned: SBElementArray
----
&amp;nbsp;
Class: iTunesFileTrack
Properties:
location (the location of the file represented by this track)
&amp;nbsp;
Method: refresh
Returned: void
update file track information from the current information in the track’s file
----
&amp;nbsp;
Class: iTunesURLTrack
Properties:
address (the URL for this track)
&amp;nbsp;
Method: download
Returned: void
download podcast episode
----
&amp;nbsp;
Class: iTunesUserPlaylist
Properties:
shared (is this playlist shared?)
smart (is this a Smart Playlist?)
&amp;nbsp;
Method: fileTracks
Returned: SBElementArray
----
Method: URLTracks
Returned: SBElementArray
----
Method: sharedTracks
Returned: SBElementArray
----&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;</content>
		<author>
			<name>Matt Aimonetti (mattetti)</name>
			<uri>http://merbist.com</uri>
		</author>
		<source>
			<title type="html">Merbist</title>
			<subtitle type="html">Rails 3, Ruby &amp;amp; MacRuby news - tutorials - consulting - training</subtitle>
			<link rel="self" href="http://merbist.com/feed/"/>
			<id>http://merbist.com/feed/</id>
			<updated>2010-03-13T12:20:05+00:00</updated>
		</source>
	</entry>

	<entry xml:lang="en">
		<title type="html">jQuery 1.4 and Malformed JSON</title>
		<link href="http://feedproxy.google.com/~r/KatzGotYourTongue/~3/E_tel3Oe_XE/"/>
		<id>http://yehudakatz.com/?p=405</id>
		<updated>2010-01-15T09:40:17+00:00</updated>
		<content type="html">&lt;p&gt;Today, we released jQuery 1.4, a mostly backward compatible release with a few minor quirks. One of these quirks is that jQuery now uses the native JSON parser where available, rejecting malformed JSON.&lt;/p&gt;
&lt;p&gt;This probably sounds fine, except that by malformed JSON, we mostly mean:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;javascript&quot;&gt;&lt;span&gt;&amp;#123;&lt;/span&gt;foo&lt;span&gt;:&lt;/span&gt; &lt;span&gt;&amp;quot;bar&amp;quot;&lt;/span&gt;&lt;span&gt;&amp;#125;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Because this is valid JavaScript, people have long assumed it was valid JSON, and a lot of hand-coded JSON is written like this. Thankfully,  JSON automatically generated by Rails does not have this problem, so most of you guys should not have an issue here.&lt;/p&gt;
&lt;p&gt;If you have server-side JSON that can&amp;#8217;t be quickly fixed in the transition to jQuery 1.4, you can use the compatibility plugin, which tries to patch a number of small incompatibilities, or you can use this workaround:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;javascript&quot;&gt;$.&lt;span&gt;ajax&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;&amp;#123;&lt;/span&gt;url&lt;span&gt;:&lt;/span&gt; &lt;span&gt;&amp;quot;/url&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt; 
  dataType&lt;span&gt;:&lt;/span&gt; &lt;span&gt;&amp;quot;json&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;
  success&lt;span&gt;:&lt;/span&gt; &lt;span&gt;function&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;json&lt;span&gt;&amp;#41;&lt;/span&gt; &lt;span&gt;&amp;#123;&lt;/span&gt;
    &lt;span&gt;// do something with json&lt;/span&gt;
  &lt;span&gt;&amp;#125;&lt;/span&gt;
&lt;span&gt;&amp;#125;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;
&amp;nbsp;
&lt;span&gt;// becomes&lt;/span&gt;
&amp;nbsp;
$.&lt;span&gt;ajax&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;&amp;#123;&lt;/span&gt;url&lt;span&gt;:&lt;/span&gt; &lt;span&gt;&amp;quot;/url&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;
  dataType&lt;span&gt;:&lt;/span&gt; &lt;span&gt;&amp;quot;text&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;
  success&lt;span&gt;:&lt;/span&gt; &lt;span&gt;function&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;text&lt;span&gt;&amp;#41;&lt;/span&gt; &lt;span&gt;&amp;#123;&lt;/span&gt;
    json &lt;span&gt;=&lt;/span&gt; &lt;span&gt;eval&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;&amp;quot;(&amp;quot;&lt;/span&gt; &lt;span&gt;+&lt;/span&gt; text &lt;span&gt;+&lt;/span&gt; &lt;span&gt;&amp;quot;)&amp;quot;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;
    &lt;span&gt;// do something with JSON&lt;/span&gt;
  &lt;span&gt;&amp;#125;&lt;/span&gt;
&lt;span&gt;&amp;#125;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Essentially, this is what jQuery used to do to evaluate JSON, so with these small changes you can get the old behavior.&lt;/p&gt;
&lt;p&gt;Again, though, you should probably be modifying your busted server-side code to stop sending malformed JSON!&lt;/p&gt;

&lt;div class=&quot;feedflare&quot;&gt;
&lt;a href=&quot;http://feeds.feedburner.com/~ff/KatzGotYourTongue?a=E_tel3Oe_XE:oi5sDCFMQGc:yIl2AUoC8zA&quot;&gt;&lt;img src=&quot;http://feeds.feedburner.com/~ff/KatzGotYourTongue?d=yIl2AUoC8zA&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src=&quot;http://feeds.feedburner.com/~r/KatzGotYourTongue/~4/E_tel3Oe_XE&quot; height=&quot;1&quot; width=&quot;1&quot; /&gt;</content>
		<author>
			<name>wycats</name>
			<uri>http://yehudakatz.com</uri>
		</author>
		<source>
			<title type="html">Katz Got Your Tongue?</title>
			<subtitle type="html">Random Geek-Related Thoughts</subtitle>
			<link rel="self" href="http://yehudakatz.com/feed/atom/"/>
			<id>http://yehudakatz.com/feed/atom/</id>
			<updated>2010-03-13T08:20:07+00:00</updated>
		</source>
	</entry>

	<entry xml:lang="en">
		<title type="html">ActiveModel: Make Any Ruby Object Feel Like ActiveRecord</title>
		<link href="http://feedproxy.google.com/~r/KatzGotYourTongue/~3/VF-vv4RQbUw/"/>
		<id>http://yehudakatz.com/?p=401</id>
		<updated>2010-01-11T05:56:58+00:00</updated>
		<content type="html">&lt;p&gt;Rails 2.3 has a ton of really nice functionality locked up in monolithic components. I&amp;#8217;ve posted quite a bit about how we&amp;#8217;ve opened up a lot of that functionality in ActionPack, making it easier to reuse the router, dispatcher, and individual parts of ActionController. ActiveModel is another way we&amp;#8217;ve exposed useful functionality to you in Rails 3.&lt;/p&gt;
&lt;h2&gt;Before I Begin, The ActiveModel API&lt;/h2&gt;
&lt;p&gt;Before I begin, there are two major elements to ActiveModel. The first is the ActiveModel API, the interface that models must adhere to in order to gain compatibility with ActionPack&amp;#8217;s helpers. I&amp;#8217;ll be talking more about that soon, but for now, the important thing about the ActiveModel API is that your models can become ActiveModel compliant without using a single line of Rails code.&lt;/p&gt;
&lt;p&gt;In order to help you ensure that your models are compliant, ActiveModel comes with a module called ActiveModel::Lint that you can include into your test cases to test compliance with the API:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;class&lt;/span&gt; LintTest &lt;span&gt;&amp;lt;&lt;/span&gt; &lt;span&gt;ActiveModel::TestCase&lt;/span&gt;
  &lt;span&gt;include&lt;/span&gt; &lt;span&gt;ActiveModel::Lint::Tests&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;class&lt;/span&gt; CompliantModel
    extend &lt;span&gt;ActiveModel::Naming&lt;/span&gt;
&amp;nbsp;
    &lt;span&gt;def&lt;/span&gt; to_model
      &lt;span&gt;self&lt;/span&gt;
    &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
    &lt;span&gt;def&lt;/span&gt; valid?&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;      &lt;span&gt;true&lt;/span&gt; &lt;span&gt;end&lt;/span&gt;
    &lt;span&gt;def&lt;/span&gt; new_record?&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt; &lt;span&gt;true&lt;/span&gt; &lt;span&gt;end&lt;/span&gt;
    &lt;span&gt;def&lt;/span&gt; destroyed?&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;  &lt;span&gt;true&lt;/span&gt; &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
    &lt;span&gt;def&lt;/span&gt; errors
      obj = &lt;span&gt;Object&lt;/span&gt;.&lt;span&gt;new&lt;/span&gt;
      &lt;span&gt;def&lt;/span&gt; obj.&lt;span&gt;&amp;#91;&lt;/span&gt;&lt;span&gt;&amp;#93;&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;key&lt;span&gt;&amp;#41;&lt;/span&gt;         &lt;span&gt;&amp;#91;&lt;/span&gt;&lt;span&gt;&amp;#93;&lt;/span&gt; &lt;span&gt;end&lt;/span&gt;
      &lt;span&gt;def&lt;/span&gt; obj.&lt;span&gt;full_messages&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt; &lt;span&gt;&amp;#91;&lt;/span&gt;&lt;span&gt;&amp;#93;&lt;/span&gt; &lt;span&gt;end&lt;/span&gt;
      obj
    &lt;span&gt;end&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;def&lt;/span&gt; setup
    &lt;span&gt;@model&lt;/span&gt; = CompliantModel.&lt;span&gt;new&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code&gt;ActiveModel::Lint::Tests&lt;/code&gt; provide a series of tests that are run against the &lt;code&gt;@model&lt;/code&gt;, testing for compliance. &lt;/p&gt;
&lt;h2&gt;ActiveModel Modules&lt;/h2&gt;
&lt;p&gt;The second interesting part of ActiveModel is a series of modules provided by ActiveModel that you can use to implement common model functionality on your own Ruby objects. These modules were extracted from ActiveRecord, and are now included in ActiveRecord. &lt;/p&gt;
&lt;p&gt;Because we&amp;#8217;re dogfooding these modules, you can be assured that APIs you bring in to your models will remain consistent with ActiveRecord, and that they&amp;#8217;ll continue to be maintained in future releases of Rails.&lt;/p&gt;
&lt;p&gt;The ActiveModel comes with internationalization baked in, providing an avenue for much better community sharing around translating error messages and the like.&lt;/p&gt;
&lt;h2&gt;The Validations System&lt;/h2&gt;
&lt;p&gt;This was perhaps the most frustrating coupling in ActiveRecord, because it meant that people writing libraries for, say, CouchDB had to choose between painstakingly copying the API over, allowing inconsistencies to creep in, or just inventing a whole new API.&lt;/p&gt;
&lt;p&gt;Validations have a few different elements.&lt;/p&gt;
&lt;p&gt;First, declaring the validations themselves. You&amp;#8217;ve seen the usage before in ActiveRecord:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;class&lt;/span&gt; Person &lt;span&gt;&amp;lt;&lt;/span&gt; &lt;span&gt;ActiveRecord::Base&lt;/span&gt;
  validates_presence_of &lt;span&gt;:first_name&lt;/span&gt;, &lt;span&gt;:last_name&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;To do the same thing for a plain old Ruby object, simply do the following:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;class&lt;/span&gt; Person
  &lt;span&gt;include&lt;/span&gt; &lt;span&gt;ActiveModel::Validations&lt;/span&gt;
&amp;nbsp;
  validates_presence_of &lt;span&gt;:first_name&lt;/span&gt;, &lt;span&gt;:last_name&lt;/span&gt;
&amp;nbsp;
  attr_accessor &lt;span&gt;:first_name&lt;/span&gt;, &lt;span&gt;:last_name&lt;/span&gt;
  &lt;span&gt;def&lt;/span&gt; initialize&lt;span&gt;&amp;#40;&lt;/span&gt;first_name, last_name&lt;span&gt;&amp;#41;&lt;/span&gt;
    &lt;span&gt;@first_name&lt;/span&gt;, &lt;span&gt;@last_name&lt;/span&gt; = first_name, last_name
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The validations system calls &lt;code&gt;read_attribute_for_validation&lt;/code&gt; to get the attribute, but by default, it aliases that method to &lt;code&gt;send&lt;/code&gt;, which supports the standard Ruby attribute system of &lt;code&gt;attr_accessor&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;To use a more custom attribute lookup, you can do:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;class&lt;/span&gt; Person
  &lt;span&gt;include&lt;/span&gt; &lt;span&gt;ActiveModel::Validations&lt;/span&gt;
&amp;nbsp;
  validates_presence_of &lt;span&gt;:first_name&lt;/span&gt;, &lt;span&gt;:last_name&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;def&lt;/span&gt; initialize&lt;span&gt;&amp;#40;&lt;/span&gt;attributes = &lt;span&gt;&amp;#123;&lt;/span&gt;&lt;span&gt;&amp;#125;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;
    &lt;span&gt;@attributes&lt;/span&gt; = attributes
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;def&lt;/span&gt; read_attribute_for_validation&lt;span&gt;&amp;#40;&lt;/span&gt;key&lt;span&gt;&amp;#41;&lt;/span&gt;
    &lt;span&gt;@attributes&lt;/span&gt;&lt;span&gt;&amp;#91;&lt;/span&gt;key&lt;span&gt;&amp;#93;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Let&amp;#8217;s look at what a validator actually is. First of all, the validates_presence_of&lt;/p&gt; method:
&lt;pre&gt;def validates_presence_of(*attr_names)
  validates_with PresenceValidator, _merge_attributes(attr_names)
end&lt;/pre&gt;
&lt;p&gt;You can see that &lt;code&gt;validates_presence_of&lt;/code&gt; is using the more primitive &lt;code&gt;validates_with&lt;/code&gt;, passing it the validator class, merging in &lt;code&gt;{:attributes =&amp;gt; attribute_names}&lt;/code&gt; into the options passed to the validator. Next, the validator itself:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;class&lt;/span&gt; PresenceValidator &lt;span&gt;&amp;lt;&lt;/span&gt; EachValidator
  &lt;span&gt;def&lt;/span&gt; validate&lt;span&gt;&amp;#40;&lt;/span&gt;record&lt;span&gt;&amp;#41;&lt;/span&gt;
    record.&lt;span&gt;errors&lt;/span&gt;.&lt;span&gt;add_on_blank&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;attributes, options&lt;span&gt;&amp;#91;&lt;/span&gt;&lt;span&gt;:message&lt;/span&gt;&lt;span&gt;&amp;#93;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code&gt;EachValidator&lt;/code&gt; that it inherits from validates each attribute with the &lt;code&gt;validate&lt;/code&gt; method. In this case, it adds the error message to the record, only if the attribute is blank. &lt;/p&gt;
&lt;p&gt;The &lt;code&gt;add_on_blank&lt;/code&gt; method does &lt;code&gt;add(attribute, :blank, :default =&amp;gt; custom_message) if value.blank?&lt;/code&gt; (among other things), which is adding the localized &lt;code&gt;:blank&lt;/code&gt; message to the object. If you take a look at the built-in &lt;code&gt;locale/en.yml&lt;/code&gt; looks like:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;yaml&quot;&gt;en:
  errors:
    # The default format use in full error messages.
    format: &amp;quot;{{attribute}} {{message}}&amp;quot;
&amp;nbsp;
    # The values :model, :attribute and :value are always available for interpolation
    # The value :count is available when applicable. Can be used for pluralization.
    messages:
      inclusion: &amp;quot;is not included in the list&amp;quot;
      exclusion: &amp;quot;is reserved&amp;quot;
      invalid: &amp;quot;is invalid&amp;quot;
      confirmation: &amp;quot;doesn't match confirmation&amp;quot;
      accepted: &amp;quot;must be accepted&amp;quot;
      empty: &amp;quot;can't be empty&amp;quot;
      blank: &amp;quot;can't be blank&amp;quot;
      too_long: &amp;quot;is too long (maximum is {{count}} characters)&amp;quot;
      too_short: &amp;quot;is too short (minimum is {{count}} characters)&amp;quot;
      wrong_length: &amp;quot;is the wrong length (should be {{count}} characters)&amp;quot;
      not_a_number: &amp;quot;is not a number&amp;quot;
      greater_than: &amp;quot;must be greater than {{count}}&amp;quot;
      greater_than_or_equal_to: &amp;quot;must be greater than or equal to {{count}}&amp;quot;
      equal_to: &amp;quot;must be equal to {{count}}&amp;quot;
      less_than: &amp;quot;must be less than {{count}}&amp;quot;
      less_than_or_equal_to: &amp;quot;must be less than or equal to {{count}}&amp;quot;
      odd: &amp;quot;must be odd&amp;quot;
      even: &amp;quot;must be even&amp;quot;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;As a result, the error message will read &lt;code&gt;first_name can't be blank&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The Error object is also a part of ActiveModel.&lt;/p&gt;
&lt;h2&gt;Serialization&lt;/h2&gt;
&lt;p&gt;ActiveRecord also comes with default serialization for JSON and XML, allowing you to do things like: &lt;code&gt;@person.to_json(:except =&amp;gt; :comment)&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The main important part of the serialization support is adding general support for specifying the attributes to include across all serializers. That means that you can do &lt;code&gt;@person.to_xml(:except =&amp;gt; :comment)&lt;/code&gt; as well.&lt;/p&gt;
&lt;p&gt;To add serialization support to your own model, you will need to include the serialization module and implement &lt;code&gt;attributes&lt;/code&gt;. Check it out:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;class&lt;/span&gt; Person
  &lt;span&gt;include&lt;/span&gt; &lt;span&gt;ActiveModel::Serialization&lt;/span&gt;
&amp;nbsp;
  attr_accessor &lt;span&gt;:attributes&lt;/span&gt;
  &lt;span&gt;def&lt;/span&gt; initialize&lt;span&gt;&amp;#40;&lt;/span&gt;attributes&lt;span&gt;&amp;#41;&lt;/span&gt;
    &lt;span&gt;@attributes&lt;/span&gt; = attributes
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
&lt;span&gt;p&lt;/span&gt; = Person.&lt;span&gt;new&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;:first_name&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; &lt;span&gt;&amp;quot;Yukihiro&amp;quot;&lt;/span&gt;, &lt;span&gt;:last_name&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; &lt;span&gt;&amp;quot;Matsumoto&amp;quot;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;
&lt;span&gt;p&lt;/span&gt;.&lt;span&gt;to_json&lt;/span&gt; &lt;span&gt;#=&amp;gt; %|{&amp;quot;first_name&amp;quot;: &amp;quot;Yukihiro&amp;quot;, &amp;quot;last_name&amp;quot;: &amp;quot;Matsumoto&amp;quot;}|&lt;/span&gt;
&lt;span&gt;p&lt;/span&gt;.&lt;span&gt;to_json&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;:only&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; &lt;span&gt;:first_name&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt; &lt;span&gt;#=&amp;gt; %|{&amp;quot;first_name&amp;quot;: &amp;quot;Yukihiro&amp;quot;}|&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You can also pass in a &lt;code&gt;:methods&lt;/code&gt; option to specify methods to call for certain attributes that are determined dynamically.&lt;/p&gt;
&lt;p&gt;Here's the Person model with validations and serialization:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;class&lt;/span&gt; Person
  &lt;span&gt;include&lt;/span&gt; &lt;span&gt;ActiveModel::Validations&lt;/span&gt;
  &lt;span&gt;include&lt;/span&gt; &lt;span&gt;ActiveModel::Serialization&lt;/span&gt;
&amp;nbsp;
  validates_presence_of &lt;span&gt;:first_name&lt;/span&gt;, &lt;span&gt;:last_name&lt;/span&gt;
&amp;nbsp;
  attr_accessor &lt;span&gt;:attributes&lt;/span&gt;
  &lt;span&gt;def&lt;/span&gt; initialize&lt;span&gt;&amp;#40;&lt;/span&gt;attributes = &lt;span&gt;&amp;#123;&lt;/span&gt;&lt;span&gt;&amp;#125;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;
    &lt;span&gt;@attributes&lt;/span&gt; = attributes
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;def&lt;/span&gt; read_attribute_for_validation&lt;span&gt;&amp;#40;&lt;/span&gt;key&lt;span&gt;&amp;#41;&lt;/span&gt;
    &lt;span&gt;@attributes&lt;/span&gt;&lt;span&gt;&amp;#91;&lt;/span&gt;key&lt;span&gt;&amp;#93;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2&gt;Others&lt;/h2&gt;
&lt;p&gt;Those are just two of the modules available in ActiveModel. Some others include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;AttributeMethods&lt;/code&gt;: Makes it easy to add attributes that are set like &lt;code&gt;table_name :foo&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Callbacks&lt;/code&gt;: ActiveRecord-style lifecycle callbacks. &lt;/li&gt;
&lt;li&gt;&lt;code&gt;Dirty&lt;/code&gt;: Support for dirty tracking&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Naming&lt;/code&gt;: Default implementations of &lt;code&gt;model.model_name&lt;/code&gt;, which are used by ActionPack (for instance, when you do &lt;code&gt;render :partial =&amp;gt; model&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Observing&lt;/code&gt;: ActiveRecord-style observers&lt;/li&gt;
&lt;li&gt;&lt;code&gt;StateMachine&lt;/code&gt;: A simple state-machine implementation for models&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Translation&lt;/code&gt;: The core translation support&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This mostly reflects the first step of ActiveRecord extractions done by Josh Peek for his Google Summer of Code project last summer. Over time, I expect to see more extractions from ActiveRecord and more abstractions built up around ActiveModel.&lt;/p&gt;
&lt;p&gt;I also expect to see a community building up around things like adding new validators, translations, serializers and more, especially now that they can be reused not only in ActiveRecord, but in MongoMapper, Cassandra Object, and other ORMs that leverage ActiveModel's built-in modules.&lt;/p&gt;

&lt;div class=&quot;feedflare&quot;&gt;
&lt;a href=&quot;http://feeds.feedburner.com/~ff/KatzGotYourTongue?a=VF-vv4RQbUw:pPqqlY7nskg:yIl2AUoC8zA&quot;&gt;&lt;img src=&quot;http://feeds.feedburner.com/~ff/KatzGotYourTongue?d=yIl2AUoC8zA&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src=&quot;http://feeds.feedburner.com/~r/KatzGotYourTongue/~4/VF-vv4RQbUw&quot; height=&quot;1&quot; width=&quot;1&quot; /&gt;</content>
		<author>
			<name>wycats</name>
			<uri>http://yehudakatz.com</uri>
		</author>
		<source>
			<title type="html">Katz Got Your Tongue?</title>
			<subtitle type="html">Random Geek-Related Thoughts</subtitle>
			<link rel="self" href="http://yehudakatz.com/feed/atom/"/>
			<id>http://yehudakatz.com/feed/atom/</id>
			<updated>2010-03-13T08:20:07+00:00</updated>
		</source>
	</entry>

	<entry xml:lang="en">
		<title type="html">Amazon Kindle 2</title>
		<link href="http://merbist.com/2010/01/05/amazon-kindle-2/"/>
		<id>http://merbist.com/?p=669</id>
		<updated>2010-01-06T01:36:12+00:00</updated>
		<content type="html">&lt;p&gt;For Noël, my wife got me a new toy: &lt;a href=&quot;http://www.amazon.com/gp/product/B0015T963C?ie=UTF8&amp;amp;tag=merbist-20&amp;amp;linkCode=as2&amp;amp;camp=1789&amp;amp;creative=390957&amp;amp;creativeASIN=B0015T963C&quot; target=&quot;_blank&quot;&gt;the Amazon Kindle 2&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;a id=&quot;aptureLink_DdJbhnud2V&quot; href=&quot;http://www.amazon.com/gp/product/B0015T963C?tag=merbist-20&quot;&gt;&lt;img title=&quot;Kindle Wireless Reading Device (6&amp;quot; Display, Global Wireless, Latest Generation)&quot; src=&quot;http://placeholder.apture.com/ph/360x280_AmazonProduct/&quot; alt=&quot;&quot; width=&quot;360px&quot; height=&quot;280px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;Kindle vs Nook&lt;/h2&gt;
&lt;p&gt;Before my wife bought what became my new favorite gadget, we checked on the Barnes &amp;amp; Noble Nook.&lt;/p&gt;
&lt;p&gt;&lt;img class=&quot;alignleft&quot; title=&quot;Nook&quot; src=&quot;http://farm5.static.flickr.com/4012/4176062087_704a33b9fd_m.jpg&quot; alt=&quot;&quot; width=&quot;240&quot; height=&quot;180&quot; /&gt;&lt;/p&gt;
&lt;p&gt;The primary problem with the Nook was that it was out of stock. That pushed me to dig deeper and really compare both devices.&lt;/p&gt;
&lt;p&gt;The Nook had some interesting features like wifi, small color screen and the book lending feature.&lt;/p&gt;
&lt;p&gt;However, I don&amp;#8217;t really care about a small, clunky extra color screen since I just want to read books. The wifi is nice&amp;#8230; but what am I going to do with it? Lending books is a cool feature, but this feature isn&amp;#8217;t available for all books and is apparently seriously limited.&lt;/p&gt;
&lt;p&gt;So I decided to focus on what I really needed instead of what I could use.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The main purpose of an e-book is reading, page loading should be fast and the user experience should be at least as good as when reading a &amp;#8216;real&amp;#8217; book.&lt;/li&gt;
&lt;li&gt;I need to be able to have a great choice of books and if possible in multiple languages.&lt;/li&gt;
&lt;li&gt;I would like to be able to read the &lt;a id=&quot;aptureLink_S0pjpCtZuq&quot; href=&quot;http://www.safaribooksonline.com/&quot;&gt;safarionline&lt;/a&gt; books that I can access online.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;From what I read almost everywhere, the nook is slow and a bit buggy (it&amp;#8217;s a first generation after all) and doesn&amp;#8217;t come with a web browser. I also checked on the ebooks available on Amazon and Barnes &amp;amp; Noble. Amazon has way more ebooks and that&amp;#8217;s really what motivated my decision. The other thing is that Amazon is present world wide and I expect them to start selling more ebooks in French and Spanish.&lt;/p&gt;
&lt;p&gt;The other thing that pushed me to buy a Kindle was the fact that I know so many people having one and being so pleased. During a recent trip to San Diego, &lt;a id=&quot;aptureLink_XM9xKzn0qQ&quot; href=&quot;http://www.linkedin.com/in/yehudakatz&quot;&gt;Yehuda Katz&lt;/a&gt; told me about how much he loves his Kindle and he recommended a book to read. I started reading the book on my iPhone (using kindle for iPhone) and was really wanted a bigger screen.&lt;/p&gt;
&lt;h2&gt;Kindle vs iTablet&lt;/h2&gt;
&lt;p&gt;A lot of people asked me why I didn&amp;#8217;t wait for the big announcement of the iTablet because it might be a Kindle killer.&lt;/p&gt;
&lt;p&gt;First, the iTablet is currently just a rumor and if we would believe the rumor, the iTablet will sell around $1,000 vs $250 for a kindle. The tablet will probably do a lot of cool stuff my e-reader can&amp;#8217;t do&amp;#8230; but I don&amp;#8217;t care I chose the kindle because I wanted a simple device to read book and only do that. Actually, the  kindle + iphone combo works great for me. I used to read a lot but because I have been moving so many times I started reading less because I always get rid of my books. Also, when traveling a lot, carrying your books with you is a pain. (especially now that most airlines charge you per bag)&lt;/p&gt;
&lt;h2&gt;Kindle and tech books/documentation.&lt;/h2&gt;
&lt;p&gt;If you want a kindle to read tech books, don&amp;#8217;t get a 6&amp;#8243;, get the kindle DX instead. Even though the 6&amp;#8243; natively supports PDF files, the text is usually too small and the Kindle doesn&amp;#8217;t support zooming (yet).&lt;/p&gt;
&lt;p&gt;&lt;a id=&quot;aptureLink_v8MeHKivTR&quot; href=&quot;http://www.amazon.com/gp/product/B0015TCML0?tag=merbist-20&quot;&gt;&lt;img title=&quot;Kindle DX Wireless Reading Device (9.7&amp;quot; Display, U.S. Wireless, Latest Generation)&quot; src=&quot;http://placeholder.apture.com/ph/360x280_AmazonProduct/&quot; alt=&quot;&quot; width=&quot;360px&quot; height=&quot;280px&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The DX is perfect to read PDFs because the ratio matches the print expectations. I was thinking about converting all the Cocoa docs to a kindle friendly format since the kindle supports search etc.. But the truth is that it&amp;#8217;s not worth it. The display on a 6&amp;#8243; screen is clunky, it just doesn&amp;#8217;t feel right. I didn&amp;#8217;t try any of the pragprog ebooks but something tells me they will just look better on the DX.&lt;/p&gt;
&lt;p&gt;Since I wanted a kindle to read books and not browse tech documentation, I&amp;#8217;m still really happy with 6&amp;#8243;.&lt;/p&gt;
&lt;p&gt;If you also have kindle or are planning on buying one soon, here are two books I&amp;#8217;ve been enjoying reading:&lt;/p&gt;
&lt;div class=&quot;wp-caption alignleft&quot;&gt;&lt;a href=&quot;http://www.amazon.com/Flashforward-ebook/dp/B002NANLCE/ref=sr_1_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1262741423&amp;amp;sr=8-1-catcorr&quot;&gt;&lt;img title=&quot;flashforward&quot; src=&quot;http://ecx.images-amazon.com/images/I/51qntNYMMEL._SL500_AA246_PIkin2,BottomRight,-12,34_AA280_SH20_OU01_.jpg&quot; alt=&quot;&quot; width=&quot;280&quot; height=&quot;280&quot; /&gt;&lt;/a&gt;&lt;p class=&quot;wp-caption-text&quot;&gt;Flashforward by Robert J. Sawyer&lt;/p&gt;&lt;/div&gt;
&lt;div class=&quot;wp-caption alignright&quot;&gt;&lt;a href=&quot;http://www.amazon.com/Talent-Code-Greatness-Grown-ebook/dp/B0026OR1UK/ref=sr_1_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1262741566&amp;amp;sr=1-1&quot;&gt;&lt;img title=&quot;Talent Code&quot; src=&quot;http://ecx.images-amazon.com/images/I/41CluL6Fo9L._SL500_AA246_PIkin2,BottomRight,-13,34_AA280_SH20_OU01_.jpg&quot; alt=&quot;&quot; width=&quot;280&quot; height=&quot;280&quot; /&gt;&lt;/a&gt;&lt;p class=&quot;wp-caption-text&quot;&gt;The Talent Code by Daniel Coyle&lt;/p&gt;&lt;/div&gt;</content>
		<author>
			<name>Matt Aimonetti (mattetti)</name>
			<uri>http://merbist.com</uri>
		</author>
		<source>
			<title type="html">Merbist</title>
			<subtitle type="html">Rails 3, Ruby &amp;amp; MacRuby news - tutorials - consulting - training</subtitle>
			<link rel="self" href="http://merbist.com/feed/"/>
			<id>http://merbist.com/feed/</id>
			<updated>2010-03-13T12:20:05+00:00</updated>
		</source>
	</entry>

	<entry xml:lang="en">
		<title type="html">The Maximal Usage Doctrine for Open Source</title>
		<link href="http://feedproxy.google.com/~r/KatzGotYourTongue/~3/WP0DM5qPt2I/"/>
		<id>http://yehudakatz.com/?p=399</id>
		<updated>2010-01-05T23:36:36+00:00</updated>
		<content type="html">&lt;p&gt;I&amp;#8217;ve worked on a number of open source projects over the past several years (the most prominent being Merb, Ruby on Rails and jQuery) and have begun to form some thoughts about the usage (aka adoption) of open source projects and the practical effects of license styles.&lt;/p&gt;
&lt;h2&gt;The Playing Field&lt;/h2&gt;
&lt;p&gt;There are essentially two kinds of licenses popularly used in open source[1]. &lt;/p&gt;
&lt;p&gt;The type I&amp;#8217;ve worked with most extensively in the BSD or MIT-style license. This license allows unlimited usage, modification, distribution, and commercialization of the source code, with two caveats. First, the copyright notice must be distributed with the source code. Second, to the extent legally enforceable, if you use my MIT-licensed source code, you may not sue me for the effects of using the source.&lt;/p&gt;
&lt;p&gt;The other major license type is called copyleft, and essentially leverages copyright law to insist that any modifications to the original source must themselves be made public. The practical effect of this license could not be effected without government-enforced copyright laws. &lt;/p&gt;
&lt;p&gt;The most popular copyleft license is GPL, which is used by Linux, and which also has viral characteristics. In short, if you link code covered by the GPL into your own code, you are required to open your code as well (if you distribute it). The GPL is not very clear about what this means for more dynamic languages (is requiring a Ruby file &amp;#8220;linking&amp;#8221;?).&lt;/p&gt;
&lt;p&gt;A less viral version of the GPL, the LGPL, requires that any modifications you make to the software itself be released, but drops the &amp;#8220;linking&amp;#8221; requirement.&lt;/p&gt;
&lt;p&gt;Because of the uncertainty surrounding the viral characteristics of the GPL, legal departments in big corporations are very hostile to the GPL license. Software covered by the LGPL license is more likely to get approval by corporate legal, and MIT/BSD licenses are the most well-liked (probably because they don&amp;#8217;t confer any obligations on the corporation).&lt;/p&gt;
&lt;h2&gt;What I Want When I Write Open Source&lt;/h2&gt;
&lt;p&gt;When I work on a serious open source project, like Ruby on Rails or jQuery, I have relatively simple desires.&lt;/p&gt;
&lt;p&gt;One. I want as many people as possible to use the software I am working on. This is probably a selfish, egoist desire, but it may also be a desire to see useful software actually used.&lt;/p&gt;
&lt;p&gt;Two. I want some subset of the many users to be willing to report bugs, and for the varied situations the code is used in to help improve the quality of the software as a result. This is essentially &amp;#8220;given enough eyeballs, all bugs are shallow&amp;#8221;.&lt;/p&gt;
&lt;p&gt;Three. I want people who are interested in improving the software to submit patches. Specifically, I want to receive patches from people who have thought through the problem the bug is trying to fix, and want to receive fewer patches from people who have hacked together a solution just to get it to work. In essence, a high signal-to-noise ratio of patches is more important than a high volume of patches.&lt;/p&gt;
&lt;p&gt;Four. I want people who are interested in improving the software long term to become long-term contributors and eventually committers.&lt;/p&gt;
&lt;p&gt;Of these concerns, numbers two and three are the highest priorities. I want to expose the code to as much real-world pressure as possible, and I want as many high-quality patches to fix those bugs as possible. If I had to choose between those two, I would pick number two: exposing my code to real-world stress is the best way to rapidly ferret out bugs and incorrect design assumptions that I know of.&lt;/p&gt;
&lt;h2&gt;Meeting Those Requirements&lt;/h2&gt;
&lt;p&gt;Starting out with the easiest, my first desire, to have my software used as much as possible, is most easily satisfied by an extremely liberal usage policy. Adding restrictions on the use of software I write reduces its adoption almost by definition.&lt;/p&gt;
&lt;p&gt;Much more importantly, the same can be said about exposing code to real world stresses. By far the most important way to achieve this goal is to make it as easy as possible for as many people as possible to use the code.&lt;/p&gt;
&lt;p&gt;If only 1% of all proprietary users of the source ever report bugs, that&amp;#8217;s 1% of potentially thousands of users, as opposed to 100% of the zero proprietary users who were able to use the software under a more restrictive usage scheme. In practice, this number is much more than 1%, as proprietary users of software experience and report bugs just like open source users do.&lt;/p&gt;
&lt;p&gt;The only real counter-argument to this is that by forcing users to contribute, some number of proprietary users will be forced to become open source users, and their contributions will outweigh the smaller contributions of proprietary users. In practice, proprietary users choose proprietary solutions instead when they are forced to choose between restrictive open source usage schemes and other proprietary software.&lt;/p&gt;
&lt;p&gt;There is also much to be said for exposing open source tools into proprietary environments. &lt;/p&gt;
&lt;p&gt;There is also much to be said for introducing proprietary developers to the open source ecosystem. Proprietary developers have access to things like paid time, access to obscure usage scenarios, and access to markets with low open source penetration that the open source community lacks.&lt;/p&gt;
&lt;p&gt;The next major desire I have while working on open source is a steady stream of high-quality patches. This should be advantage copyleft, because all users of the software are forced to contribute back. However, since copyleft licenses are not used in proprietary environments anyway, the patches to open source projects from those environments under more permissive licenses are much more numerous. Again, even if only a few percent of proprietary users contribute back to the project, that is significantly more contributions than the 100% of zero proprietary users.&lt;/p&gt;
&lt;p&gt;Also importantly, the patches are contributed by much more dedicated users of the software, instead of being force-contributions. I have never heard a team member on an open source project say that inadequate patches are received by permissive-license software, and that this problem would be solved by going to a more restrictive model.&lt;/p&gt;
&lt;p&gt;Finally, I can only speak for jQuery and Rails (and other smaller open source projects I work on), but a large number of new long-term contributors became involved while working on corporate projects, where a permissive license made the decision to use the software in the first place feasible.&lt;/p&gt;
&lt;h2&gt;Lower Barrier to Entry Helps Meet Open Source Goals&lt;/h2&gt;
&lt;p&gt;Regardless of how much of the above argument you agree with, it is clear that copyleft licenses intentionally impose a higher barrier to entry for usage than more permissive licenses.&lt;/p&gt;
&lt;p&gt;For projects that use more permissive licenses, the fact that many proprietary users of their software do not contribute back changes is a feature, not a bug.&lt;/p&gt;
&lt;p&gt;That&amp;#8217;s because we don&amp;#8217;t focus on all of the people who use our software without contributing back. Instead, we focus on all the users, bug reports, patches, and long term contributors we have &lt;strong&gt;gained&lt;/strong&gt; by keeping the barrier to entry as low as possible.&lt;/p&gt;
&lt;p&gt;In the end, the world of ubiquitous open source is here. And we made it without having to resort to coercion or the formation of an entirely self-contained set of software. We made it by building great software and convincing real users that it was better than the alternative.&lt;/p&gt;
&lt;h2&gt;Postscript: Linux&lt;/h2&gt;
&lt;p&gt;Linux is a peculiar example because its license has not impeded its usage much. In part, that is because most users of Linux do not make changes to it, and the linking characteristics of the GPL license rarely come into play.&lt;/p&gt;
&lt;p&gt;In cases like this, I would argue that copyleft licenses are close enough to usage maximization to get most of the benefits of more permissive licenses. However, it&amp;#8217;s not clear to me what benefits the copyleft licenses provide in those (relatively rare) cases.&lt;/p&gt;
&lt;p&gt;[1] There are other kinds of licenses, like the Affero license, which is even more restrictive than the GPL license. I would classify Affero as copyleft.&lt;/p&gt;

&lt;div class=&quot;feedflare&quot;&gt;
&lt;a href=&quot;http://feeds.feedburner.com/~ff/KatzGotYourTongue?a=WP0DM5qPt2I:m1Dpa6Lqy8A:yIl2AUoC8zA&quot;&gt;&lt;img src=&quot;http://feeds.feedburner.com/~ff/KatzGotYourTongue?d=yIl2AUoC8zA&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src=&quot;http://feeds.feedburner.com/~r/KatzGotYourTongue/~4/WP0DM5qPt2I&quot; height=&quot;1&quot; width=&quot;1&quot; /&gt;</content>
		<author>
			<name>wycats</name>
			<uri>http://yehudakatz.com</uri>
		</author>
		<source>
			<title type="html">Katz Got Your Tongue?</title>
			<subtitle type="html">Random Geek-Related Thoughts</subtitle>
			<link rel="self" href="http://yehudakatz.com/feed/atom/"/>
			<id>http://yehudakatz.com/feed/atom/</id>
			<updated>2010-03-13T08:20:07+00:00</updated>
		</source>
	</entry>

	<entry xml:lang="en">
		<title type="html">The Craziest F***ing Bug I&amp;#8217;ve Ever Seen</title>
		<link href="http://feedproxy.google.com/~r/KatzGotYourTongue/~3/WCWILuQlhE0/"/>
		<id>http://yehudakatz.com/?p=395</id>
		<updated>2010-01-03T01:58:33+00:00</updated>
		<content type="html">&lt;p&gt;This afternoon, I was telling a friend about one of my exploits tracking down a pretty crazy &lt;a href=&quot;http://en.wikipedia.org/wiki/Heisenbug#Heisenbug&quot;&gt;heisenbug&lt;/a&gt;, and he said he thought other people would be interested in hearing about it. So let me tell you about it.&lt;/p&gt;
&lt;p&gt;Before you continue, if you&amp;#8217;re not interested in relatively arcane technical details, feel free to skip this post. It&amp;#8217;s here mainly because a friend said he thought people would be interested in it.&lt;/p&gt;
&lt;h2&gt;Our Story Begins&lt;/h2&gt;
&lt;p&gt;Our story begins with a small change in the way Ruby 1.9 handles implicit coercion. The most common case of implicit coercion is in &lt;code&gt;Array#flatten&lt;/code&gt;. The basic logic is that Ruby checks to see if an element of the Array can be itself coerced into an Array, which is then does before flattening.&lt;/p&gt;
&lt;p&gt;There are two steps to the process:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Check to see if the element can be coerced into an Array&lt;/li&gt;
&lt;li&gt;If it can, call to_ary on the element, and repeat the process recursively&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;In Ruby 1.8, the process is essentially the following:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;if&lt;/span&gt; obj.&lt;span&gt;respond_to&lt;/span&gt;?&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;:to_ary&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;
  obj.__send__&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;:to_ary&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;
&lt;span&gt;else&lt;/span&gt;
  obj
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In Ruby 1.9, it was changed to:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;begin&lt;/span&gt;
  obj.__send__&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;:to_ary&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;
&lt;span&gt;rescue&lt;/span&gt; &lt;span&gt;NoMethodError&lt;/span&gt;
  obj
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Of course, the internal code was implemented in C, but you get the idea. This change subtly effects objects that implement &lt;code&gt;method_missing&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;class&lt;/span&gt; MyObject
  &lt;span&gt;def&lt;/span&gt; method_missing&lt;span&gt;&amp;#40;&lt;/span&gt;meth, &lt;span&gt;*&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;
    &lt;span&gt;if&lt;/span&gt; meth == &lt;span&gt;:testing&lt;/span&gt;
      &lt;span&gt;puts&lt;/span&gt; &lt;span&gt;&amp;quot;TESTING&amp;quot;&lt;/span&gt;
    &lt;span&gt;else&lt;/span&gt;
      &lt;span&gt;puts&lt;/span&gt; &lt;span&gt;&amp;quot;Calling Super&amp;quot;&lt;/span&gt;
      &lt;span&gt;super&lt;/span&gt;
    &lt;span&gt;end&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In Ruby 1.8, &lt;code&gt;#flatten&lt;/code&gt; will first call &lt;code&gt;#respond_to?&lt;/code&gt; which returns false and therefore doesn&amp;#8217;t ever trigger the &lt;code&gt;#method_missing&lt;/code&gt;. In Ruby 1.9, &lt;code&gt;#method_missing&lt;/code&gt; will be triggered blindly, and because the call to &lt;code&gt;super&lt;/code&gt; should raise a &lt;code&gt;NoMethodError&lt;/code&gt;, we will essentially get the same result.&lt;/p&gt;
&lt;p&gt;There are some subtle differences here, but for the vast majority of cases, the behavior is identical or close enough to not matter.&lt;/p&gt;
&lt;h2&gt;A Weird Quirk&lt;/h2&gt;
&lt;p&gt;When the above change landed in Ruby 1.9.2&amp;#8217;s head, Rails started experiencing weird, intermittent behavior. As you might know, Rails overrides &lt;code&gt;method_missing&lt;/code&gt; on NilClass to provide a guess about what object you were expecting instead of nil. This feature is called &amp;#8220;whiny nils&amp;#8221; and can be enabled or disabled in Rails applications.&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;def&lt;/span&gt; method_missing&lt;span&gt;&amp;#40;&lt;/span&gt;method, &lt;span&gt;*&lt;/span&gt;args, &lt;span&gt;&amp;amp;&lt;/span&gt;block&lt;span&gt;&amp;#41;&lt;/span&gt;
  &lt;span&gt;if&lt;/span&gt; klass = METHOD_CLASS_MAP&lt;span&gt;&amp;#91;&lt;/span&gt;method&lt;span&gt;&amp;#93;&lt;/span&gt;
    raise_nil_warning_for klass, method, &lt;span&gt;caller&lt;/span&gt;
  &lt;span&gt;else&lt;/span&gt;
    &lt;span&gt;super&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;But sometimes, when nil was inside an Array, and the Array was flattened, the flatten failed with a NameError: &lt;code&gt;undefined local variable or method `to_ary' for nil:NilClass&lt;/code&gt;, which resulted in a failure in the flatten entirely.&lt;/p&gt;
&lt;p&gt;The bug appeared intermittently, apparently unrelated to the code that was calling it. We worked around it by catching the NameError and reraising a NoMethodError, but the existence of the bug was rather baffling.&lt;/p&gt;
&lt;h2&gt;The Bug Reappears&lt;/h2&gt;
&lt;p&gt;When working on bundler, Carl and I saw the bug again. This time, we didn&amp;#8217;t want to let it go. In this case, we had four virtually identical tests with four identical stack traces leading to NoMethodError or NameError seemingly at random. Something didn&amp;#8217;t add up.&lt;/p&gt;
&lt;p&gt;After hunting bugs for a few hours (having dug deeply into Ruby&amp;#8217;s source), I brought in Evan Phoenix (of Rubinius). At first, Evan was baffled as well, but he correctly pointed out that the key to understanding what was going on was the difference between NameError and NoMethodError in Ruby.&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;x = &lt;span&gt;Object&lt;/span&gt;.&lt;span&gt;new&lt;/span&gt;
x.&lt;span&gt;no_method&lt;/span&gt; &lt;span&gt;#=&amp;gt; NoMethodError&lt;/span&gt;
&amp;nbsp;
&lt;span&gt;class&lt;/span&gt; Testing
  &lt;span&gt;def&lt;/span&gt; vcall_no_method
    no_method &lt;span&gt;#=&amp;gt; NameError&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;def&lt;/span&gt; call_no_method
    &lt;span&gt;self&lt;/span&gt;.&lt;span&gt;no_method&lt;/span&gt; &lt;span&gt;#=&amp;gt; NoMethodError&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In essence, when Ruby sees a method call with implicit self (which might be a typo&amp;#8217;ed local variable), it raises a NameError, rather than a NoMethodError.&lt;/p&gt;
&lt;p&gt;However, it still didn&amp;#8217;t add up, because the call to &lt;code&gt;to_ary&lt;/code&gt; was &lt;strong&gt;internal&lt;/strong&gt;, and shouldn&amp;#8217;t have been randomly interpreted as a &lt;code&gt;vcall&lt;/code&gt; (a call to a method with implicit self) or a normal call.&lt;/p&gt;
&lt;h2&gt;Tracking it Down&lt;/h2&gt;
&lt;p&gt;Evan dug deeply into the Ruby source, and found how Ruby was determining whether the call was a &lt;code&gt;vcall&lt;/code&gt; or a regular call.&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;c&quot;&gt;&lt;span&gt;static&lt;/span&gt; &lt;span&gt;inline&lt;/span&gt; VALUE
method_missing&lt;span&gt;&amp;#40;&lt;/span&gt;VALUE obj&lt;span&gt;,&lt;/span&gt; ID id&lt;span&gt;,&lt;/span&gt; &lt;span&gt;int&lt;/span&gt; argc&lt;span&gt;,&lt;/span&gt; &lt;span&gt;const&lt;/span&gt; VALUE &lt;span&gt;*&lt;/span&gt;argv&lt;span&gt;,&lt;/span&gt; &lt;span&gt;int&lt;/span&gt; call_status&lt;span&gt;&amp;#41;&lt;/span&gt;
&lt;span&gt;&amp;#123;&lt;/span&gt;
    VALUE &lt;span&gt;*&lt;/span&gt;nargv&lt;span&gt;,&lt;/span&gt; result&lt;span&gt;,&lt;/span&gt; argv_ary &lt;span&gt;=&lt;/span&gt; &lt;span&gt;0&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;
    rb_thread_t &lt;span&gt;*&lt;/span&gt;th &lt;span&gt;=&lt;/span&gt; GET_THREAD&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;
&amp;nbsp;
    th&lt;span&gt;-&amp;gt;&lt;/span&gt;method_missing_reason &lt;span&gt;=&lt;/span&gt; call_status&lt;span&gt;;&lt;/span&gt;
    th&lt;span&gt;-&amp;gt;&lt;/span&gt;passed_block &lt;span&gt;=&lt;/span&gt; &lt;span&gt;0&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;
    &lt;span&gt;// ...&lt;/span&gt;
&lt;span&gt;&amp;#125;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Before calling the &lt;code&gt;method_missing&lt;/code&gt; method itself, Ruby sets a thread-local variable called &lt;code&gt;call_status&lt;/code&gt; that reflects whether or not the original call was a &lt;code&gt;vcall&lt;/code&gt; or a normal call.&lt;/p&gt;
&lt;p&gt;Upon further examination, Evan discovered that the call to &lt;code&gt;method_missing&lt;/code&gt; in the case of type coercion did not change the thread-local &lt;code&gt;method_missing_reason&lt;/code&gt;. &lt;/p&gt;
&lt;p&gt;As a result, Ruby raised a NoMethodError or NameError based upon whether the &lt;strong&gt;last&lt;/strong&gt; call to &lt;code&gt;method_missing&lt;/code&gt; was a &lt;code&gt;vcall&lt;/code&gt; or regular call.&lt;/p&gt;
&lt;p&gt;The fix was a simple &lt;a href=&quot;http://redmine.ruby-lang.org/repositories/diff/ruby-19/vm_eval.c?rev=26124&quot;&gt;one-line patch&lt;/a&gt;, which allowed us to remove the workaround in ActiveSupport, but it was by far the &lt;strong&gt;craziest f***ing bug I&amp;#8217;ve ever seen&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;feedflare&quot;&gt;
&lt;a href=&quot;http://feeds.feedburner.com/~ff/KatzGotYourTongue?a=WCWILuQlhE0:0C3BD8MKBR4:yIl2AUoC8zA&quot;&gt;&lt;img src=&quot;http://feeds.feedburner.com/~ff/KatzGotYourTongue?d=yIl2AUoC8zA&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src=&quot;http://feeds.feedburner.com/~r/KatzGotYourTongue/~4/WCWILuQlhE0&quot; height=&quot;1&quot; width=&quot;1&quot; /&gt;</content>
		<author>
			<name>wycats</name>
			<uri>http://yehudakatz.com</uri>
		</author>
		<source>
			<title type="html">Katz Got Your Tongue?</title>
			<subtitle type="html">Random Geek-Related Thoughts</subtitle>
			<link rel="self" href="http://yehudakatz.com/feed/atom/"/>
			<id>http://yehudakatz.com/feed/atom/</id>
			<updated>2010-03-13T08:20:07+00:00</updated>
		</source>
	</entry>

	<entry xml:lang="en">
		<title type="html">Spinning up a new Rails app</title>
		<link href="http://feedproxy.google.com/~r/KatzGotYourTongue/~3/9wVeRk3BAsY/"/>
		<id>http://yehudakatz.com/?p=391</id>
		<updated>2010-01-01T03:06:52+00:00</updated>
		<content type="html">&lt;p&gt;So people have been attempting to get a Rails app up and running recently. I also have some apps in development on Rails 3, so I&amp;#8217;ve been experiencing some of the same problems many others have.&lt;/p&gt;
&lt;p&gt;The other night, I worked with &lt;a href=&quot;http://github.com/sferik&quot;&gt;sferik&lt;/a&gt; to start porting &lt;a href=&quot;http://github.com/sferik/merb-admin&quot;&gt;merb-admin&lt;/a&gt; over to Rails. Because this process involved being on edge Rails, we got the process honed to a very simple, small, repeatable process.&lt;/p&gt;
&lt;h2&gt;The Steps&lt;/h2&gt;
&lt;h3&gt;Step 1: Check out Rails&lt;/h3&gt;
&lt;pre&gt;$ git clone git://github.com/rails/rails.git&lt;/pre&gt;
&lt;h3&gt;Step 2: Generate a new app&lt;/h3&gt;
&lt;pre&gt;$ ruby rails/railties/bin/rails new_app
$ cd new_app&lt;/pre&gt;
&lt;h3&gt;Step 3: Edit the app&amp;#8217;s Gemfile&lt;/h3&gt;
&lt;pre&gt;# Add to the top
directory &quot;/path/to/rails&quot;, :glob =&gt; &quot;{*/,}*.gemspec&quot;
git &quot;git://github.com/rails/arel.git&quot;
git &quot;git://github.com/rails/rack.git&quot;&lt;/pre&gt;
&lt;h3&gt;Step 4: Bundle&lt;/h3&gt;
&lt;pre&gt;$ gem bundle&lt;/pre&gt;
&lt;h3&gt;Done&lt;/h3&gt;
&lt;p&gt;Everything should now work: &lt;code&gt;script/server&lt;/code&gt;, &lt;code&gt;script/console&lt;/code&gt;, etc. &lt;/p&gt;
&lt;p&gt;If you want to check your copy of Rails into your app, you can copy it into the app and then change your Gemfile to point to the relative location. &lt;/p&gt;
&lt;p&gt;For instance, if you copy it into vendor/rails, you can make the first line of the Gemfile &lt;code&gt;directory &quot;vendor/rails&quot;, :glob =&amp;gt; =&amp;gt; &quot;{*/,}*.gemspec&quot;&lt;/code&gt;. You&amp;#8217;ll want to run &lt;code&gt;gem bundle&lt;/code&gt; again after changing the Gemfile, of course.&lt;/p&gt;

&lt;div class=&quot;feedflare&quot;&gt;
&lt;a href=&quot;http://feeds.feedburner.com/~ff/KatzGotYourTongue?a=9wVeRk3BAsY:9289Ly8V-f4:yIl2AUoC8zA&quot;&gt;&lt;img src=&quot;http://feeds.feedburner.com/~ff/KatzGotYourTongue?d=yIl2AUoC8zA&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src=&quot;http://feeds.feedburner.com/~r/KatzGotYourTongue/~4/9wVeRk3BAsY&quot; height=&quot;1&quot; width=&quot;1&quot; /&gt;</content>
		<author>
			<name>wycats</name>
			<uri>http://yehudakatz.com</uri>
		</author>
		<source>
			<title type="html">Katz Got Your Tongue?</title>
			<subtitle type="html">Random Geek-Related Thoughts</subtitle>
			<link rel="self" href="http://yehudakatz.com/feed/atom/"/>
			<id>http://yehudakatz.com/feed/atom/</id>
			<updated>2010-03-13T08:20:07+00:00</updated>
		</source>
	</entry>

	<entry xml:lang="en">
		<title type="html">IM new year countdown with MacRuby</title>
		<link href="http://merbist.com/2009/12/31/im-new-year-count-down-with-macruby/"/>
		<id>http://merbist.com/?p=662</id>
		<updated>2009-12-31T18:25:50+00:00</updated>
		<content type="html">&lt;p&gt;Here is the geekiest way I found to wish Happy New Year to my IM contacts:&lt;/p&gt;
&lt;p&gt;&lt;img class=&quot;aligncenter&quot; title=&quot;IM&quot; src=&quot;http://img.skitch.com/20091231-c9t2n9889rxiinux6qsfhqtxfq.jpg&quot; alt=&quot;&quot; width=&quot;279&quot; height=&quot;61&quot; /&gt;&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;framework &lt;span&gt;'ScriptingBridge'&lt;/span&gt;
app = SBApplication.&lt;span&gt;applicationWithBundleIdentifier&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;&amp;quot;com.apple.iChat&amp;quot;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;
original_status = app.&lt;span&gt;statusMessage&lt;/span&gt;
new_year = &lt;span&gt;Time&lt;/span&gt;.&lt;span&gt;mktime&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;2010&lt;/span&gt;, &lt;span&gt;1&lt;/span&gt;, &lt;span&gt;1&lt;/span&gt;, &lt;span&gt;0&lt;/span&gt;, &lt;span&gt;0&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;
&amp;nbsp;
&lt;span&gt;loop&lt;/span&gt; &lt;span&gt;do&lt;/span&gt;
  now = &lt;span&gt;Time&lt;/span&gt;.&lt;span&gt;now&lt;/span&gt;
  time_left = &lt;span&gt;&amp;#40;&lt;/span&gt;new_year &lt;span&gt;-&lt;/span&gt; now&lt;span&gt;&amp;#41;&lt;/span&gt;.&lt;span&gt;ceil&lt;/span&gt;
  &lt;span&gt;if&lt;/span&gt; time_left &lt;span&gt;&amp;gt;&lt;/span&gt; &lt;span&gt;0&lt;/span&gt;
    app.&lt;span&gt;statusMessage&lt;/span&gt; = &lt;span&gt;&amp;quot;#{time_left} seconds left until 2010 (EST)&amp;quot;&lt;/span&gt;
  &lt;span&gt;else&lt;/span&gt;
    app.&lt;span&gt;statusMessage&lt;/span&gt; = &lt;span&gt;&amp;quot;Happy New Year 2010!&amp;quot;&lt;/span&gt;
    &lt;span&gt;exit&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
  &lt;span&gt;sleep&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;1&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;If you are alone at home playing WOW,  you can also trigger iTunes to play a mp3 file with crowd noise and people shouting &amp;#8216;&lt;strong&gt;Happy New Year 2010&lt;/strong&gt;&amp;#8216;!&lt;/p&gt;
&lt;p&gt;&lt;img class=&quot;aligncenter&quot; title=&quot;new year IM&quot; src=&quot;http://img.skitch.com/20091231-c5j9dhhy46t76gh26fu6a26h7c.jpg&quot; alt=&quot;&quot; width=&quot;279&quot; height=&quot;58&quot; /&gt;&lt;/p&gt;</content>
		<author>
			<name>Matt Aimonetti (mattetti)</name>
			<uri>http://merbist.com</uri>
		</author>
		<source>
			<title type="html">Merbist</title>
			<subtitle type="html">Rails 3, Ruby &amp;amp; MacRuby news - tutorials - consulting - training</subtitle>
			<link rel="self" href="http://merbist.com/feed/"/>
			<id>http://merbist.com/feed/</id>
			<updated>2010-03-13T12:20:05+00:00</updated>
		</source>
	</entry>

	<entry xml:lang="en">
		<title type="html">Fun with MacRuby</title>
		<link href="http://merbist.com/2009/12/29/fun-with-macruby/"/>
		<id>http://merbist.com/?p=653</id>
		<updated>2009-12-30T03:09:45+00:00</updated>
		<content type="html">&lt;p&gt;To be ready for 2010, I&amp;#8217;m taking some time off relaxing and spending time with my family in Florida.&lt;/p&gt;
&lt;p&gt;During my free time, I&amp;#8217;ve been reading, catching up on movies and TV shows and worked on the MacRuby book that I am writing for O&amp;#8217;Reilly.&lt;/p&gt;
&lt;p&gt;I wrote a bunch of small apps, played with various APIs and every single time I was amazed by all the goodies Apple makes available to developers. My most recent discovery is very simple but I wanted to share it with you.&lt;/p&gt;
&lt;p&gt;I often type text in English, French and Spanish and I even mix the languages from time to time. SnowLeopard comes with a great spellchecker that auto detects the language I&amp;#8217;m typing in and is most of the time correct. It&amp;#8217;s a very impressive feature and I was wondering if, as a MacRuby developer, I could use one of Apple&amp;#8217;s lib to detect what language is being used.  I dug through the documentation but didn&amp;#8217;t find anything. I started looking at some header files and found the API to use &lt;img src=&quot;http://merbist.com/wp-includes/images/smilies/icon_smile.gif&quot; alt=&quot;:)&quot; class=&quot;wp-smiley&quot; /&gt; &lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;framework &lt;span&gt;'Foundation'&lt;/span&gt;
&lt;span&gt;class&lt;/span&gt; &lt;span&gt;String&lt;/span&gt;
  &lt;span&gt;def&lt;/span&gt; language
    CFStringTokenizerCopyBestStringLanguage&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;self&lt;/span&gt;, CFRangeMake&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;0&lt;/span&gt;, &lt;span&gt;self&lt;/span&gt;.&lt;span&gt;size&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
&lt;span&gt;puts&lt;/span&gt; &lt;span&gt;&amp;quot;Bonne année!&amp;quot;&lt;/span&gt;.&lt;span&gt;language&lt;/span&gt;
&lt;span&gt;# =&amp;amp;gt; &amp;quot;fr&amp;quot;&lt;/span&gt;
&lt;span&gt;puts&lt;/span&gt; &lt;span&gt;&amp;quot;Happy new year!&amp;quot;&lt;/span&gt;.&lt;span&gt;language&lt;/span&gt;
&lt;span&gt;# =&amp;amp;gt; &amp;quot;en&amp;quot;&lt;/span&gt;
&lt;span&gt;puts&lt;/span&gt; &lt;span&gt;&amp;quot;¡Feliz año nuevo!&amp;quot;&lt;/span&gt;.&lt;span&gt;language&lt;/span&gt;
&lt;span&gt;# =&amp;amp;gt; &amp;quot;es&amp;quot;&lt;/span&gt;
&lt;span&gt;puts&lt;/span&gt; &lt;span&gt;&amp;quot;Felice anno nuovo!&amp;quot;&lt;/span&gt;.&lt;span&gt;language&lt;/span&gt;
&lt;span&gt;# =&amp;amp;gt; &amp;quot;it&amp;quot;&lt;/span&gt;
&lt;span&gt;puts&lt;/span&gt; &lt;span&gt;&amp;quot;أعياد سعيدة&amp;quot;&lt;/span&gt;.&lt;span&gt;language&lt;/span&gt;
&lt;span&gt;# =&amp;amp;gt; &amp;quot;ar&amp;quot;&lt;/span&gt;
&lt;span&gt;puts&lt;/span&gt; &lt;span&gt;&amp;quot;明けましておめでとうございます。&amp;quot;&lt;/span&gt;.&lt;span&gt;language&lt;/span&gt;
&lt;span&gt;# =&amp;amp;gt; &amp;quot;ja&amp;quot;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The documentation says that the result is not guaranteed to be accurate and that typically 200-400 characters are required to reliably guess the language of a string. (&lt;a href=&quot;http://developer.apple.com/mac/library/documentation/CoreFoundation/Reference/CFStringTokenizerRef/Reference/reference.html#//apple_ref/c/func/CFStringTokenizerCopyBestStringLanguage&quot; target=&quot;_blank&quot;&gt;CFStringTokenizer Doc&lt;/a&gt;)&lt;/p&gt;
&lt;p&gt;Probably not the most useful piece of code, but really cool none the less &lt;img src=&quot;http://merbist.com/wp-includes/images/smilies/icon_smile.gif&quot; alt=&quot;:)&quot; class=&quot;wp-smiley&quot; /&gt; &lt;/p&gt;
&lt;p&gt;Happy new year!&lt;/p&gt;</content>
		<author>
			<name>Matt Aimonetti (mattetti)</name>
			<uri>http://merbist.com</uri>
		</author>
		<source>
			<title type="html">Merbist</title>
			<subtitle type="html">Rails 3, Ruby &amp;amp; MacRuby news - tutorials - consulting - training</subtitle>
			<link rel="self" href="http://merbist.com/feed/"/>
			<id>http://merbist.com/feed/</id>
			<updated>2010-03-13T12:20:05+00:00</updated>
		</source>
	</entry>

	<entry xml:lang="en">
		<title type="html">The Rails 3 Router: Rack it Up</title>
		<link href="http://yehudakatz.com/2009/12/26/the-rails-3-router-rack-it-up/"/>
		<id>http://yehudakatz.com/?p=387</id>
		<updated>2009-12-26T06:51:15+00:00</updated>
		<content type="html">&lt;p&gt;In my previous post about generic actions in Rails 3, I made reference to significant improvements in the router. Some of those have been covered &lt;a href=&quot;http://rizwanreza.com/2009/12/20/revamped-routes-in-rails-3&quot;&gt;on other blogs&lt;/a&gt;, but the full scope of the improvements hasn&amp;#8217;t yet been covered.&lt;/p&gt;
&lt;p&gt;In this post, I&amp;#8217;ll cover a number of the larger design decisions, as well as specific improvements that have been made. Most of these features were in the Merb router, but the Rails DSL is more fully developed, and the fuller emphasis on Rack is a strong improvement from the Merb approach.&lt;/p&gt;
&lt;h2&gt;Improved DSL&lt;/h2&gt;
&lt;p&gt;While the old &lt;code&gt;map.connect&lt;/code&gt; DSL still works just fine, the new standard DSL is less verbose and more readable.&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;# old way&lt;/span&gt;
&lt;span&gt;ActionController::Routing::Routes&lt;/span&gt;.&lt;span&gt;draw&lt;/span&gt; &lt;span&gt;do&lt;/span&gt; &lt;span&gt;|&lt;/span&gt;map&lt;span&gt;|&lt;/span&gt;
  map.&lt;span&gt;connect&lt;/span&gt; &lt;span&gt;&amp;quot;/main/:id&amp;quot;&lt;/span&gt;, &lt;span&gt;:controller&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; &lt;span&gt;&amp;quot;main&amp;quot;&lt;/span&gt;, &lt;span&gt;:action&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; &lt;span&gt;&amp;quot;home&amp;quot;&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
&lt;span&gt;# new way&lt;/span&gt;
&lt;span&gt;Basecamp::Application&lt;/span&gt;.&lt;span&gt;routes&lt;/span&gt; &lt;span&gt;do&lt;/span&gt;
  match &lt;span&gt;&amp;quot;/main/:id&amp;quot;&lt;/span&gt;, &lt;span&gt;:to&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; &lt;span&gt;&amp;quot;main#home&amp;quot;&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;First, the routes are attached to your application, which is now its own object and used throughout Railties. Second, we no longer need &lt;code&gt;map&lt;/code&gt;, and the new DSL (match/to) is more expressive. Finally, we have a shortcut for controller/action pairs (&lt;code&gt;&quot;main#home&quot;&lt;/code&gt; is &lt;code&gt;{:controller =&amp;gt; &quot;main&quot;, :action =&amp;gt; &quot;home&quot;&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;Another useful shortcut allows you to specify the method more simply than before:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;Basecamp::Application&lt;/span&gt;.&lt;span&gt;routes&lt;/span&gt; &lt;span&gt;do&lt;/span&gt;
  post &lt;span&gt;&amp;quot;/main/:id&amp;quot;&lt;/span&gt;, &lt;span&gt;:to&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; &lt;span&gt;&amp;quot;main#home&amp;quot;&lt;/span&gt;, &lt;span&gt;:as&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; &lt;span&gt;:homepage&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code&gt;:as&lt;/code&gt; in the above example specifies a named route, and creates the &lt;code&gt;homepage_url&lt;/code&gt; et al helpers as in Rails 2.&lt;/p&gt;
&lt;h2&gt;Rack It Up&lt;/h2&gt;
&lt;p&gt;When designing the new router, we all agreed that it should be built first as a standalone piece of functionality, with Rails sugar added on top. As a result, we used &lt;code&gt;rack-mount&lt;/code&gt;, which was built by Josh Peek as a standalone Rack router.&lt;/p&gt;
&lt;p&gt;Internally, the router simply matches requests to a rack endpoint, and knows nothing about controllers or controller semantics. Essentially, the router is designed to work like this:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;Basecamp::Application&lt;/span&gt;.&lt;span&gt;routes&lt;/span&gt; &lt;span&gt;do&lt;/span&gt;
  match &lt;span&gt;&amp;quot;/home&amp;quot;&lt;/span&gt;, &lt;span&gt;:to&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; HomeApp
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This will match requests with the &lt;code&gt;/home&lt;/code&gt; path, and dispatches them to a valid Rack application at HomeApp. This means that dispatching to a Sinatra app is trivial:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;class&lt;/span&gt; HomeApp &lt;span&gt;&amp;lt;&lt;/span&gt; &lt;span&gt;Sinatra::Base&lt;/span&gt;
  get &lt;span&gt;&amp;quot;/&amp;quot;&lt;/span&gt; &lt;span&gt;do&lt;/span&gt;
    &lt;span&gt;&amp;quot;Hello World!&amp;quot;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
&lt;span&gt;Basecamp::Application&lt;/span&gt;.&lt;span&gt;routes&lt;/span&gt; &lt;span&gt;do&lt;/span&gt;
  match &lt;span&gt;&amp;quot;/home&amp;quot;&lt;/span&gt;, &lt;span&gt;:to&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; HomeApp
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The one small piece of the puzzle that might have you wondering at this point is that in the previous section, I showed the usage of &lt;code&gt;:to =&amp;gt; &quot;main#home&quot;&lt;/code&gt;, and now I say that &lt;code&gt;:to&lt;/code&gt; takes a Rack application.&lt;/p&gt;
&lt;p&gt;Another improvement in Rails 3 bridges this gap. In Rails 3, &lt;code&gt;PostsController.action(:index)&lt;/code&gt; returns a fully valid Rack application pointing at the &lt;code&gt;index&lt;/code&gt; action of &lt;code&gt;PostsController&lt;/code&gt;. So &lt;code&gt;main#home&lt;/code&gt; is simply a shortcut for &lt;code&gt;MainController.action(:home)&lt;/code&gt;, and it otherwise is identical to providing a Sinatra application.&lt;/p&gt;
&lt;p&gt;As I posted before, this is also the engine behind &lt;code&gt;match &quot;/foo&quot;, :to =&amp;gt; redirect(&quot;/bar&quot;)&lt;/code&gt;.&lt;/p&gt;
&lt;h2&gt;Expanded Constraints&lt;/h2&gt;
&lt;p&gt;Probably the most common desired improvement to the Rails 2 router has been support for routing based on subdomains. There is currently a plugin called subdomain_routes that implements this functionality as follows:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;ActionController::Routing::Routes&lt;/span&gt;.&lt;span&gt;draw&lt;/span&gt; &lt;span&gt;do&lt;/span&gt; &lt;span&gt;|&lt;/span&gt;map&lt;span&gt;|&lt;/span&gt;
  map.&lt;span&gt;subdomain&lt;/span&gt; &lt;span&gt;:support&lt;/span&gt; &lt;span&gt;do&lt;/span&gt; &lt;span&gt;|&lt;/span&gt;support&lt;span&gt;|&lt;/span&gt;
    support.&lt;span&gt;resources&lt;/span&gt; &lt;span&gt;:tickets&lt;/span&gt;
    ...
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This solves the most common case, but the reality is that this is just one common case. In truth, it should be possible to constrain routes based not just on path segments, method, and subdomain, but also based on any element of the request.&lt;/p&gt;
&lt;p&gt;The Rails 3 router exposes this functionality. Here is how you would constrain requests based on subdomains in Rails 3:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;Basecamp::Application&lt;/span&gt;.&lt;span&gt;routes&lt;/span&gt; &lt;span&gt;do&lt;/span&gt;
  match &lt;span&gt;&amp;quot;/foo/bar&amp;quot;&lt;/span&gt;, &lt;span&gt;:to&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; &lt;span&gt;&amp;quot;foo#bar&amp;quot;&lt;/span&gt;, &lt;span&gt;:constraints&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; &lt;span&gt;&amp;#123;&lt;/span&gt;:subdomain &lt;span&gt;=&amp;gt;&lt;/span&gt; &lt;span&gt;&amp;quot;support&amp;quot;&lt;/span&gt;&lt;span&gt;&amp;#125;&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;These constraints can include path segments as well as any method on &lt;code&gt;ActionDispatch::Request&lt;/code&gt;. You could use a String or a regular expression, so &lt;code&gt;:constraints =&amp;gt; {:subdomain =&amp;gt; /support\d/}&lt;/code&gt; would be valid as well.&lt;/p&gt;
&lt;p&gt;Arbitrary constraints can also be specified in block form, as follows:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;Basecamp::Application&lt;/span&gt;.&lt;span&gt;routes&lt;/span&gt; &lt;span&gt;do&lt;/span&gt;
  constraints&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;:subdomain&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; &lt;span&gt;&amp;quot;support&amp;quot;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt; &lt;span&gt;do&lt;/span&gt;
    match &lt;span&gt;&amp;quot;/foo/bar&amp;quot;&lt;/span&gt;, &lt;span&gt;:to&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; &lt;span&gt;&amp;quot;foo#bar&amp;quot;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Finally, constraints can be specified as objects:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;class&lt;/span&gt; SupportSubdomain
  &lt;span&gt;def&lt;/span&gt; &lt;span&gt;self&lt;/span&gt;.&lt;span&gt;matches&lt;/span&gt;?&lt;span&gt;&amp;#40;&lt;/span&gt;request&lt;span&gt;&amp;#41;&lt;/span&gt;
    request.&lt;span&gt;subdomain&lt;/span&gt; == &lt;span&gt;&amp;quot;support&amp;quot;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
&lt;span&gt;Basecamp::Application&lt;/span&gt;.&lt;span&gt;routes&lt;/span&gt; &lt;span&gt;do&lt;/span&gt;
  constraints&lt;span&gt;&amp;#40;&lt;/span&gt;SupportSubdomain&lt;span&gt;&amp;#41;&lt;/span&gt; &lt;span&gt;do&lt;/span&gt;
    match &lt;span&gt;&amp;quot;/foo/bar&amp;quot;&lt;/span&gt;, &lt;span&gt;:to&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; &lt;span&gt;&amp;quot;foo#bar&amp;quot;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2&gt;Optional Segments&lt;/h2&gt;
&lt;p&gt;In Rails 2.3 and earlier, there were some optional segments. Unfortunately, they were hardcoded names and not controllable. Since we&amp;#8217;re using a generic router, magical optional segment names and semantics would not do. And having exposed support for optional segments in Merb was pretty nice. So we added them.&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;# Rails 2.3&lt;/span&gt;
&lt;span&gt;ActionController::Routing::Routes&lt;/span&gt;.&lt;span&gt;draw&lt;/span&gt; &lt;span&gt;do&lt;/span&gt; &lt;span&gt;|&lt;/span&gt;map&lt;span&gt;|&lt;/span&gt;
  &lt;span&gt;# Note that :action and :id are optional, and&lt;/span&gt;
  &lt;span&gt;# :format is implicit&lt;/span&gt;
  map.&lt;span&gt;connect&lt;/span&gt; &lt;span&gt;&amp;quot;/:controller/:action/:id&amp;quot;&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
&lt;span&gt;# Rails 3&lt;/span&gt;
&lt;span&gt;Basecamp::Application&lt;/span&gt;.&lt;span&gt;routes&lt;/span&gt; &lt;span&gt;do&lt;/span&gt;
  &lt;span&gt;# equivalent&lt;/span&gt;
  match &lt;span&gt;&amp;quot;/:controller(/:action(/:id))(.:format)&amp;quot;&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In Rails 3, we can be explicit about the optional segments, and even nest optional segments. If we want the format to be a prefix path, we can do &lt;code&gt;match &quot;(/:format)/home&quot;&lt;/code&gt; and the format is optional. We can use a similar technique to add an optional company ID prefix or a locale.&lt;/p&gt;
&lt;h2&gt;Pervasive Blocks&lt;/h2&gt;
&lt;p&gt;You may have noticed this already, but as a general rule, if you can specify something as an inline condition, you can also specify it as a block constraint.&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;Basecamp::Application&lt;/span&gt;.&lt;span&gt;routes&lt;/span&gt; &lt;span&gt;do&lt;/span&gt;
  controller &lt;span&gt;:home&lt;/span&gt; &lt;span&gt;do&lt;/span&gt;
    match &lt;span&gt;&amp;quot;/:action&amp;quot;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In the above example, we are not required to specify the controller inline, because we specified it via a block. You can use this for subdomains, controller restrictions, request method (&lt;code&gt;get&lt;/code&gt; etc. take a block). There is also a &lt;code&gt;scope&lt;/code&gt; method that can be used to scope a block of routes under a top-level path:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;Basecamp::Application&lt;/span&gt;.&lt;span&gt;routes&lt;/span&gt; &lt;span&gt;do&lt;/span&gt;
  scope &lt;span&gt;&amp;quot;/home&amp;quot;&lt;/span&gt; &lt;span&gt;do&lt;/span&gt;
    match &lt;span&gt;&amp;quot;/:action&amp;quot;&lt;/span&gt;, &lt;span&gt;:to&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; &lt;span&gt;&amp;quot;homepage&amp;quot;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The above route would match &lt;code&gt;/home/hello/foo&lt;/code&gt; to &lt;code&gt;homepage#foo&lt;/code&gt;.&lt;/p&gt;
&lt;h2&gt;Closing&lt;/h2&gt;
&lt;p&gt;There are additional (substantial) improvements around resources, which I will save for another time, assuming someone else doesn&amp;#8217;t get to it first.&lt;/p&gt;</content>
		<author>
			<name>wycats</name>
			<uri>http://yehudakatz.com</uri>
		</author>
		<source>
			<title type="html">Katz Got Your Tongue?</title>
			<subtitle type="html">Random Geek-Related Thoughts</subtitle>
			<link rel="self" href="http://yehudakatz.com/feed/atom/"/>
			<id>http://yehudakatz.com/feed/atom/</id>
			<updated>2010-03-13T08:20:07+00:00</updated>
		</source>
	</entry>

	<entry xml:lang="en">
		<title type="html">Generic Actions in Rails 3</title>
		<link href="http://yehudakatz.com/2009/12/20/generic-actions-in-rails-3/"/>
		<id>http://yehudakatz.com/?p=379</id>
		<updated>2009-12-21T05:14:18+00:00</updated>
		<content type="html">&lt;p&gt;So Django has an interesting feature called &amp;#8220;generic views&amp;#8221;, which essentially allow you to to render a template with generic code. In Rails, the same feature would be called &amp;#8220;generic actions&amp;#8221; (just a terminology difference).&lt;/p&gt;
&lt;p&gt;This was possible, but somewhat difficult in Rails 2.x, but it&amp;#8217;s a breeze in Rails 3.&lt;/p&gt;
&lt;p&gt;Let&amp;#8217;s take a look at a simple generic view in Django, the &amp;#8220;redirect_to&amp;#8221; view:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;python&quot;&gt;urlpatterns = patterns&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;'django.views.generic.simple'&lt;/span&gt;,
    &lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;'^foo/(?P&amp;lt;id&amp;gt;&lt;span&gt;\d&lt;/span&gt;+)/$'&lt;/span&gt;, &lt;span&gt;'redirect_to'&lt;/span&gt;, &lt;span&gt;&amp;#123;&lt;/span&gt;&lt;span&gt;'url'&lt;/span&gt;: &lt;span&gt;'/bar/%(id)s/'&lt;/span&gt;&lt;span&gt;&amp;#125;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;,
&lt;span&gt;&amp;#41;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This essentially redirects &lt;code&gt;&quot;/foo/&amp;lt;id&amp;gt;&quot;&lt;/code&gt; to &lt;code&gt;&quot;/bar/&amp;lt;id&amp;gt;s/&quot;&lt;/code&gt;. In Rails 2.3, a way to achieve equivalent behavior was to create a generic controller that handled this:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;class&lt;/span&gt; GenericController &lt;span&gt;&amp;lt;&lt;/span&gt; ApplicationController
  &lt;span&gt;def&lt;/span&gt; redirect
    redirect_to&lt;span&gt;&amp;#40;&lt;/span&gt;params&lt;span&gt;&amp;#91;&lt;/span&gt;&lt;span&gt;:url&lt;/span&gt;&lt;span&gt;&amp;#93;&lt;/span&gt; &lt;span&gt;%&lt;/span&gt; params, params&lt;span&gt;&amp;#91;&lt;/span&gt;&lt;span&gt;:options&lt;/span&gt;&lt;span&gt;&amp;#93;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;And then you could use this in your router:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;map.&lt;span&gt;connect&lt;/span&gt; &lt;span&gt;&amp;quot;/foo/:id&amp;quot;&lt;/span&gt;, &lt;span&gt;:controller&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; &lt;span&gt;&amp;quot;generic&amp;quot;&lt;/span&gt;, &lt;span&gt;:action&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; &lt;span&gt;&amp;quot;redirect&amp;quot;&lt;/span&gt;, &lt;span&gt;:url&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; &lt;span&gt;&amp;quot;/bar/%{id}s&amp;quot;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This uses the new Ruby 1.9 interpolation syntax (&amp;#8220;%{first} %{last}&amp;#8221; % {:foo =&gt; &amp;#8220;hello&amp;#8221;, :bar =&gt; &amp;#8220;sir&amp;#8221;} == &amp;#8220;hello sir&amp;#8221;) that has been backported to Ruby 1.8 via ActiveSupport.&lt;/p&gt;
&lt;h2&gt;Better With Rails 3&lt;/h2&gt;
&lt;p&gt;However, this is a bit clumsy, and requires us to have a special controller to handle this (relatively simple) case. It also saddles us with the conceptual overhead of a controller in the router itself.&lt;/p&gt;
&lt;p&gt;Here&amp;#8217;s how you do the same thing in Rails 3:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;match &lt;span&gt;&amp;quot;/foo/:id&amp;quot;&lt;/span&gt;, &lt;span&gt;:to&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; redirect&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;&amp;quot;/bar/%{id}s&amp;quot;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This is built-into Rails 3&amp;#8217;s router, but the way it works is actually pretty cool. The Rails 3 router is conceptually decoupled from Rails itself, and the &lt;code&gt;:to&lt;/code&gt; key points at a Rack endpoint. For instance, the following would be a valid route in Rails 3:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;match &lt;span&gt;&amp;quot;/foo&amp;quot;&lt;/span&gt;, &lt;span&gt;:to&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; &lt;span&gt;proc&lt;/span&gt; &lt;span&gt;&amp;#123;&lt;/span&gt;&lt;span&gt;|&lt;/span&gt;env&lt;span&gt;|&lt;/span&gt; &lt;span&gt;&amp;#91;&lt;/span&gt;&lt;span&gt;200&lt;/span&gt;, &lt;span&gt;&amp;#123;&lt;/span&gt;&lt;span&gt;&amp;#125;&lt;/span&gt;, &lt;span&gt;&amp;#91;&lt;/span&gt;&lt;span&gt;&amp;quot;Hello world&amp;quot;&lt;/span&gt;&lt;span&gt;&amp;#93;&lt;/span&gt;&lt;span&gt;&amp;#93;&lt;/span&gt; &lt;span&gt;&amp;#125;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code&gt;redirect&lt;/code&gt; method simply returns a rack endpoint that knows how to handle the redirection:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;def&lt;/span&gt; redirect&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;*&lt;/span&gt;args, &lt;span&gt;&amp;amp;&lt;/span&gt;block&lt;span&gt;&amp;#41;&lt;/span&gt;
  options = args.&lt;span&gt;last&lt;/span&gt;.&lt;span&gt;is_a&lt;/span&gt;?&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;Hash&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt; ? args.&lt;span&gt;pop&lt;/span&gt; : &lt;span&gt;&amp;#123;&lt;/span&gt;&lt;span&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
  path = args.&lt;span&gt;shift&lt;/span&gt; &lt;span&gt;||&lt;/span&gt; block
  path_proc = path.&lt;span&gt;is_a&lt;/span&gt;?&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;Proc&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt; ? path : &lt;span&gt;proc&lt;/span&gt; &lt;span&gt;&amp;#123;&lt;/span&gt;&lt;span&gt;|&lt;/span&gt;params&lt;span&gt;|&lt;/span&gt; path &lt;span&gt;%&lt;/span&gt; params &lt;span&gt;&amp;#125;&lt;/span&gt;
  status = options&lt;span&gt;&amp;#91;&lt;/span&gt;&lt;span&gt;:status&lt;/span&gt;&lt;span&gt;&amp;#93;&lt;/span&gt; &lt;span&gt;||&lt;/span&gt; &lt;span&gt;301&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;lambda&lt;/span&gt; &lt;span&gt;do&lt;/span&gt; &lt;span&gt;|&lt;/span&gt;env&lt;span&gt;|&lt;/span&gt;
    req = &lt;span&gt;Rack::Request&lt;/span&gt;.&lt;span&gt;new&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;env&lt;span&gt;&amp;#41;&lt;/span&gt;
    params = path_proc.&lt;span&gt;call&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;env&lt;span&gt;&amp;#91;&lt;/span&gt;&lt;span&gt;&amp;quot;action_dispatch.request.path_parameters&amp;quot;&lt;/span&gt;&lt;span&gt;&amp;#93;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;
    url = req.&lt;span&gt;scheme&lt;/span&gt; &lt;span&gt;+&lt;/span&gt; &lt;span&gt;'://'&lt;/span&gt; &lt;span&gt;+&lt;/span&gt; req.&lt;span&gt;host&lt;/span&gt; &lt;span&gt;+&lt;/span&gt; params
    &lt;span&gt;&amp;#91;&lt;/span&gt;status, &lt;span&gt;&amp;#123;&lt;/span&gt;&lt;span&gt;'Location'&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; url, &lt;span&gt;'Content-Type'&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; &lt;span&gt;'text/html'&lt;/span&gt;&lt;span&gt;&amp;#125;&lt;/span&gt;, &lt;span&gt;&amp;#91;&lt;/span&gt;&lt;span&gt;'Moved Permanently'&lt;/span&gt;&lt;span&gt;&amp;#93;&lt;/span&gt;&lt;span&gt;&amp;#93;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;There&amp;#8217;s a few things going on here, but the important part is the last few lines, where the &lt;code&gt;redirect&lt;/code&gt; method returns a valid Rack endpoint. If you look closely at the code, you can see that the following would be valid as well:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;match &lt;span&gt;&amp;quot;/api/v1/:api&amp;quot;&lt;/span&gt;, &lt;span&gt;:to&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; 
  redirect &lt;span&gt;&amp;#123;&lt;/span&gt;&lt;span&gt;|&lt;/span&gt;params&lt;span&gt;|&lt;/span&gt; &lt;span&gt;&amp;quot;/api/v2/#{params[:api].pluralize}&amp;quot;&lt;/span&gt; &lt;span&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
&lt;span&gt;# and&lt;/span&gt;
&amp;nbsp;
match &lt;span&gt;&amp;quot;/api/v1/:api&amp;quot;&lt;/span&gt;, &lt;span&gt;:to&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; 
  redirect&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;:status&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; &lt;span&gt;302&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt; &lt;span&gt;&amp;#123;&lt;/span&gt;&lt;span&gt;|&lt;/span&gt;params&lt;span&gt;|&lt;/span&gt; &lt;span&gt;&amp;quot;/api/v2/#{params[:api].pluralize}&amp;quot;&lt;/span&gt; &lt;span&gt;&amp;#125;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2&gt;Another Generic Action&lt;/h2&gt;
&lt;p&gt;Another nice generic action that Django provides is allowing you to render a template directly without needing an explicit action. It looks like this:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;python&quot;&gt;urlpatterns = patterns&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;'django.views.generic.simple'&lt;/span&gt;,
    &lt;span&gt;&amp;#40;&lt;/span&gt;r&lt;span&gt;'^foo/$'&lt;/span&gt;,             &lt;span&gt;'direct_to_template'&lt;/span&gt;, &lt;span&gt;&amp;#123;&lt;/span&gt;&lt;span&gt;'template'&lt;/span&gt;: &lt;span&gt;'foo_index.html'&lt;/span&gt;&lt;span&gt;&amp;#125;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;,
    &lt;span&gt;&amp;#40;&lt;/span&gt;r&lt;span&gt;'^foo/(?P&amp;lt;id&amp;gt;&lt;span&gt;\d&lt;/span&gt;+)/$'&lt;/span&gt;, &lt;span&gt;'direct_to_template'&lt;/span&gt;, &lt;span&gt;&amp;#123;&lt;/span&gt;&lt;span&gt;'template'&lt;/span&gt;: &lt;span&gt;'foo_detail.html'&lt;/span&gt;&lt;span&gt;&amp;#125;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;,
&lt;span&gt;&amp;#41;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This provides a special mechanism for rendering a template directly from the Django router. Again, this could be implemented by creating a special controller in Rails 2 and used as follows:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;class&lt;/span&gt; GenericController &lt;span&gt;&amp;lt;&lt;/span&gt; ApplicationController
  &lt;span&gt;def&lt;/span&gt; direct_to_template
    render&lt;span&gt;&amp;#40;&lt;/span&gt;params&lt;span&gt;&amp;#91;&lt;/span&gt;&lt;span&gt;:options&lt;/span&gt;&lt;span&gt;&amp;#93;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
&lt;span&gt;# Router&lt;/span&gt;
map.&lt;span&gt;connect&lt;/span&gt; &lt;span&gt;&amp;quot;/foo&amp;quot;&lt;/span&gt;, &lt;span&gt;:controller&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; &lt;span&gt;&amp;quot;generic&amp;quot;&lt;/span&gt;, &lt;span&gt;:action&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; &lt;span&gt;&amp;quot;direct_to_template&amp;quot;&lt;/span&gt;, &lt;span&gt;:options&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; &lt;span&gt;&amp;#123;&lt;/span&gt;:template &lt;span&gt;=&amp;gt;&lt;/span&gt; &lt;span&gt;&amp;quot;foo_detail&amp;quot;&lt;/span&gt;&lt;span&gt;&amp;#125;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2&gt;A Prettier API&lt;/h2&gt;
&lt;p&gt;A nicer way to do this would be something like this:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;match &lt;span&gt;&amp;quot;/foo&amp;quot;&lt;/span&gt;, &lt;span&gt;:to&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; render&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;&amp;quot;foo&amp;quot;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;For the sake of clarity, let&amp;#8217;s say that directly rendered templates will come out of &lt;code&gt;app/views/direct&lt;/code&gt; unless otherwise specified. Also, let&amp;#8217;s say that the render method should work identically to the render method used in Rails controllers themselves, so that &lt;code&gt;render :template =&amp;gt; &quot;foo&quot;, :status =&amp;gt; 201, :content_type =&amp;gt; Mime::JSON&lt;/code&gt; et al will work as expected.&lt;/p&gt;
&lt;p&gt;In order to make this work, we&amp;#8217;ll use &lt;code&gt;ActionController::Metal&lt;/code&gt;, which exposes a Rack-compatible object with access to all of the powers of a full &lt;code&gt;ActionController::Base&lt;/code&gt; object.&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;class&lt;/span&gt; RenderDirectly &lt;span&gt;&amp;lt;&lt;/span&gt; &lt;span&gt;ActionController::Metal&lt;/span&gt;
  &lt;span&gt;include&lt;/span&gt; &lt;span&gt;ActionController::Rendering&lt;/span&gt;
  &lt;span&gt;include&lt;/span&gt; &lt;span&gt;ActionController::Layouts&lt;/span&gt;
&amp;nbsp;
  append_view_path Rails.&lt;span&gt;root&lt;/span&gt;.&lt;span&gt;join&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;&amp;quot;app&amp;quot;&lt;/span&gt;, &lt;span&gt;&amp;quot;views&amp;quot;&lt;/span&gt;, &lt;span&gt;&amp;quot;direct&amp;quot;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;
  append_view_path Rails.&lt;span&gt;root&lt;/span&gt;.&lt;span&gt;join&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;&amp;quot;app&amp;quot;&lt;/span&gt;, &lt;span&gt;&amp;quot;views&amp;quot;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;
&amp;nbsp;
  layout &lt;span&gt;&amp;quot;application&amp;quot;&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;def&lt;/span&gt; index
    render &lt;span&gt;*&lt;/span&gt;env&lt;span&gt;&amp;#91;&lt;/span&gt;&lt;span&gt;&amp;quot;generic_views.render_args&amp;quot;&lt;/span&gt;&lt;span&gt;&amp;#93;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
&lt;span&gt;module&lt;/span&gt; GenericActions
  &lt;span&gt;module&lt;/span&gt; Render
    &lt;span&gt;def&lt;/span&gt; render&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;*&lt;/span&gt;args&lt;span&gt;&amp;#41;&lt;/span&gt;
      app = RenderDirectly.&lt;span&gt;action&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;:index&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;
      &lt;span&gt;lambda&lt;/span&gt; &lt;span&gt;do&lt;/span&gt; &lt;span&gt;|&lt;/span&gt;env&lt;span&gt;|&lt;/span&gt;
        env&lt;span&gt;&amp;#91;&lt;/span&gt;&lt;span&gt;&amp;quot;generic_views.render_args&amp;quot;&lt;/span&gt;&lt;span&gt;&amp;#93;&lt;/span&gt; = args
        app.&lt;span&gt;call&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;env&lt;span&gt;&amp;#41;&lt;/span&gt;
      &lt;span&gt;end&lt;/span&gt;
    &lt;span&gt;end&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The trick here is that we&amp;#8217;re subclassing &lt;code&gt;ActionController::Metal&lt;/code&gt; and pulling in just &lt;code&gt;Rendering&lt;/code&gt; and &lt;code&gt;Layouts&lt;/code&gt;, which gives you full access to the normal rendering API without any of the other overhead of normal controllers. We add both the &lt;code&gt;direct&lt;/code&gt; directory and the normal view directory to the view path, which means that any templates you place inside &lt;code&gt;app/views/direct&lt;/code&gt; will take be used first, but it&amp;#8217;ll fall back to the normal view directory for layouts or partials. We also specify that the layout is &lt;code&gt;application&lt;/code&gt;, which is not the default in Rails 3 &lt;strong&gt;in this case&lt;/strong&gt; since our metal controller does not inherit from &lt;code&gt;ApplicationController&lt;/code&gt;.&lt;/p&gt;
&lt;h3&gt;Note for the Curious&lt;/h3&gt;
&lt;blockquote&gt;&lt;p&gt;In all normal application cases, Rails will look up the inheritance chain for a named layout matching the controller name. This means that the Rails 2 behavior, which allows you to provide a layout named after the controller, still works exactly the same as before, and that &lt;code&gt;ApplicationController&lt;/code&gt; is just another controller name, and &lt;code&gt;application.html.erb&lt;/code&gt; is its default layout.&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;And then, the actual use in your application:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;Rails.&lt;span&gt;application&lt;/span&gt;.&lt;span&gt;routes&lt;/span&gt; &lt;span&gt;do&lt;/span&gt;
  extend GenericActions
&amp;nbsp;
  match &lt;span&gt;&amp;quot;/foo&amp;quot;&lt;/span&gt;, &lt;span&gt;:to&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; render&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;&amp;quot;foo_index&amp;quot;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;
  &lt;span&gt;# match &amp;quot;/foo&amp;quot; =&amp;gt; render(&amp;quot;foo_index&amp;quot;) is a valid shortcut for the simple case&lt;/span&gt;
  match &lt;span&gt;&amp;quot;/foo/:id&amp;quot;&lt;/span&gt;, &lt;span&gt;:constraints&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; &lt;span&gt;&amp;#123;&lt;/span&gt;:id &lt;span&gt;=&amp;gt;&lt;/span&gt; &lt;span&gt;/&lt;/span&gt;\d&lt;span&gt;+/&lt;/span&gt;&lt;span&gt;&amp;#125;&lt;/span&gt;, &lt;span&gt;:to&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; render&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;&amp;quot;foo_detail&amp;quot;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Of course, because we&amp;#8217;re using a real controller shell, you&amp;#8217;ll be able to use any other options available on the render (like :status, :content_type, :location, :action, :layout, etc.).&lt;/p&gt;</content>
		<author>
			<name>wycats</name>
			<uri>http://yehudakatz.com</uri>
		</author>
		<source>
			<title type="html">Katz Got Your Tongue?</title>
			<subtitle type="html">Random Geek-Related Thoughts</subtitle>
			<link rel="self" href="http://yehudakatz.com/feed/atom/"/>
			<id>http://yehudakatz.com/feed/atom/</id>
			<updated>2010-03-13T08:20:07+00:00</updated>
		</source>
	</entry>

	<entry xml:lang="en-US">
		<title type="html">My Proposed Solution to the &quot;Can't Comment On RTs&quot; Problem: Retweet-Replies</title>
		<link href="http://gweezlebur.com/2009/12/13/retweet-replies.html"/>
		<id>http://gweezlebur.com/2009/12/13/retweet-replies</id>
		<updated>2009-12-13T06:00:00+00:00</updated>
		<content type="html">&lt;p&gt;I love retweets. More importantly, I love the new, native Retweets. I know there are plenty of people still up in arms about them, or refusing to use them because they don&amp;#8217;t like change, or for a whole bunch of reasons that I thought were silly at the time and didn&amp;#8217;t remember long enough to refute them.&lt;/p&gt;

&lt;p&gt;But that&amp;#8217;s not what I&amp;#8217;m writing about tonight.&lt;/p&gt;

&lt;p&gt;There is one real deficiency with native Retweets. Ev even mentioned it in &lt;a href=&quot;http://evhead.com/2009/11/why-retweet-works-way-it-does.html&quot;&gt;his blog post&lt;/a&gt;, which is excellent, and required reading before I&amp;#8217;ll debate why native Retweets are awesome with you.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://evhead.com/2009/11/why-retweet-works-way-it-does.html&quot;&gt;@ev&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The other thing some people will not like is that, unlike organic RTs, there&amp;#8217;s no way to annotate or leave your own comment when you retweet something with the new system&amp;#8230;we have some ideas there, and it&amp;#8217;s possible we&amp;#8217;ll build that in at a later date. (This point should not be missed.)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is important, and it&amp;#8217;s a big reason some people don&amp;#8217;t want to use the native Retweets. Even &lt;a href=&quot;http://twitter.com/jack/statuses/6151286368&quot;&gt;@jack isn&amp;#8217;t using them&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I have a solution. It&amp;#8217;s a really simple solution, but unfortunately it requires a little bit of work from Twitter. I&amp;#8217;d rather this were a completely user-behavior or client-side solution, but it is what it is.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;If I retweet @someone, and then reply to the retweet, push my reply to all of my followers, even if they don&amp;#8217;t follow @someone.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If Twitter made this simple change (to replies, you&amp;#8217;ll notice, not to retweets), it would mean I could use an entire 140 characters to annotate my retweet, and that annotation doesn&amp;#8217;t need Yet Another special case in Twitter clients, or an API change, or something new for users to learn.&lt;/p&gt;

&lt;p&gt;It would also mean innovative clients could add a &amp;#8220;RT/Reply&amp;#8221; button that would mark a tweet for retweeting, get the reply, and then do the retweet and the reply back to back so they show up next to each other.&lt;/p&gt;

&lt;p&gt;I call this idea &amp;#8220;Retweet-Replies&amp;#8221;, since, you know&amp;#8230;that&amp;#8217;s what they are.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://twitter.com/?status=@ivey+I+think+retweet-replies+are...&quot;&gt;What do you think?&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;CC: #twitterapi&lt;/p&gt;</content>
		<author>
			<name>Michael Ivey</name>
			<email>ivey@gweezlebur.com</email>
			<uri>http://gweezlebur.com/</uri>
		</author>
		<source>
			<title type="html">Michael Ivey: blogging since '99</title>
			<link rel="self" href="http://gweezlebur.com/atom.xml"/>
			<id>http://gweezlebur.com/</id>
			<updated>2010-01-31T16:20:06+00:00</updated>
		</source>
	</entry>

	<entry xml:lang="en-US">
		<title type="html">Introducing Moralist</title>
		<link href="http://gweezlebur.com/2009/12/12/moralist.html"/>
		<id>http://gweezlebur.com/2009/12/12/moralist</id>
		<updated>2009-12-12T06:00:00+00:00</updated>
		<content type="html">&lt;p&gt;I love Twitter lists. Really. I think they&amp;#8217;re one of the most important features Twitter has rolled out in the past year, even more than geotweets.&lt;/p&gt;

&lt;p&gt;First off, I think they&amp;#8217;ll slowly be the end of Follow Friday. Why plug people one day a week when you can have a list of people you suggest all the time? Even better, you can have multiple lists, organized by topic.&lt;/p&gt;

&lt;p&gt;More importantly, though, lists mean I can have multiple timelines. I have my main timeline of people I follow all the time. When they say something, I want to see it. (I haven&amp;#8217;t completely cleaned up my following list yet, but I dropped more than 100 people when I started using lists.) There are a lot of the people who are funny, but I don&amp;#8217;t need to see every tweet from them. They&amp;#8217;re on my &lt;a href=&quot;http://twitter.com/ivey/funny-people&quot;&gt;@ivey/funny-people&lt;/a&gt; list, and I read them when I have time.&lt;/p&gt;

&lt;p&gt;The problem is, not only do I have my list of funny people, but I also follow a few other people&amp;#8217;s lists of funny people, like &lt;a href=&quot;http://twitter.com/Favstar/rising-stars&quot;&gt;@Favstar/rising-stars&lt;/a&gt; and &lt;a href=&quot;http://twitter.com/Favstar/top-50-funny&quot;&gt;@Favstar/top-50-funny&lt;/a&gt;. There&amp;#8217;s a lot of overlap between these lists, which means I see the same tweets 2 or 3 times. That bugs me.&lt;/p&gt;

&lt;p&gt;So I fixed it. First, I made a little library called, creatively, &lt;a href=&quot;http://github.com/ivey/twitter-lists&quot;&gt;twitter-lists&lt;/a&gt;. It uses the twitter gem to do some set-based operations on Twitter lists, mainly Union and Intersection. It&amp;#8217;s not fully fleshed out yet, but it&amp;#8217;s usable. Patches welcome.&lt;/p&gt;

&lt;p&gt;Then, I made an app that uses it: &lt;a href=&quot;http://moralist.gweezlebur.com&quot;&gt;Moralist&lt;/a&gt;. Moralist makes it easy to combine lists in interesting ways. I&amp;#8217;m using it to merge &lt;a href=&quot;http://twitter.com/Favstar/rising-stars&quot;&gt;@Favstar/rising-stars&lt;/a&gt;, &lt;a href=&quot;http://twitter.com/Favstar/top-50-funny&quot;&gt;@Favstar/top-50-funny&lt;/a&gt;, and &lt;a href=&quot;http://twitter.com/ivey/funny-people&quot;&gt;@ivey/funny-people&lt;/a&gt; into a single list, &lt;a href=&quot;http://twitter.com/ivey/funny-hah&quot;&gt;@ivey/hah&lt;/a&gt;. This is the list I read when I have a few minutes and need a chuckle (although &lt;a href=&quot;http://favstar.fm&quot;&gt;Favstar&amp;#8217;s&lt;/a&gt; Favit iPhone app is replacing it, but that&amp;#8217;s another story.)&lt;/p&gt;

&lt;p&gt;Anyway, try it out. It refreshes once a day, it crashes sometimes, and it comes with no warranty. If people find it useful, I might bump it up to a paid Heroku plan, but for now it meets my needs so I&amp;#8217;m leaving it alone.&lt;/p&gt;

&lt;p&gt;PS: here&amp;#8217;s a snippet from Moralist to make a twitter-auth app able to use the twitter gem using the same set of keys: &lt;a href=&quot;http://gist.github.com/255214&quot;&gt;http://gist.github.com/255214&lt;/a&gt;&lt;/p&gt;</content>
		<author>
			<name>Michael Ivey</name>
			<email>ivey@gweezlebur.com</email>
			<uri>http://gweezlebur.com/</uri>
		</author>
		<source>
			<title type="html">Michael Ivey: blogging since '99</title>
			<link rel="self" href="http://gweezlebur.com/atom.xml"/>
			<id>http://gweezlebur.com/</id>
			<updated>2010-01-31T16:20:06+00:00</updated>
		</source>
	</entry>

	<entry xml:lang="en-US">
		<title type="html">Thanks for the Retweets</title>
		<link href="http://gweezlebur.com/2009/12/11/thanks-for-the-retweets.html"/>
		<id>http://gweezlebur.com/2009/12/11/thanks-for-the-retweets</id>
		<updated>2009-12-11T06:00:00+00:00</updated>
		<content type="html">&lt;p&gt;Lately I&amp;#8217;ve been seeing a handful of tweets per day that say something like, &amp;#8220;Thanks to @soandso, @friend, @someoneelse for the retweets today!&amp;#8221; and &amp;#8230; it doesn&amp;#8217;t feel right, to me.&lt;/p&gt;

&lt;p&gt;I get it. Twitter&amp;#8217;s about social, and connections, and all that jazz. If someone retweets something you said, they&amp;#8217;re lending you a little bit of their credibility. That&amp;#8217;s a nice thing to do, and it&amp;#8217;s worth thanking them for it, especially if it&amp;#8217;s something you&amp;#8217;d really like to get out there.&lt;/p&gt;

&lt;p&gt;If it was a DM, or a reply to that one person, I wouldn&amp;#8217;t notice. (I might not even see it.) Which is why people use the more public list of thank-yous, of course.&lt;/p&gt;

&lt;p&gt;Somehow, though, it seems like it&amp;#8217;s less about a genuine thank-you, and more of the self-promotion feedback loop we see over and over again on Twitter. What do you think? Am I being cynical and reading it wrong?&lt;/p&gt;</content>
		<author>
			<name>Michael Ivey</name>
			<email>ivey@gweezlebur.com</email>
			<uri>http://gweezlebur.com/</uri>
		</author>
		<source>
			<title type="html">Michael Ivey: blogging since '99</title>
			<link rel="self" href="http://gweezlebur.com/atom.xml"/>
			<id>http://gweezlebur.com/</id>
			<updated>2010-01-31T16:20:06+00:00</updated>
		</source>
	</entry>

	<entry xml:lang="en">
		<title type="html">Lots of rubies, now what?</title>
		<link href="http://merbist.com/2009/11/30/lots-of-rubies-now-what/"/>
		<id>http://merbist.com/?p=648</id>
		<updated>2009-11-30T21:38:53+00:00</updated>
		<content type="html">&lt;p&gt;If you were at RubyConf 2009 or looked at the schedule, you saw that the big thing happening in the Ruby scene is the maturation of a many of the Ruby implementations:&lt;br /&gt;
BlueRuby, HotRuby, IronRuby, JRuby, MacRuby, Maglev, MRI, REE, Rubinius, SmallRuby&amp;#8230;&lt;br /&gt;
Ruby developers really have plenty of choice when it comes to choosing an alternative Ruby implementation.&lt;/p&gt;
&lt;p&gt;Is that a bad thing?&lt;/p&gt;
&lt;p&gt;Turns out, Matz , Ruby author mentioned one ore time during RubyConf that it was actually a good thing for the Ruby ecosystem.&lt;/p&gt;
&lt;p&gt;But maybe we should take a step back and look at the big picture. So here are some of my thoughts on the topic. &lt;em&gt;(disclaimer: being part of the MacRuby team, I&amp;#8217;m certainly biased)&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Stop comparing alternative Ruby implementations against Ruby 1.8.x.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Even though a lot of people are still using Ruby 1.8.x, it&amp;#8217;s totally unfair to compare against such an old version of Matz&amp;#8217;s Ruby.&lt;br /&gt;
Ruby 1.9 is what people should compare against and believe me, Ruby 1.9.2 is going to change the game (much faster, cleaned up version).&lt;br /&gt;
Hopefully, all the Ruby alternative implementations will quickly become 1.9 compatible.&lt;/p&gt;
&lt;p&gt;(On a site note, &lt;a href=&quot;http://weblog.rubyonrails.org/2009/11/30/ruby-on-rails-2-3-5-released&quot; target=&quot;_blank&quot;&gt;Rails 2.3.5 was released&lt;/a&gt; a few days ago which fixes some of the last few remaining Ruby 1.9 bugs)&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Don&amp;#8217;t trust the benchmarks&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;As we all know, &lt;a id=&quot;j2gx&quot; title=&quot;there are lies, damn lies and statistics&quot; href=&quot;http://en.wikipedia.org/wiki/Lies,_damned_lies,_and_statistics&quot;&gt;there are lies, damn lies and statistics&lt;/a&gt;. Evan Phoenix explained that very clearly with lots of examples during his &lt;a href=&quot;http://rubyconf2009.confreaks.com/&quot; target=&quot;_blank&quot;&gt;RubyConf talk&lt;/a&gt; and I totally agree with him.&lt;br /&gt;
Micro benchmarks only help implementers know where they are standing and find performance issue.&lt;br /&gt;
Let&amp;#8217;s take a very simple example: &lt;a href=&quot;http://jruby.org/&quot; target=&quot;_blank&quot;&gt;JRuby&lt;/a&gt;.&lt;br /&gt;
What you might not know is that most JRuby benchmarks are usually &amp;#8216;warmed up&amp;#8217;.  Basically, to get the results you see in most reported benchmarks, you need to run the benchmark in a loop X amount of times and then run the benchmark. The reason for that is that JRuby uses HotSpot which needs to process the code to optimize the JITting and finally get your code to run faster.&lt;br /&gt;
The problem is that it is not a fair comparison. I was benchmarking Rails3 for instance and JRuby starts way slower than Ruby 1.8.6 and takes a little while to get as fast/faster than 1.9.1.&lt;br /&gt;
This is something you need to take in consideration since each deployment will slow down your app and you probably shouldn&amp;#8217;t use JRuby if you are not using long running processes.&lt;br /&gt;
Another example would be MacRuby, even though the MacRuby project never released any benchmarks, it&amp;#8217;s known to be a &lt;em&gt;fast&lt;/em&gt; implementation.&lt;br /&gt;
Currently, the allocation of objects in MacRuby is actually still a bit costly and if you benchmark that, you can &lt;em&gt;show&lt;/em&gt; that MacRuby is slow. Other operations are super optimized and both Rubinius and MacRuby will be way faster than anyone else. (check Phoenix&amp;#8217;s talk at Ruby conf for more examples)&lt;br /&gt;
So, before choosing an implementation for performance reasons, benchmark against the code you are going to use.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;There is only one real original Ruby&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;I think nobody will disagree with the fact that there is only one Ruby reference and that&amp;#8217;s Matz.&lt;br /&gt;
Therefore is only one &amp;#8220;real&amp;#8221; Ruby and that is Matz&amp;#8217;s. (Matz actually disagreed with me when I said that, since all the other implementations are also &amp;#8220;real&amp;#8221; but you get the point)&lt;br /&gt;
There is nothing wrong with Ruby&amp;#8217;s alternatives but they stay alternatives to the original&lt;br /&gt;
MRI might not be the best option for every single case out there but it is and will stay the reference.&lt;/p&gt;
&lt;p&gt;During RubyConf, I was joking with Matz and Koichi that maybe they should claim a Ruby tax to the Ruby implementations.&lt;br /&gt;
The reality is that Ruby is maintained by 5 to 10 contributors, most of them doing that on the side of their full time job.&lt;br /&gt;
Most of the other implementations have full time staff working hard on their implementations. That&amp;#8217;s exciting but sad at the same time.&lt;/p&gt;
&lt;p&gt;During the different talks given by the Ruby core team, it was clear that they really want to bridge the West/East gap and asked for help. It&amp;#8217;s very honorable on their part and I hope we will see more contributions coming from outside Japan.&lt;/p&gt;
&lt;p&gt;Finally, I think that even if people work hard to write the best Ruby implementation ever, let&amp;#8217;s not forget to respect Matz&amp;#8217;s project, team and achievements. People spent hours/weeks/months creating the Ruby language we love and even though there are some aspects that could be and are being improved, let&amp;#8217;s not forget that when criticizing/comparing their work.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;There is not only one way to do things&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;I don&amp;#8217;t know where that&amp;#8217;s coming from, but some people in our community seem to believe that there is only one way to do things and if you don&amp;#8217;t do it their way, you do it wrong.&lt;br /&gt;
I personally have a hard time believing that and I even think it goes against what Ruby was designed for. (However, I must admit that sometimes I did/do think that some approaches were totally wrong :p)&lt;br /&gt;
It reminds me of people trying to convince you that God exists, other people trying to convince you that their religion is the only true one, or finally the other people trying to convince believers that they are wrong.&lt;br /&gt;
Maybe it is just that as humans we have a hard time dealing with people coming to a different conclusion than us and therefore we have to convince them that we are right and they are wrong.&lt;/p&gt;
&lt;p&gt;The way I see it, for any new Ruby project, you should start be using with Ruby1.9. If it doesn&amp;#8217;t fit your needs, then, consider an alternative. Here is how I look at the other alternatives (feel free to disagree):&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a title=&quot;BlueRuby&quot; href=&quot;https://wiki.sdn.sap.com/wiki/display/Research/BlueRuby&quot; target=&quot;_blank&quot;&gt;BlueRuby&lt;/a&gt; &amp;#8211; use if you are writing a SAP app&lt;/li&gt;
&lt;li&gt;&lt;a title=&quot;HotRuby&quot; href=&quot;http://hotruby.yukoba.jp/&quot; target=&quot;_blank&quot;&gt;HotRuby&lt;/a&gt; &amp;#8211; use if you want to use Ruby to write a Flash app&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://ironruby.net/&quot; target=&quot;_blank&quot;&gt;IronRuby&lt;/a&gt; &amp;#8211; use it if you want to integrate with .NET and/or write a silverlight app&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://jruby.org/&quot; target=&quot;_blank&quot;&gt;JRuby&lt;/a&gt; &amp;#8211; use if you want to integrate with the Java world&lt;/li&gt;
&lt;li&gt;&lt;a title=&quot;MacRuby&quot; href=&quot;http://macruby.org&quot; target=&quot;_blank&quot;&gt;MacRuby&lt;/a&gt; &amp;#8211; use it if you want to integrate with Cocoa/want a native OSX application&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://maglev.gemstone.com/&quot; target=&quot;_blank&quot;&gt;Maglev&lt;/a&gt; &amp;#8211; use if you want to use an object persistence layer and a distributed shared cache&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.rubyenterpriseedition.com/&quot; target=&quot;_blank&quot;&gt;REE&lt;/a&gt; &amp;#8211; use if you have a ruby 1.8 web app that uses too much memory&lt;/li&gt;
&lt;li&gt;&lt;a title=&quot;Rubinius&quot; href=&quot;http://rubini.us&quot; target=&quot;_blank&quot;&gt;Rubinius&lt;/a&gt; &amp;#8211; use if you want to know what&amp;#8217;s going on in your code&lt;/li&gt;
&lt;li&gt;&lt;a id=&quot;c..p&quot; title=&quot;SmallRuby&quot; href=&quot;http://smalltalk.felk.cvut.cz/projects/smallruby&quot;&gt;SmallRuby&lt;/a&gt; &amp;#8211; don&amp;#8217;t use it yet, but keep an eye on it&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This is the way I see the various existing implementations and I think this is where they shine and why they were created in the first place. They are all complimentary and help bring the Ruby language further. The goal is always the same.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The future is exciting&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Ruby is getting everywhere or almost. Matz&amp;#8217;s Ruby is going to get multiVM support, various other improvements and get even faster and faster.&lt;br /&gt;
Alternative implementations are getting more and more mature and they grow the community by bringing people from different communities (.NET, java, obj-c/cocoa)&lt;/p&gt;
&lt;p&gt;At the end of the day, we are all beneficiaries of the work done by all the implementers, thank you guys and keep up the good work!&lt;/p&gt;</content>
		<author>
			<name>Matt Aimonetti (mattetti)</name>
			<uri>http://merbist.com</uri>
		</author>
		<source>
			<title type="html">Merbist</title>
			<subtitle type="html">Rails 3, Ruby &amp;amp; MacRuby news - tutorials - consulting - training</subtitle>
			<link rel="self" href="http://merbist.com/feed/"/>
			<id>http://merbist.com/feed/</id>
			<updated>2010-03-13T12:20:05+00:00</updated>
		</source>
	</entry>

	<entry xml:lang="en">
		<title type="html">The Ruby Revolution, take II</title>
		<link href="http://merbist.com/2009/11/16/the-ruby-revolution-take-ii/"/>
		<id>http://merbist.com/?p=636</id>
		<updated>2009-11-16T16:05:55+00:00</updated>
		<content type="html">&lt;div id=&quot;attachment_638&quot; class=&quot;wp-caption alignright&quot;&gt;&lt;img class=&quot;size-full wp-image-638&quot; title=&quot;Gandhi&quot; src=&quot;http://merbist.com/wp-content/uploads/2009/11/GANDHI.jpg&quot; alt=&quot;Mohandas Karamchand Gandhi&quot; width=&quot;300&quot; height=&quot;450&quot; /&gt;&lt;p class=&quot;wp-caption-text&quot;&gt;Mohandas Karamchand Gandhi&lt;/p&gt;&lt;/div&gt;
&lt;p&gt;My recent &amp;#8216;&lt;a title=&quot;Ruby revolution&quot; href=&quot;http://merbist.com/2009/11/09/the-ruby-revolution-is-over&quot; target=&quot;_blank&quot;&gt;Ruby revolution being over&lt;/a&gt;&amp;#8216; blog post generated quite a &lt;a href=&quot;http://merbist.com/2009/11/09/the-ruby-revolution-is-over/#comments&quot; target=&quot;_blank&quot;&gt;lot of comments&lt;/a&gt;.&lt;br /&gt;
Let&amp;#8217;s be honest, I did not expect less from the readers.&lt;/p&gt;
&lt;p&gt;However, I noticed three types of reactions I would like to address:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;It was not a Ruby revolution, it was a Rails revolution&lt;/li&gt;
&lt;li&gt;The revolution has stalled due to no major enterprise backing&lt;/li&gt;
&lt;li&gt;The revolution will only be over when we will reach a greater adoption&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;First of all, as &lt;a href=&quot;http://merbist.com/2009/11/09/the-ruby-revolution-is-over/#comment-817&quot; target=&quot;_blank&quot;&gt;Joe correctly mentioned&lt;/a&gt;, for me, the revolution is not about specifics or individuals. It&amp;#8217;s really about the big picture.&lt;/p&gt;
&lt;p&gt;The influence Ruby had and still has on the IT world seems to be undermined by some.&lt;br /&gt;
Ruby is a dynamic, truly Object Oriented programming scripting language &lt;strong&gt;designed for humans first&lt;/strong&gt;.&lt;br /&gt;
The real paradigm shift is in the fact that Ruby was designed to make programming fast, enjoyable and easy instead of being optimized for the machines running it.&lt;br /&gt;
This is for me the essence of the revolution and it is meant to transcend the scope of the Ruby language.&lt;/p&gt;
&lt;p&gt;The way I see it, Yukihiro Matsumoto (Matz) is more of an artist than a technician. He had a vision for software development. &lt;strong&gt;Programming languages cannot be optimized/designed for both machines and humans, the language designer has to choose which one he wants to privilege.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Most programming languages believe that it&amp;#8217;s up to the programmer to make an extra effort since he is smarter and easier to optimize than a machine. Matz questioned this approached and decided to turn things around. The result is one of the reasons why developers seem to just fall in love with Ruby.&lt;/p&gt;
&lt;h3&gt;It was not a Ruby revolution, it was a Rails revolution.&lt;/h3&gt;
&lt;p&gt;I am not denying that there &lt;strong&gt;*also*&lt;/strong&gt; was a Rails revolution.&lt;br /&gt;
But if you look at it, Rails and its revolution are a direct effect from Ruby&amp;#8217;s revolution.&lt;br /&gt;
One might argue that it is actually an extension of Ruby&amp;#8217;s philosophy. But what is Rails if not a web framework designed to make web development fast, easy and enjoyable?&lt;br /&gt;
Without Ruby there would not have been Rails and that was my point, the underlying revolution comes from the language itself.&lt;/p&gt;
&lt;h3&gt;The revolution has stalled due to no major enterprise backing.&lt;/h3&gt;
&lt;p&gt;That&amp;#8217;s an interesting comment. It is true that .NET and Java are still dominating the enterprise world. But let&amp;#8217;s be clear, Ruby was not designed to please &amp;#8220;suit people&amp;#8221;.&lt;br /&gt;
And to this day, there is still a strong feeling, from some individuals against the enterprise.&lt;br /&gt;
In the past, DHH openly said that he did not care nor wanted to hear about the enterprise, more recently, Obie Fernandez, during one of his talks said: &lt;a href=&quot;http://blip.tv/file/2733212&quot; target=&quot;_blank&quot;&gt;&amp;#8220;Fuck the enterprise&amp;#8221;&lt;/a&gt; (49:39).&lt;br /&gt;
&lt;strong&gt;But the truth is that Ruby and the so called enterprise, both, are changing.&lt;/strong&gt;&lt;br /&gt;
The smart people in the enterprise world saw potential in Ruby and decided to give it a chance. An easy way to include Ruby&amp;#8217;s philosophy without breaking the fragile enterprise equilibrium was to inject Ruby in the midst of well known and respected technologies such as Java and .NET. The enterprise can now use &amp;#8220;re-branded Ruby versions&amp;#8221; with &amp;#8220;new taste or &amp;#8216;improved&amp;#8217; flavor&amp;#8221; like JRuby, Scala, groovy, IronRuby.&lt;br /&gt;
I work for some enterprise clients and I can tell you that they &amp;#8216;also&amp;#8217; use Ruby. Mainly because developers love the language.&lt;br /&gt;
Microsoft, Apple and SAP investing in their own implementation of the language is yet another example that the enterprise recognizes the value of Matz&amp;#8217;s work.&lt;br /&gt;
Nobody can blame them to try to make Ruby fit more their requirements.&lt;br /&gt;
So, at the end of the day, Ruby is not the #1 enterprise language and Rails isn&amp;#8217;t used by the large majority of enterprise web apps, but that is NOT the point. Ruby has influenced the enterprise and we will see its effects for many years.&lt;/p&gt;
&lt;h3&gt;The revolution will only be over when we will reach a greater adoption&lt;/h3&gt;
&lt;p&gt;Saying that is missing the point entirely. A revolution is a step towards a situation change. Things don&amp;#8217;t change right away after a revolution. It takes a long time for mentalities to evolve and for people to change their habits.&lt;br /&gt;
The consequences of a revolution are to be studied over the decades following the event. Take smalltalk for instance. Smalltalk adoption was not that great, however it brought a paradigm shift that directly influenced languages such as Ruby, Python and Objective-C.&lt;br /&gt;
So, again, do not focus on the adoption but instead look at the influence of the Ruby revolution and the ripple effect around it.&lt;/p&gt;</content>
		<author>
			<name>Matt Aimonetti (mattetti)</name>
			<uri>http://merbist.com</uri>
		</author>
		<source>
			<title type="html">Merbist</title>
			<subtitle type="html">Rails 3, Ruby &amp;amp; MacRuby news - tutorials - consulting - training</subtitle>
			<link rel="self" href="http://merbist.com/feed/"/>
			<id>http://merbist.com/feed/</id>
			<updated>2010-03-13T12:20:05+00:00</updated>
		</source>
	</entry>

	<entry xml:lang="en">
		<title type="html">Metaprogramming in Ruby: It&amp;#8217;s All About the Self</title>
		<link href="http://feedproxy.google.com/~r/KatzGotYourTongue/~3/ZqeCaHw53i8/"/>
		<id>http://yehudakatz.com/?p=367</id>
		<updated>2009-11-16T03:02:20+00:00</updated>
		<content type="html">&lt;p&gt;After writing my last post on Rails plugin idioms, I realized that Ruby metaprogramming, at its core, is actually quite simple.&lt;/p&gt;
&lt;p&gt;It comes down to the fact that all Ruby code is executed code&amp;#8211;there is no separate compile or runtime phase. In Ruby, every line of code is executed against a particular &lt;code&gt;self&lt;/code&gt;. Consider the following five snippets:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;class&lt;/span&gt; Person
  &lt;span&gt;def&lt;/span&gt; &lt;span&gt;self&lt;/span&gt;.&lt;span&gt;species&lt;/span&gt;
    &lt;span&gt;&amp;quot;Homo Sapien&amp;quot;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
&lt;span&gt;class&lt;/span&gt; Person
  &lt;span&gt;class&lt;/span&gt; &lt;span&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span&gt;self&lt;/span&gt;
    &lt;span&gt;def&lt;/span&gt; species
      &lt;span&gt;&amp;quot;Homo Sapien&amp;quot;&lt;/span&gt;
    &lt;span&gt;end&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
&lt;span&gt;class&lt;/span&gt; &lt;span&gt;&amp;lt;&amp;lt;&lt;/span&gt; Person
  &lt;span&gt;def&lt;/span&gt; species
    &lt;span&gt;&amp;quot;Homo Sapien&amp;quot;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
Person.&lt;span&gt;instance_eval&lt;/span&gt; &lt;span&gt;do&lt;/span&gt;
  &lt;span&gt;def&lt;/span&gt; species
    &lt;span&gt;&amp;quot;Homo Sapien&amp;quot;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
&lt;span&gt;def&lt;/span&gt; Person.&lt;span&gt;species&lt;/span&gt;
  &lt;span&gt;&amp;quot;Homo Sapien&amp;quot;&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;All five of these snippets define a &lt;code&gt;Person.species&lt;/code&gt; that returns &lt;code&gt;Homo Sapien&lt;/code&gt;. Now consider another set of snippets:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;class&lt;/span&gt; Person
  &lt;span&gt;def&lt;/span&gt; name
    &lt;span&gt;&amp;quot;Matz&amp;quot;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
Person.&lt;span&gt;class_eval&lt;/span&gt; &lt;span&gt;do&lt;/span&gt;
  &lt;span&gt;def&lt;/span&gt; name
    &lt;span&gt;&amp;quot;Matz&amp;quot;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;These snippets all define a method called &lt;code&gt;name&lt;/code&gt; on the Person class. So &lt;code&gt;Person.new.name&lt;/code&gt; will return &amp;#8220;Matz&amp;#8221;. For those familiar with Ruby, this isn&amp;#8217;t news. When learning about metaprogramming, each of these snippets is presented in isolation: another mechanism for getting methods where they &amp;#8220;belong&amp;#8221;. In fact, however, there is a single unified reason that all of these snippets work the way they do.&lt;/p&gt;
&lt;p&gt;First, it is important to understand how Ruby&amp;#8217;s metaclass works. When you first learn Ruby, you learn about the concept of the class, and that each object in Ruby has one:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;class&lt;/span&gt; Person
&lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
Person.&lt;span&gt;class&lt;/span&gt; &lt;span&gt;#=&amp;gt; Class&lt;/span&gt;
&amp;nbsp;
&lt;span&gt;class&lt;/span&gt; &lt;span&gt;Class&lt;/span&gt;
  &lt;span&gt;def&lt;/span&gt; loud_name
    &lt;span&gt;&amp;quot;#{name.upcase}!&amp;quot;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
Person.&lt;span&gt;loud_name&lt;/span&gt; &lt;span&gt;#=&amp;gt; &amp;quot;PERSON!&amp;quot;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;code&gt;Person&lt;/code&gt; is an instance of &lt;code&gt;Class&lt;/code&gt;, so any methods added to &lt;code&gt;Class&lt;/code&gt; are available on &lt;code&gt;Person&lt;/code&gt; as well. What they don&amp;#8217;t tell you, however, is that each object in Ruby also has its own &lt;strong&gt;metaclass&lt;/strong&gt;, a &lt;code&gt;Class&lt;/code&gt; that can have methods, but is only attached to the object itself.&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;matz = &lt;span&gt;Object&lt;/span&gt;.&lt;span&gt;new&lt;/span&gt;
&lt;span&gt;def&lt;/span&gt; matz.&lt;span&gt;speak&lt;/span&gt;
  &lt;span&gt;&amp;quot;Place your burden to machine's shoulders&amp;quot;&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;What&amp;#8217;s going on here is that we&amp;#8217;re adding the &lt;code&gt;speak&lt;/code&gt; method to &lt;code&gt;matz&lt;/code&gt;&amp;#8217;s &lt;strong&gt;metaclass&lt;/strong&gt;, and the &lt;code&gt;matz&lt;/code&gt; object inherits from its &lt;strong&gt;metaclass&lt;/strong&gt; and then &lt;code&gt;Object&lt;/code&gt;. The reason this is somewhat less clear than ideal is that the metaclass is invisible in Ruby:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;matz = &lt;span&gt;Object&lt;/span&gt;.&lt;span&gt;new&lt;/span&gt;
&lt;span&gt;def&lt;/span&gt; matz.&lt;span&gt;speak&lt;/span&gt;
  &lt;span&gt;&amp;quot;Place your burden to machine's shoulders&amp;quot;&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
matz.&lt;span&gt;class&lt;/span&gt; &lt;span&gt;#=&amp;gt; Object&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In fact, &lt;code&gt;matz&lt;/code&gt;&amp;#8217;s &amp;#8220;class&amp;#8221; is its invisible metaclass. We can even get access to the metaclass:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;metaclass = &lt;span&gt;class&lt;/span&gt; &lt;span&gt;&amp;lt;&amp;lt;&lt;/span&gt; matz; &lt;span&gt;self&lt;/span&gt;; &lt;span&gt;end&lt;/span&gt;
metaclass.&lt;span&gt;instance_methods&lt;/span&gt;.&lt;span&gt;grep&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;/&lt;/span&gt;speak&lt;span&gt;/&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt; &lt;span&gt;#=&amp;gt; [&amp;quot;speak&amp;quot;]&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;At this point in other articles on this topic, you&amp;#8217;re probably struggling to keep all of the details in your head; it seems as though there are so many rules. And what&amp;#8217;s this &lt;code&gt;class &amp;lt;&amp;lt; matz&lt;/code&gt; thing anyway?&lt;/p&gt;
&lt;p&gt;It turns out that all of these weird rules collapse down into a single concept: control over the &lt;code&gt;self&lt;/code&gt; in a given part of the code. Let&amp;#8217;s go back and take a look at some of the snippets we looked at earlier:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;class&lt;/span&gt; Person
  &lt;span&gt;def&lt;/span&gt; name
    &lt;span&gt;&amp;quot;Matz&amp;quot;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;self&lt;/span&gt;.&lt;span&gt;name&lt;/span&gt; &lt;span&gt;#=&amp;gt; &amp;quot;Person&amp;quot;&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Here, we are adding the &lt;code&gt;name&lt;/code&gt; method to the &lt;code&gt;Person&lt;/code&gt; class. Once we say &lt;code&gt;class Person&lt;/code&gt;, the &lt;code&gt;self&lt;/code&gt; until the end of the block is the &lt;code&gt;Person&lt;/code&gt; class itself.&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;Person.&lt;span&gt;class_eval&lt;/span&gt; &lt;span&gt;do&lt;/span&gt;
  &lt;span&gt;def&lt;/span&gt; name
    &lt;span&gt;&amp;quot;Matz&amp;quot;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;self&lt;/span&gt;.&lt;span&gt;name&lt;/span&gt; &lt;span&gt;#=&amp;gt; &amp;quot;Person&amp;quot;&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Here, we&amp;#8217;re doing exactly the same thing: adding the &lt;code&gt;name&lt;/code&gt; method to instances of the Person class. In this case, &lt;code&gt;class_eval&lt;/code&gt; is setting the &lt;code&gt;self&lt;/code&gt; to &lt;code&gt;Person&lt;/code&gt; until the end of the block. This is all perfectly straight forward when dealing with classes, but it&amp;#8217;s equally straight forward when dealing with metaclasses:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;def&lt;/span&gt; Person.&lt;span&gt;species&lt;/span&gt;
  &lt;span&gt;&amp;quot;Homo Sapien&amp;quot;&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
Person.&lt;span&gt;name&lt;/span&gt; &lt;span&gt;#=&amp;gt; &amp;quot;Person&amp;quot;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;As in the &lt;code&gt;matz&lt;/code&gt; example earlier, we are defining the &lt;code&gt;species&lt;/code&gt; method on &lt;code&gt;Person&lt;/code&gt;&amp;#8217;s metaclass. We have not manipulated &lt;code&gt;self&lt;/code&gt;, but you can see using &lt;code&gt;def&lt;/code&gt; with an object attaches the method to the object&amp;#8217;s metaclass.&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;class&lt;/span&gt; Person
  &lt;span&gt;def&lt;/span&gt; &lt;span&gt;self&lt;/span&gt;.&lt;span&gt;species&lt;/span&gt;
    &lt;span&gt;&amp;quot;Homo Sapien&amp;quot;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;self&lt;/span&gt;.&lt;span&gt;name&lt;/span&gt; &lt;span&gt;#=&amp;gt; &amp;quot;Person&amp;quot;&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Here, we have opened the &lt;code&gt;Person&lt;/code&gt; class, setting the &lt;code&gt;self&lt;/code&gt; to &lt;code&gt;Person&lt;/code&gt; for the duration of the block, as in the example above. However, we are defining a method on &lt;code&gt;Person&lt;/code&gt;&amp;#8217;s metaclass here, since we&amp;#8217;re defining the method on an object (&lt;code&gt;self&lt;/code&gt;). Also, you can see that &lt;code&gt;self.name&lt;/code&gt; while inside the person class is identical to &lt;code&gt;Person.name&lt;/code&gt; while outside it.&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;class&lt;/span&gt; &lt;span&gt;&amp;lt;&amp;lt;&lt;/span&gt; Person
  &lt;span&gt;def&lt;/span&gt; species
    &lt;span&gt;&amp;quot;Homo Sapien&amp;quot;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;self&lt;/span&gt;.&lt;span&gt;name&lt;/span&gt; &lt;span&gt;#=&amp;gt; &amp;quot;&amp;quot;&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Ruby provides a syntax for accessing an object&amp;#8217;s metaclass directly. By doing &lt;code&gt;class &amp;lt;&amp;lt; Person&lt;/code&gt;, we are setting &lt;code&gt;self&lt;/code&gt; to &lt;code&gt;Person&lt;/code&gt;&amp;#8217;s metaclass for the duration of the block. As a result, the &lt;code&gt;species&lt;/code&gt; method is added to &lt;code&gt;Person&lt;/code&gt;&amp;#8217;s metaclass, rather than the class itself.&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;class&lt;/span&gt; Person
  &lt;span&gt;class&lt;/span&gt; &lt;span&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span&gt;self&lt;/span&gt;
    &lt;span&gt;def&lt;/span&gt; species
      &lt;span&gt;&amp;quot;Homo Sapien&amp;quot;&lt;/span&gt;
    &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
    &lt;span&gt;self&lt;/span&gt;.&lt;span&gt;name&lt;/span&gt; &lt;span&gt;#=&amp;gt; &amp;quot;&amp;quot;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Here, we combine several of the techniques. First, we open &lt;code&gt;Person&lt;/code&gt;, making &lt;code&gt;self&lt;/code&gt; equal to the &lt;code&gt;Person&lt;/code&gt; class. Next, we do &lt;code&gt;class &amp;lt;&amp;lt; self&lt;/code&gt;, making &lt;code&gt;self&lt;/code&gt; equal to &lt;code&gt;Person&lt;/code&gt;&amp;#8217;s metaclass. When we then define the &lt;code&gt;species&lt;/code&gt; method, it is defined on &lt;code&gt;Person&lt;/code&gt;&amp;#8217;s metaclass.&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;Person.&lt;span&gt;instance_eval&lt;/span&gt; &lt;span&gt;do&lt;/span&gt;
  &lt;span&gt;def&lt;/span&gt; species
    &lt;span&gt;&amp;quot;Homo Sapien&amp;quot;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;self&lt;/span&gt;.&lt;span&gt;name&lt;/span&gt; &lt;span&gt;#=&amp;gt; &amp;quot;Person&amp;quot;&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The last case, &lt;code&gt;instance_eval&lt;/code&gt;, actually does something interesting. It breaks apart the &lt;code&gt;self&lt;/code&gt; into the &lt;code&gt;self&lt;/code&gt; that is used to execute methods and the &lt;code&gt;self&lt;/code&gt; that is used when new methods are defined. When &lt;code&gt;instance_eval&lt;/code&gt; is used, new methods are defined on the &lt;strong&gt;metaclass&lt;/strong&gt;, but the &lt;code&gt;self&lt;/code&gt; is the object itself.&lt;/p&gt;
&lt;p&gt;In some of these cases, the multiple ways to achieve the same thing arise naturally out of Ruby&amp;#8217;s semantics. After this explanation, it should be clear that &lt;code&gt;def Person.species&lt;/code&gt;, &lt;code&gt;class &amp;lt;&amp;lt; Person; def species&lt;/code&gt;, and &lt;code&gt;class Person; class &amp;lt;&amp;lt; self; def species&lt;/code&gt; aren&amp;#8217;t three ways to achieve the same thing &lt;strong&gt;by design&lt;/strong&gt;, but that they arise out of Ruby&amp;#8217;s flexibility with regard to specifying what &lt;code&gt;self&lt;/code&gt; is at any given point in your program.&lt;/p&gt;
&lt;p&gt;On the other hand, &lt;code&gt;class_eval&lt;/code&gt; is slightly different. Because it take a block, rather than act as a keyword, it captures the local variables surrounding it. This can provide powerful DSL capabilities, in addition to controlling the &lt;code&gt;self&lt;/code&gt; used in a code block. But other than that, they are exactly identical to the other constructs used here.&lt;/p&gt;
&lt;p&gt;Finally, &lt;code&gt;instance_eval&lt;/code&gt; breaks apart the &lt;code&gt;self&lt;/code&gt; into two parts, while also giving you access to local variables defined outside of it.&lt;/p&gt;
&lt;p&gt;In the following table, &lt;em&gt;defines a new scope&lt;/em&gt; means that code inside the block does &lt;strong&gt;not&lt;/strong&gt; have access to local variables outside of the block.&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;mechanism&lt;/th&gt;
&lt;th&gt;method resolution&lt;/th&gt;
&lt;th&gt;method definition&lt;/th&gt;
&lt;th&gt;new scope?&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;th&gt;class Person&lt;/th&gt;
&lt;td&gt;Person&lt;/td&gt;
&lt;td&gt;same&lt;/td&gt;
&lt;td&gt;yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;th&gt;class  Person&lt;/th&gt;
&lt;td&gt;Person&amp;#8217;s metaclass&lt;/td&gt;
&lt;td&gt;same&lt;/td&gt;
&lt;td&gt;yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;th&gt;Person.class_eval&lt;/th&gt;
&lt;td&gt;Person&lt;/td&gt;
&lt;td&gt;same&lt;/td&gt;
&lt;td&gt;no&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;th&gt;Person.instance_eval&lt;/th&gt;
&lt;td&gt;Person&lt;/td&gt;
&lt;td&gt;Person&amp;#8217;s metaclass&lt;/td&gt;
&lt;td&gt;no&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Also note that &lt;code&gt;class_eval&lt;/code&gt; is only available to &lt;code&gt;Modules&lt;/code&gt; (note that Class inherits from Module) and is an alias for &lt;code&gt;module_eval&lt;/code&gt;. Additionally, &lt;code&gt;instance_exec&lt;/code&gt;, which was added to Ruby in 1.8.7, works exactly like &lt;code&gt;instance_eval&lt;/code&gt;, except that it also allows you to send variables into the block.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;UPDATE:&lt;/strong&gt; Thank you to Yugui of the Ruby core team for &lt;a href=&quot;http://yugui.jp/articles/846&quot;&gt;correcting the original post&lt;/a&gt;, which ignored the fact that &lt;code&gt;self&lt;/code&gt; is broken into two in the case of &lt;code&gt;instance_eval&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;feedflare&quot;&gt;
&lt;a href=&quot;http://feeds.feedburner.com/~ff/KatzGotYourTongue?a=ZqeCaHw53i8:EnR_gXBEMGY:yIl2AUoC8zA&quot;&gt;&lt;img src=&quot;http://feeds.feedburner.com/~ff/KatzGotYourTongue?d=yIl2AUoC8zA&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src=&quot;http://feeds.feedburner.com/~r/KatzGotYourTongue/~4/ZqeCaHw53i8&quot; height=&quot;1&quot; width=&quot;1&quot; /&gt;</content>
		<author>
			<name>wycats</name>
			<uri>http://yehudakatz.com</uri>
		</author>
		<source>
			<title type="html">Katz Got Your Tongue?</title>
			<subtitle type="html">Random Geek-Related Thoughts</subtitle>
			<link rel="self" href="http://yehudakatz.com/feed/atom/"/>
			<id>http://yehudakatz.com/feed/atom/</id>
			<updated>2010-03-13T08:20:07+00:00</updated>
		</source>
	</entry>

	<entry xml:lang="en">
		<title type="html">Better Ruby Idioms</title>
		<link href="http://feedproxy.google.com/~r/KatzGotYourTongue/~3/VWgrh-jlpA4/"/>
		<id>http://yehudakatz.com/?p=365</id>
		<updated>2009-11-12T08:17:03+00:00</updated>
		<content type="html">&lt;p&gt;Carl and I have been working on the plugins system over the past few days. As part of that process, we read through the &lt;a href=&quot;http://guides.rubyonrails.org/plugins.html&quot;&gt;Rails Plugin Guide&lt;/a&gt;. While reading through the guide, we noticed a number of idioms presented in the guide that are serious overkill for the task at hand.&lt;/p&gt;
&lt;p&gt;I don&amp;#8217;t blame the author of the guide; the idioms presented are roughly the same that have been used since the early days of Rails. However, looking at them brought back memories of my early days using Rails, when the code made me feel as though Ruby was full of magic incantations and ceremony to accomplish relatively simple things.&lt;/p&gt;
&lt;p&gt;Here&amp;#8217;s an example:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;module&lt;/span&gt; Yaffle
  &lt;span&gt;def&lt;/span&gt; &lt;span&gt;self&lt;/span&gt;.&lt;span&gt;included&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;base&lt;span&gt;&amp;#41;&lt;/span&gt;
    base.&lt;span&gt;send&lt;/span&gt; &lt;span&gt;:extend&lt;/span&gt;, ClassMethods
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;module&lt;/span&gt; ClassMethods
    &lt;span&gt;# any method placed here will apply to classes, like Hickwall&lt;/span&gt;
    &lt;span&gt;def&lt;/span&gt; acts_as_something
      send &lt;span&gt;:include&lt;/span&gt;, InstanceMethods
    &lt;span&gt;end&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;module&lt;/span&gt; InstanceMethods
    &lt;span&gt;# any method placed here will apply to instaces, like @hickwall&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;To begin with, the &lt;code&gt;send&lt;/code&gt; is completely unneeded. The &lt;code&gt;acts_as_something&lt;/code&gt; method will be run on the Class itself, giving the method access to &lt;code&gt;include&lt;/code&gt;, a private method.&lt;/p&gt;
&lt;p&gt;This code intended to be used as follows:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;class&lt;/span&gt; &lt;span&gt;ActiveRecord::Base&lt;/span&gt;
  &lt;span&gt;include&lt;/span&gt; Yaffle
&lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
&lt;span&gt;class&lt;/span&gt; Article &lt;span&gt;&amp;lt;&lt;/span&gt; &lt;span&gt;ActiveRecord::Base&lt;/span&gt;
  acts_as_yaffle
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;What the code does is:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Register a hook so that when the module is included, the ClassMethods are extended onto the class&lt;/li&gt;
&lt;li&gt;In that module, define a method that includes the InstanceMethods&lt;/li&gt;
&lt;li&gt;So that you can say &lt;code&gt;acts_as_something&lt;/code&gt; in your code&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The crazy thing about all of this is that it&amp;#8217;s completely reinventing the module system that Ruby already has. This would be exactly identical:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;module&lt;/span&gt; Yaffle
  &lt;span&gt;# any method placed here will apply to classes, like Hickwall&lt;/span&gt;
  &lt;span&gt;def&lt;/span&gt; acts_as_something
    send &lt;span&gt;:include&lt;/span&gt;, InstanceMethods
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;module&lt;/span&gt; InstanceMethods
    &lt;span&gt;# any method placed here will apply to instances, like @hickwall&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;To be used via:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;class&lt;/span&gt; &lt;span&gt;ActiveRecord::Base&lt;/span&gt;
  extend Yaffle
&lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
&lt;span&gt;class&lt;/span&gt; Article &lt;span&gt;&amp;lt;&lt;/span&gt; &lt;span&gt;ActiveRecord::Base&lt;/span&gt;
  acts_as_yaffle
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In a nutshell, there&amp;#8217;s no point in overriding &lt;code&gt;include&lt;/code&gt; to behave like &lt;code&gt;extend&lt;/code&gt; when Ruby provides both!&lt;/p&gt;
&lt;p&gt;To take this a bit further, you could do:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;module&lt;/span&gt; Yaffle
  &lt;span&gt;# any method placed here will apply to instances, like @hickwall, &lt;/span&gt;
  &lt;span&gt;# because that's how modules work!&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;To be used via:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;class&lt;/span&gt; Article &lt;span&gt;&amp;lt;&lt;/span&gt; &lt;span&gt;ActiveRecord::Base&lt;/span&gt;
  &lt;span&gt;include&lt;/span&gt; Yaffle
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In effect, the initial code (override included hook to extend a method on, which then includes a module) is two layers of abstraction around a simple Ruby include!&lt;/p&gt;
&lt;p&gt;Let&amp;#8217;s look at a few more examples:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;module&lt;/span&gt; Yaffle
  &lt;span&gt;def&lt;/span&gt; &lt;span&gt;self&lt;/span&gt;.&lt;span&gt;included&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;base&lt;span&gt;&amp;#41;&lt;/span&gt;
    base.&lt;span&gt;send&lt;/span&gt; &lt;span&gt;:extend&lt;/span&gt;, ClassMethods
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;module&lt;/span&gt; ClassMethods
    &lt;span&gt;def&lt;/span&gt; acts_as_yaffle&lt;span&gt;&amp;#40;&lt;/span&gt;options = &lt;span&gt;&amp;#123;&lt;/span&gt;&lt;span&gt;&amp;#125;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;
      cattr_accessor &lt;span&gt;:yaffle_text_field&lt;/span&gt;
      &lt;span&gt;self&lt;/span&gt;.&lt;span&gt;yaffle_text_field&lt;/span&gt; = &lt;span&gt;&amp;#40;&lt;/span&gt;options&lt;span&gt;&amp;#91;&lt;/span&gt;&lt;span&gt;:yaffle_text_field&lt;/span&gt;&lt;span&gt;&amp;#93;&lt;/span&gt; &lt;span&gt;||&lt;/span&gt; &lt;span&gt;:last_squawk&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;.&lt;span&gt;to_s&lt;/span&gt;
    &lt;span&gt;end&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
&lt;span&gt;ActiveRecord::Base&lt;/span&gt;.&lt;span&gt;send&lt;/span&gt; &lt;span&gt;:include&lt;/span&gt;, Yaffle&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Again, we have the idiom of overriding &lt;code&gt;include&lt;/code&gt; to behave like &lt;code&gt;extend&lt;/code&gt; (instead of just using &lt;code&gt;extend&lt;/code&gt;!).&lt;/p&gt;
&lt;p&gt;A better solution:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;module&lt;/span&gt; Yaffle
  &lt;span&gt;def&lt;/span&gt; acts_as_yaffle&lt;span&gt;&amp;#40;&lt;/span&gt;options = &lt;span&gt;&amp;#123;&lt;/span&gt;&lt;span&gt;&amp;#125;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;
    cattr_accessor &lt;span&gt;:yaffle_text_field&lt;/span&gt;
    &lt;span&gt;self&lt;/span&gt;.&lt;span&gt;yaffle_text_field&lt;/span&gt; = options&lt;span&gt;&amp;#91;&lt;/span&gt;&lt;span&gt;:yaffle_text_field&lt;/span&gt;&lt;span&gt;&amp;#93;&lt;/span&gt;.&lt;span&gt;to_s&lt;/span&gt; &lt;span&gt;||&lt;/span&gt; &lt;span&gt;&amp;quot;last_squawk&amp;quot;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
&lt;span&gt;ActiveRecord::Base&lt;/span&gt;.&lt;span&gt;extend&lt;/span&gt; Yaffle&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In this case, it&amp;#8217;s appropriate to use an &lt;code&gt;acts_as_yaffle&lt;/code&gt;, since you&amp;#8217;re providing additional options which could not be encapsulated using the normal Ruby &lt;code&gt;extend&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Another &amp;#8220;more advanced&amp;#8221; case:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;module&lt;/span&gt; Yaffle
  &lt;span&gt;def&lt;/span&gt; &lt;span&gt;self&lt;/span&gt;.&lt;span&gt;included&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;base&lt;span&gt;&amp;#41;&lt;/span&gt;
    base.&lt;span&gt;send&lt;/span&gt; &lt;span&gt;:extend&lt;/span&gt;, ClassMethods
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;module&lt;/span&gt; ClassMethods
    &lt;span&gt;def&lt;/span&gt; acts_as_yaffle&lt;span&gt;&amp;#40;&lt;/span&gt;options = &lt;span&gt;&amp;#123;&lt;/span&gt;&lt;span&gt;&amp;#125;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;
      cattr_accessor &lt;span&gt;:yaffle_text_field&lt;/span&gt;
      &lt;span&gt;self&lt;/span&gt;.&lt;span&gt;yaffle_text_field&lt;/span&gt; = &lt;span&gt;&amp;#40;&lt;/span&gt;options&lt;span&gt;&amp;#91;&lt;/span&gt;&lt;span&gt;:yaffle_text_field&lt;/span&gt;&lt;span&gt;&amp;#93;&lt;/span&gt; &lt;span&gt;||&lt;/span&gt; &lt;span&gt;:last_squawk&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;.&lt;span&gt;to_s&lt;/span&gt;
      send &lt;span&gt;:include&lt;/span&gt;, InstanceMethods
    &lt;span&gt;end&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;module&lt;/span&gt; InstanceMethods
    &lt;span&gt;def&lt;/span&gt; squawk&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;string&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;
      write_attribute&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;self&lt;/span&gt;.&lt;span&gt;class&lt;/span&gt;.&lt;span&gt;yaffle_text_field&lt;/span&gt;, &lt;span&gt;string&lt;/span&gt;.&lt;span&gt;to_squawk&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;
    &lt;span&gt;end&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
&lt;span&gt;ActiveRecord::Base&lt;/span&gt;.&lt;span&gt;send&lt;/span&gt; &lt;span&gt;:include&lt;/span&gt;, Yaffle&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Again, we have the idiom of overriding include to pretend to be an extend, and a &lt;code&gt;send&lt;/code&gt; where it is not needed. Identical functionality:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;module&lt;/span&gt; Yaffle
  &lt;span&gt;def&lt;/span&gt; acts_as_yaffle&lt;span&gt;&amp;#40;&lt;/span&gt;options = &lt;span&gt;&amp;#123;&lt;/span&gt;&lt;span&gt;&amp;#125;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;
    cattr_accessor &lt;span&gt;:yaffle_text_field&lt;/span&gt;
    &lt;span&gt;self&lt;/span&gt;.&lt;span&gt;yaffle_text_field&lt;/span&gt; = &lt;span&gt;&amp;#40;&lt;/span&gt;options&lt;span&gt;&amp;#91;&lt;/span&gt;&lt;span&gt;:yaffle_text_field&lt;/span&gt;&lt;span&gt;&amp;#93;&lt;/span&gt; &lt;span&gt;||&lt;/span&gt; &lt;span&gt;:last_squawk&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;.&lt;span&gt;to_s&lt;/span&gt;
    &lt;span&gt;include&lt;/span&gt; InstanceMethods
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;module&lt;/span&gt; InstanceMethods
    &lt;span&gt;def&lt;/span&gt; squawk&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;string&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;
      write_attribute&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;self&lt;/span&gt;.&lt;span&gt;class&lt;/span&gt;.&lt;span&gt;yaffle_text_field&lt;/span&gt;, &lt;span&gt;string&lt;/span&gt;.&lt;span&gt;to_squawk&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;
    &lt;span&gt;end&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
&lt;span&gt;ActiveRecord::Base&lt;/span&gt;.&lt;span&gt;extend&lt;/span&gt; Yaffle&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Of course, it is also possible to do:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;module&lt;/span&gt; Yaffle
  &lt;span&gt;def&lt;/span&gt; squawk&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;string&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;
    write_attribute&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;self&lt;/span&gt;.&lt;span&gt;class&lt;/span&gt;.&lt;span&gt;yaffle_text_field&lt;/span&gt;, &lt;span&gt;string&lt;/span&gt;.&lt;span&gt;to_squawk&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
&lt;span&gt;class&lt;/span&gt; &lt;span&gt;ActiveRecord::Base&lt;/span&gt;
  &lt;span&gt;def&lt;/span&gt; &lt;span&gt;self&lt;/span&gt;.&lt;span&gt;acts_as_yaffle&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;options = &lt;span&gt;&amp;#123;&lt;/span&gt;&lt;span&gt;&amp;#125;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;
    cattr_accessor &lt;span&gt;:yaffle_text_field&lt;/span&gt;
    &lt;span&gt;self&lt;/span&gt;.&lt;span&gt;yaffle_text_field&lt;/span&gt; = &lt;span&gt;&amp;#40;&lt;/span&gt;options&lt;span&gt;&amp;#91;&lt;/span&gt;&lt;span&gt;:yaffle_text_field&lt;/span&gt;&lt;span&gt;&amp;#93;&lt;/span&gt; &lt;span&gt;||&lt;/span&gt; &lt;span&gt;:last_squawk&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;.&lt;span&gt;to_s&lt;/span&gt;
    &lt;span&gt;include&lt;/span&gt; Yaffle
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Since the module is &lt;strong&gt;always&lt;/strong&gt; included in &lt;code&gt;ActiveRecord::Base&lt;/code&gt;, there is no reason that the earlier code, with its additional modules and use of extend, is superior to simply reopening the class and adding the &lt;code&gt;acts_as_yaffle&lt;/code&gt; method directly. Now we can put the &lt;code&gt;squawk&lt;/code&gt; method directly inside the &lt;code&gt;Yaffle&lt;/code&gt; module, where it can be included cleanly.&lt;/p&gt;
&lt;p&gt;It may not seem like a huge deal, but it significantly reduces the amount of &lt;strong&gt;apparent&lt;/strong&gt; magic in the plugin pattern, making it more accessible for new users. Additionally, it exposes the new user to &lt;code&gt;include&lt;/code&gt; and &lt;code&gt;extend&lt;/code&gt; quickly, instead of making them feel as though they were magic incantations requiring the use of &lt;code&gt;send&lt;/code&gt; and special modules named &lt;code&gt;ClassMethods&lt;/code&gt; in order to get them to work.&lt;/p&gt;
&lt;p&gt;To be clear, I&amp;#8217;m not saying that these idioms aren&amp;#8217;t sometimes needed in special, advanced cases. However, I am saying that in the most common cases, they&amp;#8217;re huge overkill that obscures the real functionality and confuses users.&lt;/p&gt;

&lt;div class=&quot;feedflare&quot;&gt;
&lt;a href=&quot;http://feeds.feedburner.com/~ff/KatzGotYourTongue?a=VWgrh-jlpA4:doJ5NIDm5Uw:yIl2AUoC8zA&quot;&gt;&lt;img src=&quot;http://feeds.feedburner.com/~ff/KatzGotYourTongue?d=yIl2AUoC8zA&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src=&quot;http://feeds.feedburner.com/~r/KatzGotYourTongue/~4/VWgrh-jlpA4&quot; height=&quot;1&quot; width=&quot;1&quot; /&gt;</content>
		<author>
			<name>wycats</name>
			<uri>http://yehudakatz.com</uri>
		</author>
		<source>
			<title type="html">Katz Got Your Tongue?</title>
			<subtitle type="html">Random Geek-Related Thoughts</subtitle>
			<link rel="self" href="http://yehudakatz.com/feed/atom/"/>
			<id>http://yehudakatz.com/feed/atom/</id>
			<updated>2010-03-13T08:20:07+00:00</updated>
		</source>
	</entry>

	<entry xml:lang="en">
		<title type="html">The Ruby revolution is over</title>
		<link href="http://merbist.com/2009/11/09/the-ruby-revolution-is-over/"/>
		<id>http://merbist.com/?p=619</id>
		<updated>2009-11-09T08:10:16+00:00</updated>
		<content type="html">&lt;p&gt;&lt;img class=&quot;aligncenter&quot; title=&quot;Ruby revolution&quot; src=&quot;http://upload.wikimedia.org/wikipedia/commons/thumb/a/a7/Eug%C3%A8ne_Delacroix_-_La_libert%C3%A9_guidant_le_peuple.jpg/300px-Eug%C3%A8ne_Delacroix_-_La_libert%C3%A9_guidant_le_peuple.jpg&quot; alt=&quot;Liberté guidant le peuple - Eugene Delacroix posted by Matt Aimonetti&quot; title=&quot;Liberté guidant le peuple - Eugene Delacroix posted by Matt Aimonetti&quot; width=&quot;300&quot; height=&quot;238&quot; /&gt;&lt;/p&gt;
&lt;p&gt;According to wikipedia, a &lt;strong&gt;revolution&lt;/strong&gt; (from the latin &lt;em&gt;revolutio&lt;/em&gt;, &amp;#8220;a turn around&amp;#8221;) is a fundamental change in power or organizational structures that takes place in a relatively short period of time.&lt;/p&gt;
&lt;p&gt;Somehow, I believe this is exactly what Ruby has done in the programming world, especially with the help of Rails. Over the last few years, Ruby lead a mini revolution in the midst of software development. Thanks to Ruby, developers now look at software development differently. One thing for sure, it pushed DHH to write Rails and then convinced thousands of people to develop Ruby based applications on a daily basis.&lt;/p&gt;
&lt;h3&gt;How did it happen?&lt;/h3&gt;
&lt;p&gt;Let&amp;#8217;s take a look at history of revolutions. Some people get frustrated their situation, they try to find workarounds until it&amp;#8217;s just too much and the revolution kicks in.&lt;/p&gt;
&lt;p&gt;Ruby came up with a new holistic perspective on things. Unlike most other programming languages, one of the main key value of Ruby is that writing code should feel right for the developer. You feel good about it because the language was written for humans and not machines. Basically, the language was designed to make you productive because it&amp;#8217;s designed to please you.&lt;/p&gt;
&lt;p&gt;As people were discovering web 2.0, Ruby also came with an opinionated framework, pushing for productivity, testing, simplicity and elegance. People started to see a new way of doing things and it quickly became the new, cool technology. Rails became a buzz word, developers were hired to work on cool projects, and books were selling by the thousands.&lt;/p&gt;
&lt;h3&gt;What did it change?&lt;/h3&gt;
&lt;p&gt;If you ask my mom, she would probably say: nothing, except that now my son works from his home office and he seems to really enjoy what he does for living.&lt;/p&gt;
&lt;p&gt;Relatively speaking, Ruby did not change the way we work or live. However, I believe that it has influenced many software developers around the globe. Why else do you think that companies like Microsoft, Apple or SAP are working on their own implementation of the Ruby language?&lt;/p&gt;
&lt;p&gt;When I first discovered Ruby, I was amazed at how &amp;#8220;right&amp;#8221; it felt, at how much fun it was to write code using its syntax and idioms. Now, if I don&amp;#8217;t get that feeling when testing a programming language, I think there is something wrong.&lt;/p&gt;
&lt;p&gt;The Ruby community also revived the Agile/XP world. Testing being a strong value of the community, we spent a lot of time discussing TDD, BDD, integration test as well as other practices such as pair programming, code review, sprints etc..&lt;/p&gt;
&lt;p&gt;A few years ago, when people were asking me what programming language I would write their app in, I would reply Ruby and had to explain what it was, why it is great and would have to answer a lot of questions from potential clients. Nowadays, people don&amp;#8217;t even argue, sites like hulu.com, twitter.com, yellowpages.com and many others are written in Ruby and it&amp;#8217;s just part of the tools known to work very well.&lt;/p&gt;
&lt;h3&gt;The revolution is over!&lt;/h3&gt;
&lt;p&gt;Yes, Ruby made it&amp;#8217;s revolution and the world &amp;#8220;has changed&amp;#8221;. But a real movement doesn&amp;#8217;t die after its revolution, that&amp;#8217;s actually when it has to be strong and defend its values.&lt;/p&gt;
&lt;p&gt;This doesn&amp;#8217;t mean that Ruby is dead or that Rails is &amp;#8220;passé&amp;#8221;. To the contrary, Ruby imposed itself as a new valued and respected player, a new standard if you will.&lt;/p&gt;
&lt;p&gt;Ruby is certainly not the &amp;#8220;new kid in the block&amp;#8221;anymore nor the &amp;#8220;popular kid&amp;#8221;, however lots of older kids seem to want to have her on their team. (.NET, Java, Objective-C can all use Ruby)&lt;/p&gt;
&lt;p&gt;The TDD + Ruby combo doesn&amp;#8217;t surprise anyone anymore and the Enterprise is slowly but surely adopting Ruby. Ruby is now just getting better, tools and libraries are improving and the amount of users is growing.&lt;/p&gt;
&lt;p&gt;Certainly the Ruby community is still small compared to other software developer communities, but the fundamental change was done and we are now working on improvement and keeping things running smoothly, growing and getting new ideas inspired by our experience and other communities.&lt;/p&gt;
&lt;p&gt;Long live Ruby!&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://merbist.com/2009/11/16/the-ruby-revolution-take-ii/&quot; target=&quot;_blank&quot;&gt;See my follow up.&lt;/a&gt;&lt;/p&gt;</content>
		<author>
			<name>Matt Aimonetti (mattetti)</name>
			<uri>http://merbist.com</uri>
		</author>
		<source>
			<title type="html">Merbist</title>
			<subtitle type="html">Rails 3, Ruby &amp;amp; MacRuby news - tutorials - consulting - training</subtitle>
			<link rel="self" href="http://merbist.com/feed/"/>
			<id>http://merbist.com/feed/</id>
			<updated>2010-03-13T12:20:05+00:00</updated>
		</source>
	</entry>

	<entry xml:lang="en">
		<title type="html">Using the New Gem Bundler Today</title>
		<link href="http://feedproxy.google.com/~r/KatzGotYourTongue/~3/_izngKWHh7s/"/>
		<id>http://yehudakatz.com/?p=354</id>
		<updated>2009-11-03T09:53:40+00:00</updated>
		<content type="html">&lt;p&gt;As you might have heard, Carl and I released a new project that allows you to bundle your gems (both pure-ruby and native) with your application. Before I get into the process for using the bundler today, I&amp;#8217;d like to go into the design goals of the project.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The bundler should allow the specification of all dependencies in a separate place from the application itself. In other words, it should be possible to determine the dependencies for an application without needing to start up the application.&lt;/li&gt;
&lt;li&gt;The bundler should have a built-in dependency resolving mechanism, so it can determine the required gems for an entire set of dependencies.&lt;/li&gt;
&lt;li&gt;Once the dependencies are resolved, it should be possible to get the application up and running on a new system without needing to check Rubyforge (or gemcutter) again. This is especially important for compiled gems (it should be possible to get the list of required gems once and compile on remote systems as desired).&lt;/li&gt;
&lt;li&gt;Above all else, the bundler should provide a reproducible installation of Ruby applications. New gem releases or down remote servers should not be able to impact the successful installation of an application. In most cases, &lt;code&gt;git clone; gem bundle&lt;/code&gt; should be all that is needed to get an application on a new system and up and running.&lt;/li&gt;
&lt;li&gt;Finally, the bundler should not assume anything about Rails applications. While it should work flawlessly in the context of a Rails application, this should be because a Rails application is a Ruby application.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Using the Bundler Today&lt;/h3&gt;
&lt;p&gt;To use the gem bundler today in a non-Rails application, follow the following steps:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;code&gt;gem install bundler&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Create a &lt;code&gt;Gemfile&lt;/code&gt; in the root of your application&lt;/li&gt;
&lt;li&gt;Add dependencies to your Gemfile. See below for more details on the sorts of things you can do in the Gemfile. At the simplest level, &lt;code&gt;gem &quot;gem_name&quot;, &quot;version&quot;&lt;/code&gt; will add a dependency of the gem and version to your application&lt;/li&gt;
&lt;li&gt;At the root, run &lt;code&gt;gem bundle&lt;/code&gt;. The bundler should tell you that it is resolving dependencies, then downloading and installing the gems.&lt;/li&gt;
&lt;li&gt;Add &lt;code&gt;vendor/gems/gems&lt;/code&gt;, &lt;code&gt;vendor/gems/specifications&lt;/code&gt;, &lt;code&gt;vendor/gems/doc&lt;/code&gt;, and &lt;code&gt;vendor/gems/environment.rb&lt;/code&gt; to your &lt;code&gt;.gitignore file&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Inside your application, require &lt;code&gt;vendor/gems/environment.rb&lt;/code&gt; to add the bundled dependencies to your load paths.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;Bundler.require_env :optional_environment&lt;/code&gt; to actually require the files.&lt;/li&gt;
&lt;li&gt;After committing, run &lt;code&gt;gem bundle&lt;/code&gt; in a fresh clone to re-expand the gems. Since you left the &lt;code&gt;vendor/gems/cache&lt;/code&gt; in your source control, new machines will be guaranteed to use the same files as the original machine, requiring no remote dependencies&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The bundler will also install binaries into the app&amp;#8217;s &lt;code&gt;bin&lt;/code&gt; directory. You can, therefore, run &lt;code&gt;bin/rackup&lt;/code&gt; for instance, which will ensure that the local bundle, rather than the system, is used. You can also run &lt;code&gt;gem exec rackup&lt;/code&gt;, which runs any command using the local bundle. This allows things like &lt;code&gt;gem exec ruby -e &quot;puts Nokogiri::VERSION&quot;&lt;/code&gt; or the even more adventurous &lt;code&gt;gem exec bash&lt;/code&gt;, which will open a new shell in the context of the bundle.&lt;/p&gt;
&lt;h3&gt;Gemfile&lt;/h3&gt;
&lt;p&gt;You can do any of the following in the Gemfile:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;gem &quot;name&quot;, &quot;version&quot;&lt;/code&gt;: &lt;code&gt;version&lt;/code&gt; may be a strict version or a version requirement like &lt;code&gt;&amp;amp;gt;= 1.0.6&lt;/code&gt;. The version is optional.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;gem &quot;name&quot;, &quot;version&quot;, :require_as =&amp;amp;gt; &quot;file&quot;&lt;/code&gt;: the &lt;code&gt;require_as&lt;/code&gt; allows you to specify which file should be required when the &lt;code&gt;require_env&lt;/code&gt; is called. By default, it is the gem&amp;#8217;s name&lt;/li&gt;
&lt;li&gt;&lt;code&gt;gem &quot;name&quot;, &quot;version&quot;, :only =&amp;amp;gt; :testing&lt;/code&gt;: The environment name can be anything. It is used later in your &lt;code&gt;require_env&lt;/code&gt; call. You may specify either &lt;code&gt;:only&lt;/code&gt;, or &lt;code&gt;:except&lt;/code&gt; constraints&lt;/li&gt;
&lt;li&gt;&lt;code&gt;gem &quot;name&quot;, &quot;version&quot;, :git =&amp;amp;gt; &quot;git://github.com/wycats/thor&quot;&lt;/code&gt;: Specify a git repository to be used to satisfy the dependency. You must use a hard dependency (&amp;#8220;1.0.6&amp;#8243;) rather than a soft dependency (&amp;#8220;&amp;gt;= 1.0.6&amp;#8243;). If a .gemspec is found in the repository, it is used for further dependency lookup. If the repository has multiple .gemspecs, each directory will a .gemspec will be considered a gem.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;gem &quot;name&quot;, &quot;version&quot;, :git =&amp;amp;gt; &quot;git://github.com/wycats/thor&quot;, :branch =&amp;amp;gt; &quot;experimental&quot;&lt;/code&gt;: Further specify a branch, tag, or ref to use. All of :branch, :tag, and :ref are valid options&lt;/li&gt;
&lt;li&gt;&lt;code&gt;gem &quot;name&quot;, &quot;version&quot;, :vendored_at =&amp;amp;gt; &quot;vendor/nokogiri&quot;&lt;/code&gt;: In the next version of bundler, this option will be changing to &lt;code&gt;:path&lt;/code&gt;. This specifies that the dependency can be found in the local file system, rather than remotely. It is resolved relative to the location of the Gemfile&lt;/li&gt;
&lt;li&gt;&lt;code&gt;clear_sources&lt;/code&gt;: Empties the list of gem sources to search inside of.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;source &quot;http://gems.github.com&quot;&lt;/code&gt;: Adds a gem source to the list of available gem sources.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;bundle_path &quot;vendor/my_gems&quot;&lt;/code&gt;: Changes the default location of bundled gems from &lt;code&gt;vendor/gems&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;bin_path &quot;my_executables&quot;&lt;/code&gt;: Changes the default location of the installed executables&lt;/li&gt;
&lt;li&gt;&lt;code&gt;disable_system_gems&lt;/code&gt;: Without this command, both bundled gems and system gems will be used. You can therefore have things like &lt;code&gt;ruby-debug&lt;/code&gt; in your system and use it. However, it also means that you may be using something in development mode that is installed on your system but not available in production. For this reason, it is best to &lt;code&gt;disable_system_gems&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;disable_rubygems&lt;/code&gt;: This completely disables rubygems, reducing startup times considerably. However, it often doesn&amp;#8217;t work if libraries you are using depend on features of Rubygems. In this mode, the bundler shims the features of Rubygems that we know people are using, but it&amp;#8217;s possible that someone is using a feature we&amp;#8217;re unaware of. You are free to try &lt;code&gt;disable_rubygems&lt;/code&gt; first, then remove it if it doesn&amp;#8217;t work. Note that Rails 2.3 cannot be made to work in this mode&lt;/li&gt;
&lt;li&gt;&lt;code&gt;only :environment { gem &quot;rails&quot; }&lt;/code&gt;: You can use &lt;code&gt;only&lt;/code&gt; or &lt;code&gt;except&lt;/code&gt; in block mode to specify a number of gems at once&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Bundler process&lt;/h3&gt;
&lt;p&gt;When you run &lt;code&gt;gem bundle&lt;/code&gt;, a few things happen. First, the bundler attempts to resolve your list of dependencies against the gems you have already bundled. If they don&amp;#8217;t resolve, the metadata for each specified source is fetched and the gems are downloaded. Next (either way), the bundler checks to see whether the downloaded gems are expanded. For any gem that is not yet expanded, the bundler expands it. Finally, the bundler creates the &lt;code&gt;environment.rb&lt;/code&gt; file with the new settings. This means that running &lt;code&gt;gem bundler&lt;/code&gt; over and over again will be extremely fast, because after the first time, all gems are downloaded and expanded. If you change settings, like &lt;code&gt;disable_rubygems&lt;/code&gt;, running &lt;code&gt;gem bundle&lt;/code&gt; again will simply regenerate the &lt;code&gt;environment.rb&lt;/code&gt;.&lt;/p&gt;
&lt;h3&gt;Rails 2.3&lt;/h3&gt;
&lt;p&gt;To get this working with Rails 2.3, you need to create a &lt;code&gt;preinitializer.rb&lt;/code&gt; and insert the following:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;require&lt;/span&gt; &lt;span&gt;&amp;quot;#{File.dirname(__FILE__)}/../vendor/bundler_gems/environment&amp;quot;&lt;/span&gt;
&amp;nbsp;
&lt;span&gt;class&lt;/span&gt; &lt;span&gt;Rails::Boot&lt;/span&gt;
  &lt;span&gt;def&lt;/span&gt; run
    load_initializer
    extend_environment
    &lt;span&gt;Rails::Initializer&lt;/span&gt;.&lt;span&gt;run&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;:set_load_path&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;def&lt;/span&gt; extend_environment
    &lt;span&gt;Rails::Initializer&lt;/span&gt;.&lt;span&gt;class_eval&lt;/span&gt; &lt;span&gt;do&lt;/span&gt;
      old_load = instance_method&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;:load_environment&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;
      define_method&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;:load_environment&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt; &lt;span&gt;do&lt;/span&gt;
        Bundler.&lt;span&gt;require_env&lt;/span&gt; RAILS_ENV
        old_load.&lt;span&gt;bind&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;self&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;.&lt;span&gt;call&lt;/span&gt;
      &lt;span&gt;end&lt;/span&gt;
    &lt;span&gt;end&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;It&amp;#8217;s a bit ugly, but you can copy and paste that code and forget it. Astute readers will notice that we&amp;#8217;re using &lt;code&gt;vendor/bundler_gems/environment.rb&lt;/code&gt;. This is because Rails 2.3 attaches special, irrevocable meaning to &lt;code&gt;vendor/gems&lt;/code&gt;. As a result, make sure to do the following in your &lt;code&gt;Gemfile&lt;/code&gt;: &lt;code&gt;bundle_path &quot;vendor/bundler_gems&quot;&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Gemcutter uses this setup and it&amp;#8217;s working great for them.&lt;/p&gt;
&lt;h3&gt;Bundler 0.7&lt;/h3&gt;
&lt;p&gt;We&amp;#8217;re going to be releasing Bundler 0.7 tomorrow. It has some new features:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;List outdated gems by passing &lt;code&gt;--outdated-gems&lt;/code&gt;. Bundler conservatively does not update your gems simply because a new version came out that satisfies the requirement. This is so that you can be sure that the versions running on your local machine will make it safely to production. This will allow you to check for outdated gems so you can decide whether to update your gems with &amp;#8211;update. Hat tip to &lt;code&gt;manfred&lt;/code&gt;, who submitted this patch as part of his submission to the &lt;a href=&quot;http://2009.rubyenrails.nl/&quot;&gt;Rumble at Ruby en Rails&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Specify the build requirements for gems in a YAML file that you specify with &lt;code&gt;--build-options&lt;/code&gt;. The file looks something like this:

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;yaml&quot;&gt;mysql:
  config: /path/to/mysql_config&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This is equivalent to &amp;#8211;with-mysql-config=/path/to/mysql_config&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;Specify &lt;code&gt;:bundle =&amp;amp;gt; false&lt;/code&gt; to indicate that you want to use system gems for a particular dependency. This will ensure that it gets resolved correctly during dependency resolution but does not need to be included in the bundle&lt;/li&gt;
&lt;li&gt;Support for multiple bundles containing multiple platforms. This is especially useful for people moving back and forth between Ruby 1.8 and 1.9 and don&amp;#8217;t want to constantly have to nuke and rebundle&lt;/li&gt;
&lt;li&gt;Deprecate &lt;code&gt;:vendored_at&lt;/code&gt; and replace with &lt;code&gt;:path&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;A new &lt;code&gt;directory&lt;/code&gt; DSL method in the &lt;code&gt;Gemfile&lt;/code&gt;:

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;directory &lt;span&gt;&amp;quot;vendor/rails&amp;quot;&lt;/span&gt; &lt;span&gt;do&lt;/span&gt;
  gem &lt;span&gt;&amp;quot;activesupport&amp;quot;&lt;/span&gt;, &lt;span&gt;:path&lt;/span&gt; =&lt;span&gt;&amp;amp;&lt;/span&gt;gt; &lt;span&gt;&amp;quot;activesupport&amp;quot;&lt;/span&gt; &lt;span&gt;# :path is optional if it's identical to the gem name&lt;/span&gt;
                                                &lt;span&gt;# the version is optional if it can be determined from&lt;/span&gt;
                                                &lt;span&gt;# a gemspec&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;/li&gt;
&lt;li&gt;You can do the same with the &lt;code&gt;git&lt;/code&gt; DSL method

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;git &lt;span&gt;&amp;quot;git://github.com/rails/rails.git&amp;quot;&lt;/span&gt; &lt;span&gt;do&lt;/span&gt; &lt;span&gt;# :branch, :tag, or :ref work here&lt;/span&gt;
  gem &lt;span&gt;&amp;quot;activesupport&amp;quot;&lt;/span&gt;, &lt;span&gt;:path&lt;/span&gt; =&lt;span&gt;&amp;amp;&lt;/span&gt;gt; &lt;span&gt;&amp;quot;activesupport&amp;quot;&lt;/span&gt; &lt;span&gt;# same rules as directory, except that the files are&lt;/span&gt;
                                                &lt;span&gt;# first downloaded from git.&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;/li&gt;
&lt;li&gt;Fix some bugs in resolving prerelease dependencies&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;feedflare&quot;&gt;
&lt;a href=&quot;http://feeds.feedburner.com/~ff/KatzGotYourTongue?a=_izngKWHh7s:uvctsio1_xQ:yIl2AUoC8zA&quot;&gt;&lt;img src=&quot;http://feeds.feedburner.com/~ff/KatzGotYourTongue?d=yIl2AUoC8zA&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src=&quot;http://feeds.feedburner.com/~r/KatzGotYourTongue/~4/_izngKWHh7s&quot; height=&quot;1&quot; width=&quot;1&quot; /&gt;</content>
		<author>
			<name>wycats</name>
			<uri>http://yehudakatz.com</uri>
		</author>
		<source>
			<title type="html">Katz Got Your Tongue?</title>
			<subtitle type="html">Random Geek-Related Thoughts</subtitle>
			<link rel="self" href="http://yehudakatz.com/feed/atom/"/>
			<id>http://yehudakatz.com/feed/atom/</id>
			<updated>2010-03-13T08:20:07+00:00</updated>
		</source>
	</entry>

	<entry xml:lang="en">
		<title type="html">MacRuby tips: browse for folder or file dialog</title>
		<link href="http://merbist.com/2009/10/25/macruby-browse-for-folder-or-file-dialog/"/>
		<id>http://merbist.com/?p=624</id>
		<updated>2009-10-25T23:52:14+00:00</updated>
		<content type="html">&lt;p&gt;This is yet another pretty simple tip.&lt;br /&gt;
Use case: let say you want your applications users to choose one or multiple files or folder on their file system. A good example would be that you want the user to choose a file to process or a folder where to save some data.&lt;/p&gt;
&lt;p&gt;&lt;img class=&quot;aligncenter&quot; title=&quot;MacRuby, file browser dialog&quot; src=&quot;http://img.skitch.com/20091025-nc89xd2ywqutqqddnwm2met3x4.jpg&quot; alt=&quot;&quot; width=&quot;389&quot; height=&quot;43&quot; /&gt;&lt;/p&gt;
&lt;p&gt;In the example above, I added a browse button and a text field.&lt;/p&gt;
&lt;p&gt;I would like my users to click on the browse button, locate a folder and display it in the text field.&lt;/p&gt;
&lt;p&gt;In your MacRuby controller, use a simple action method as well as an accessor to the text field:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;line_numbers&quot;&gt;&lt;pre&gt;1
2
3
4
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;attr_accessor &lt;span&gt;:destination_path&lt;/span&gt;
&amp;nbsp;
&lt;span&gt;def&lt;/span&gt; browse&lt;span&gt;&amp;#40;&lt;/span&gt;sender&lt;span&gt;&amp;#41;&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Now, in Interface builder bind the destination_path outlet to the text field you want to use to display the path and bind the button to the browse action.&lt;/p&gt;
&lt;p&gt;Let&amp;#8217;s go back to our action method and let&amp;#8217;s create a dialog panel, set some options and handle the user selection:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;line_numbers&quot;&gt;&lt;pre&gt;1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;def&lt;/span&gt; browse&lt;span&gt;&amp;#40;&lt;/span&gt;sender&lt;span&gt;&amp;#41;&lt;/span&gt;
  &lt;span&gt;# Create the File Open Dialog class.&lt;/span&gt;
  dialog = NSOpenPanel.&lt;span&gt;openPanel&lt;/span&gt;
  &lt;span&gt;# Disable the selection of files in the dialog.&lt;/span&gt;
  dialog.&lt;span&gt;canChooseFiles&lt;/span&gt; = &lt;span&gt;false&lt;/span&gt;
  &lt;span&gt;# Enable the selection of directories in the dialog.&lt;/span&gt;
  dialog.&lt;span&gt;canChooseDirectories&lt;/span&gt; = &lt;span&gt;true&lt;/span&gt;
  &lt;span&gt;# Disable the selection of multiple items in the dialog.&lt;/span&gt;
  dialog.&lt;span&gt;allowsMultipleSelection&lt;/span&gt; = &lt;span&gt;false&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;# Display the dialog and process the selected folder&lt;/span&gt;
  &lt;span&gt;if&lt;/span&gt; dialog.&lt;span&gt;runModalForDirectory&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;nil&lt;/span&gt;, file:&lt;span&gt;nil&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt; == NSOKButton
  &lt;span&gt;# if we had a allowed for the selection of multiple items&lt;/span&gt;
  &lt;span&gt;# we would have want to loop through the selection&lt;/span&gt;
    destination_path.&lt;span&gt;stringValue&lt;/span&gt; = dialog.&lt;span&gt;filenames&lt;/span&gt;.&lt;span&gt;first&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;That&amp;#8217;s it, your user can now browse for a folder and the selection will be displayed in the text field. Look at the &lt;a href=&quot;http://developer.apple.com/mac/library/documentation/Cocoa/Reference/ApplicationKit/Classes/NSOpenPanel_Class/Reference/Reference.html&quot; target=&quot;_blank&quot;&gt;NSOpenPanel documentation&lt;/a&gt; for more details on the Cocoa API.&lt;/p&gt;</content>
		<author>
			<name>Matt Aimonetti (mattetti)</name>
			<uri>http://merbist.com</uri>
		</author>
		<source>
			<title type="html">Merbist</title>
			<subtitle type="html">Rails 3, Ruby &amp;amp; MacRuby news - tutorials - consulting - training</subtitle>
			<link rel="self" href="http://merbist.com/feed/"/>
			<id>http://merbist.com/feed/</id>
			<updated>2010-03-13T12:20:05+00:00</updated>
		</source>
	</entry>

	<entry xml:lang="en">
		<title type="html">RailsSummit – an amazing adventure</title>
		<link href="http://merbist.com/2009/10/23/railssummit-2009/"/>
		<id>http://merbist.com/?p=613</id>
		<updated>2009-10-23T18:17:45+00:00</updated>
		<content type="html">&lt;p&gt;I have had the opportunity to go to and speak at many conferences but this year was the first time I had the chance to go to &lt;a href=&quot;http://railssummit.com.br&quot; target=&quot;_blank&quot;&gt;RailsSummit&lt;/a&gt; in São Paulo, Brazil.&lt;/p&gt;
&lt;p&gt;I was really looking forward to this trip and I have to say it went beyond my expectations. I had really good feedback from people like the &lt;a href=&quot;http://www.phusion.nl/&quot; target=&quot;_blank&quot;&gt;Phusion guys&lt;/a&gt; (Ninh &amp;amp; Hongli), Gregg Pollack and others.&lt;/p&gt;
&lt;p&gt;For those who don&amp;#8217;t know, RailsSummit is one of the biggest, if not the biggest, Ruby event in Latin America. It&amp;#8217;s organized yearly by &lt;a href=&quot;http://www.locaweb.com.br/&quot; target=&quot;_blank&quot;&gt;Locaweb&lt;/a&gt;, the #1 hosting company in Brazil (props to Akita &amp;amp; Cristiane for their work).&lt;/p&gt;
&lt;p&gt;&lt;img class=&quot;alignleft&quot; title=&quot;railssummit conf&quot; src=&quot;http://farm3.static.flickr.com/2761/4027342992_789777fd03_m_d.jpg&quot; alt=&quot;&quot; width=&quot;360&quot; height=&quot;240&quot; /&gt;&lt;br /&gt;&lt;/p&gt;
&lt;p&gt;As part of my involvement in the Ruby community I have met a lot of Brazilians always willing to help and especially giving time to translate content. (A good example would be the &lt;a href=&quot;http://wiki.rubyonrails.org/pt/start&quot; target=&quot;_blank&quot;&gt;Portuguese version of the Rails wiki&lt;/a&gt; or &lt;a href=&quot;http://blog.plataformatec.com.br/&quot; target=&quot;_blank&quot;&gt;José Valim&lt;/a&gt; GSOC contribution) However, I did not realize how fast Rails was growing over here.&lt;/p&gt;
&lt;p&gt;To come back to the conference, I have to say it was one of the best conference I have gone to. Chad Fowler, who gave the opening keynote, later told me that it reminded him of the first Ruby conferences in the US . For me, what made a huge difference was the fact that it was a very positive conference. People were happy to be here, eager to share and you could feel the passion. Unfortunately, I missed the first few Ruby conferences, but I can totally imagine how must have been. Passionate people, not trying to push their products but instead, share the cool stuff they&amp;#8217;ve been working on. This is exactly the feeling I had during this conference.&lt;br /&gt;&lt;/p&gt;
&lt;p&gt;Maybe it&amp;#8217;s because I don&amp;#8217;t understand Portuguese well enough or maybe it&amp;#8217;s just a cultural thing, but the people at the conference were just super friendly and always willing to help. I was really glad to meet those who have been using some of my work, some new people to Ruby and people who don&amp;#8217;t do Ruby but were just interested in knowing what was going on in the Ruby world.&lt;/p&gt;
&lt;p&gt;&lt;img class=&quot;alignright&quot; title=&quot;railssummit speakers&quot; src=&quot;http://farm3.static.flickr.com/2719/4027357802_1082f38090.jpg&quot; alt=&quot;&quot; width=&quot;500&quot; height=&quot;333&quot; /&gt;&lt;/p&gt;
&lt;p&gt;The schedule was pretty packed and the discussions very interesting, you could certainly feel the excitement in the air. Ruby seems to catching up quickly over there. Brazilian Rubyists seem to be very pragmatic and a good illustration of that was certainly made by &lt;a href=&quot;http://improveit.com.br/en&quot; target=&quot;_blank&quot;&gt;Vinicus Teles&lt;/a&gt;, Brazil Agile XPert, who shared tips on how to release a successful product.&lt;/p&gt;
&lt;p&gt;I stayed a few days after the conference and went to visit the Rubyists in Rio. Rio is a great city. It has some of the best soccer players in the world and some seriously talented software developers. Ruby &amp;amp; Rails are not just the new trendy startup secret to success, companies like globo.com, currently the largest TV network in &lt;a title=&quot;Latin America&quot; href=&quot;http://en.wikipedia.org/wiki/Latin_America&quot;&gt;Latin America&lt;/a&gt; and the fourth largest in the world, also started using Ruby and Rails. I had the opportunity to visit their office and meet their teams. It&amp;#8217;s very exciting to see how they use Agile/XP and Ruby and how they seem to be so successful. But I will keep that for another post.&lt;/p&gt;
&lt;p&gt;Overall, even though the actual traveling to/from Brazil was a bit long, RailsSummit was a blast. I really hope to be able to come back next year, and by then, hopefully my Portuguese will have improved.&lt;/p&gt;</content>
		<author>
			<name>Matt Aimonetti (mattetti)</name>
			<uri>http://merbist.com</uri>
		</author>
		<source>
			<title type="html">Merbist</title>
			<subtitle type="html">Rails 3, Ruby &amp;amp; MacRuby news - tutorials - consulting - training</subtitle>
			<link rel="self" href="http://merbist.com/feed/"/>
			<id>http://merbist.com/feed/</id>
			<updated>2010-03-13T12:20:05+00:00</updated>
		</source>
	</entry>

	<entry xml:lang="en">
		<title type="html">MacRuby tips: capturing keyboard events</title>
		<link href="http://merbist.com/2009/10/09/macruby-tips-capturing-keyboard-events/"/>
		<id>http://merbist.com/?p=602</id>
		<updated>2009-10-10T03:24:04+00:00</updated>
		<content type="html">&lt;p&gt;If you are writing any type of games you might want your users to interact with your application using their keyboards.&lt;/p&gt;
&lt;p&gt;This is actually not that hard. The approach is simple and fast forward if you are used to Cocoa.&lt;/p&gt;
&lt;p&gt;Everything starts in Interface Builder, add a custom view instance to your window.&lt;/p&gt;
&lt;p&gt;&lt;img class=&quot;aligncenter&quot; src=&quot;http://img.skitch.com/20091010-8tf834wf9se6y81h2jf7a7e5he.jpg&quot; alt=&quot;&quot; width=&quot;270&quot; height=&quot;554&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Now switch to your project and a new file with a class called KeyboardControlView and make in inherit from NSView. We are creating a subview of NSView so we will be able to make our top view &amp;#8220;layer&amp;#8221; use this subclass.&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;class&lt;/span&gt; KeyboardControlView &lt;span&gt;&amp;amp;&lt;/span&gt;lt; NSView
  attr_accessor &lt;span&gt;:game_controller&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;def&lt;/span&gt; acceptsFirstResponder
    &lt;span&gt;true&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;As you can see in the example above, I added an attribute accessor. attr_accessor class method creates getters and setters. It&amp;#8217;s basically the same as writing:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt; &lt;span&gt;def&lt;/span&gt; game_controller=&lt;span&gt;&amp;#40;&lt;/span&gt;value&lt;span&gt;&amp;#41;&lt;/span&gt;
  &lt;span&gt;@game_controller&lt;/span&gt; = value
&lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
&lt;span&gt;def&lt;/span&gt; game_controller
  &lt;span&gt;@game_controller&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;MacRuby is keeping an eye on these accessors and let bind outlets to them.&lt;br /&gt;
But let&amp;#8217;s not get ahead of ourselves, we&amp;#8217;ll keep that for another time.&lt;/p&gt;
&lt;p&gt;Let&amp;#8217;s go back to our newly created class. Notice, we also added a method called `&lt;br /&gt;
&lt;a href=&quot;http://developer.apple.com/mac/library/documentation/Cocoa/Reference/ApplicationKit/Classes/NSResponder_Class/Reference/Reference.html#//apple_ref/occ/instm/NSResponder/acceptsFirstResponder&quot; target=&quot;_blank&quot;&gt;acceptsFirstResponder&lt;/a&gt;` and returns true. &lt;a href=&quot;http://developer.apple.com/mac/library/documentation/Cocoa/Reference/ApplicationKit/Classes/NSResponder_Class/Reference/Reference.html#//apple_ref/occ/instm/NSResponder/acceptsFirstResponder&quot; target=&quot;_blank&quot;&gt;acceptsFirstResponder&lt;/a&gt; returns false by default.&lt;br /&gt;
But in this case we want it to return true so our new class instance can be first in the responder chain.&lt;/p&gt;
&lt;p&gt;Now that our class is ready, let&amp;#8217;s go back to Interface Builder, select our new custom view and click on the inspector button.&lt;/p&gt;
&lt;p&gt;&lt;img class=&quot;aligncenter&quot; title=&quot;IB&quot; src=&quot;http://img.skitch.com/20091010-1trkxhw2r2paaik3ipa4gtytgg.jpg&quot; alt=&quot;&quot; width=&quot;228&quot; height=&quot;184&quot; /&gt;&lt;br /&gt;
Click on the (i) icon and in the Class field choose our new KeyboardControlView.&lt;br /&gt;
Yep, our new class just shows up by magic, it&amp;#8217;s also called the lrz effect, just don&amp;#8217;t ask &lt;img src=&quot;http://merbist.com/wp-includes/images/smilies/icon_wink.gif&quot; alt=&quot;;)&quot; class=&quot;wp-smiley&quot; /&gt;&lt;br /&gt;
So now when our application starts, a new instance of our NSView class is created and Cocoa will call different methods based on events triggered.&lt;/p&gt;
&lt;p&gt;The two methods we are interested in reimplementing are keyDown and keyUp. They get called when a key gets pressed or released.&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;def&lt;/span&gt; keyDown&lt;span&gt;&amp;#40;&lt;/span&gt;event&lt;span&gt;&amp;#41;&lt;/span&gt;
  characters = event.&lt;span&gt;characters&lt;/span&gt;
  &lt;span&gt;if&lt;/span&gt; characters.&lt;span&gt;length&lt;/span&gt; == &lt;span&gt;1&lt;/span&gt; &lt;span&gt;&amp;amp;&lt;/span&gt;amp;&lt;span&gt;&amp;amp;&lt;/span&gt;amp; !event.&lt;span&gt;isARepeat&lt;/span&gt;
    character = characters.&lt;span&gt;characterAtIndex&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;0&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;
    &lt;span&gt;if&lt;/span&gt; character == NSLeftArrowFunctionKey
      &lt;span&gt;puts&lt;/span&gt; &lt;span&gt;&amp;quot;LEFT pressed&amp;quot;&lt;/span&gt;
    &lt;span&gt;elsif&lt;/span&gt; character == NSRightArrowFunctionKey
      &lt;span&gt;puts&lt;/span&gt; &lt;span&gt;&amp;quot;RIGHT pressed&amp;quot;&lt;/span&gt;
    &lt;span&gt;elsif&lt;/span&gt; character == NSUpArrowFunctionKey
      &lt;span&gt;puts&lt;/span&gt; &lt;span&gt;&amp;quot;UP pressed&amp;quot;&lt;/span&gt;
    &lt;span&gt;elsif&lt;/span&gt; character == NSDownArrowFunctionKey
      &lt;span&gt;puts&lt;/span&gt; &lt;span&gt;&amp;quot;DOWN pressed&amp;quot;&lt;/span&gt;
    &lt;span&gt;end&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
 &lt;span&gt;super&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;I don&amp;#8217;t think the code above needs much explanation. The only things that you might not understand are &amp;#8216;event.isARepeat&amp;#8217;. This method returns true if the user left his/her finger on the key. The other thing is the use of the &amp;#8217;super&amp;#8217; call at the end of the method. Basically, we reopened a method that was already defined and we don&amp;#8217;t want to just overwrite it, we just want to inject out code within the existing method, so once we are done handling the event, we just pass it back to original method.&lt;/p&gt;
&lt;p&gt;Final result:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;class&lt;/span&gt; KeyboardControlView &lt;span&gt;&amp;amp;&lt;/span&gt;lt; NSView
  attr_accessor &lt;span&gt;:game_controller&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;def&lt;/span&gt; acceptsFirstResponder
    &lt;span&gt;true&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;def&lt;/span&gt; keyDown&lt;span&gt;&amp;#40;&lt;/span&gt;event&lt;span&gt;&amp;#41;&lt;/span&gt;
    characters = event.&lt;span&gt;characters&lt;/span&gt;
    &lt;span&gt;if&lt;/span&gt; characters.&lt;span&gt;length&lt;/span&gt; == &lt;span&gt;1&lt;/span&gt; &lt;span&gt;&amp;amp;&lt;/span&gt;amp;&lt;span&gt;&amp;amp;&lt;/span&gt;amp; !event.&lt;span&gt;isARepeat&lt;/span&gt;
      character = characters.&lt;span&gt;characterAtIndex&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;0&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;
      &lt;span&gt;if&lt;/span&gt; character == NSLeftArrowFunctionKey
        &lt;span&gt;puts&lt;/span&gt; &lt;span&gt;&amp;quot;LEFT pressed&amp;quot;&lt;/span&gt;
      &lt;span&gt;elsif&lt;/span&gt; character == NSRightArrowFunctionKey
        &lt;span&gt;puts&lt;/span&gt; &lt;span&gt;&amp;quot;RIGHT pressed&amp;quot;&lt;/span&gt;
      &lt;span&gt;elsif&lt;/span&gt; character == NSUpArrowFunctionKey
        &lt;span&gt;puts&lt;/span&gt; &lt;span&gt;&amp;quot;UP pressed&amp;quot;&lt;/span&gt;
      &lt;span&gt;elsif&lt;/span&gt; character == NSDownArrowFunctionKey
  	&lt;span&gt;puts&lt;/span&gt; &lt;span&gt;&amp;quot;DOWN pressed&amp;quot;&lt;/span&gt;
      &lt;span&gt;end&lt;/span&gt;
    &lt;span&gt;end&lt;/span&gt;
    &lt;span&gt;super&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;# Deals with keyboard keys being released&lt;/span&gt;
  &lt;span&gt;def&lt;/span&gt; keyUp&lt;span&gt;&amp;#40;&lt;/span&gt;event&lt;span&gt;&amp;#41;&lt;/span&gt;
    characters = event.&lt;span&gt;characters&lt;/span&gt;
    &lt;span&gt;if&lt;/span&gt; characters.&lt;span&gt;length&lt;/span&gt; == &lt;span&gt;1&lt;/span&gt;
      character = characters.&lt;span&gt;characterAtIndex&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;0&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;
      &lt;span&gt;if&lt;/span&gt; character == NSLeftArrowFunctionKey
       &lt;span&gt;puts&lt;/span&gt; &lt;span&gt;&amp;quot;LEFT released&amp;quot;&lt;/span&gt;
      &lt;span&gt;elsif&lt;/span&gt; character == NSRightArrowFunctionKey
        &lt;span&gt;puts&lt;/span&gt; &lt;span&gt;&amp;quot;RIGHT released&amp;quot;&lt;/span&gt;
      &lt;span&gt;elsif&lt;/span&gt; character == NSUpArrowFunctionKey
       &lt;span&gt;puts&lt;/span&gt; &lt;span&gt;&amp;quot;UP released&amp;quot;&lt;/span&gt;
      &lt;span&gt;elsif&lt;/span&gt; character == NSDownArrowFunctionKey
        &lt;span&gt;puts&lt;/span&gt; &lt;span&gt;&amp;quot;DOWN released&amp;quot;&lt;/span&gt;
      &lt;span&gt;end&lt;/span&gt;
    &lt;span&gt;end&lt;/span&gt;
    &lt;span&gt;super&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now it&amp;#8217;s up to you to handle the other keystrokes and do whatever you want. That&amp;#8217;s it for this tip, I hope it helps.&lt;/p&gt;</content>
		<author>
			<name>Matt Aimonetti (mattetti)</name>
			<uri>http://merbist.com</uri>
		</author>
		<source>
			<title type="html">Merbist</title>
			<subtitle type="html">Rails 3, Ruby &amp;amp; MacRuby news - tutorials - consulting - training</subtitle>
			<link rel="self" href="http://merbist.com/feed/"/>
			<id>http://merbist.com/feed/</id>
			<updated>2010-03-13T12:20:05+00:00</updated>
		</source>
	</entry>

	<entry xml:lang="en">
		<title type="html">MacRuby 0.5 beta 1 and Textorize</title>
		<link href="http://merbist.com/2009/10/07/macruby-0-5-beta-1-and-textorize/"/>
		<id>http://merbist.com/?p=592</id>
		<updated>2009-10-08T04:45:02+00:00</updated>
		<content type="html">&lt;p&gt;Good news everyone!&lt;/p&gt;
&lt;p&gt;MacRuby beta 1 has been released! &lt;a title=&quot;MacRuby&quot; href=&quot;http://www.macruby.org/blog/2009/10/07/macruby05b1.html&quot; target=&quot;_blank&quot;&gt;Official announcement here&lt;/a&gt;.&lt;/p&gt;
&lt;div class=&quot;wp-caption aligncenter&quot;&gt;&lt;a href=&quot;http://www.macruby.org/files/MacRuby%200.5%20beta%201.zip&quot;&gt;&lt;img title=&quot;Download MacRuby 0.5 beta1&quot; src=&quot;http://www.macruby.org/images/zip.png&quot; alt=&quot;Download MacRuby 0.5 beta1&quot; width=&quot;144&quot; height=&quot;142&quot; /&gt;&lt;/a&gt;&lt;p class=&quot;wp-caption-text&quot;&gt;Download MacRuby 0.5 beta1&lt;/p&gt;&lt;/div&gt;
&lt;p&gt;Note that the download is only for SnowLeopard, intel machines.&lt;/p&gt;
&lt;p&gt;Lots of great stuff in this new release, the first one based on LLVM. Check the &lt;a href=&quot;http://www.macruby.org/blog/2009/10/07/macruby05b1.html&quot; target=&quot;_blank&quot;&gt;Laurent&amp;#8217;s post&lt;/a&gt; to learn more about the work done on compilation, optimization, concurrency, compatibility and Cocoa interaction. And a big thank you to &lt;a title=&quot;Laurent Sansonetti&quot; href=&quot;http://www.youtube.com/watch?v=BVgM7qeAlko&quot; target=&quot;_blank&quot;&gt;Laurent Sansonetti&lt;/a&gt; who is putting so much effort in this project!&lt;/p&gt;
&lt;p&gt;However, don&amp;#8217;t forget it&amp;#8217;s still a beta release and you might encounter bugs. Feel free to report them in the bug tracker or ask on the mailing list.&lt;/p&gt;
&lt;p&gt;On a different topic, the other day, John Gruber from &lt;a href=&quot;http://daringfireball.net/&quot; target=&quot;_blank&quot;&gt;Daring Fireball&lt;/a&gt; &lt;a href=&quot;http://daringfireball.net/linked/2009/09/30/textorize&quot; target=&quot;_blank&quot;&gt;wrote a quick note&lt;/a&gt; about &lt;a href=&quot;http://mir.aculo.us/2009/09/29/textorize-pristine-font-rendering-for-the-web/&quot; target=&quot;_blank&quot;&gt;Thomas Fuchs&amp;#8217; textorize script&lt;/a&gt; which since got its own place on the internet &lt;a href=&quot;http://textorize.org/&quot; target=&quot;_blank&quot;&gt;http://textorize.org/&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Textorize is a Ruby-based font rasterizer command line utility for Mac OS X. It generates PNG files from an input string and options, using Mac OS X&amp;#8217;s pristine typography facilities. As John said, it&amp;#8217;s a case where a few lines of Ruby code beat Photoshop.&lt;/p&gt;
&lt;p&gt;Thomas version is based on RubyCocoa which is great&amp;#8230; but not MacRuby.&lt;/p&gt;
&lt;p&gt;To celebrate&lt;a title=&quot;MacRuby Beta 1&quot; href=&quot;http://www.macruby.org/blog/2009/10/07/macruby05b1.html&quot; target=&quot;_blank&quot;&gt; MacRuby 0.5 beta1&lt;/a&gt;, I ported the gem over and pushed it to the excellent &lt;a href=&quot;http://gemcutter.org/&quot; target=&quot;_blank&quot;&gt;gemcutter.org facility&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;After installing MacRuby beta, follow these directives:&lt;/p&gt;
&lt;pre&gt;$ macgem sources -a http://gemcutter.org
$ sudo macgem install textorize-mr
$ textorize -f&quot;Didot&quot; -s200 &quot;MacRuby 0.5b1&quot;
$ open output.png&lt;/pre&gt;
&lt;p&gt;And you will get a &lt;a href=&quot;http://en.wikipedia.org/wiki/Subpixel_antialiasing&quot;&gt;subpixel antialiased&lt;/a&gt; fancy bitmap like that:&lt;/p&gt;
&lt;p&gt;&lt;img class=&quot;aligncenter size-full wp-image-595&quot; title=&quot;macruby05b1&quot; src=&quot;http://merbist.com/wp-content/uploads/2009/10/macruby05b1.png&quot; alt=&quot;macruby05b1&quot; width=&quot;564&quot; height=&quot;102&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Check &lt;a href=&quot;http://textorize.org/&quot; target=&quot;_blank&quot;&gt;http://textorize.org/&lt;/a&gt; for more examples and &lt;a href=&quot;http://github.com/mattetti/textorize&quot; target=&quot;_blank&quot;&gt;http://github.com/mattetti/textorize&lt;/a&gt; for the source code.&lt;/p&gt;</content>
		<author>
			<name>Matt Aimonetti (mattetti)</name>
			<uri>http://merbist.com</uri>
		</author>
		<source>
			<title type="html">Merbist</title>
			<subtitle type="html">Rails 3, Ruby &amp;amp; MacRuby news - tutorials - consulting - training</subtitle>
			<link rel="self" href="http://merbist.com/feed/"/>
			<id>http://merbist.com/feed/</id>
			<updated>2010-03-13T12:20:05+00:00</updated>
		</source>
	</entry>

	<entry xml:lang="en">
		<title type="html">MacRuby tips: how to play an audio file</title>
		<link href="http://merbist.com/2009/10/06/macruby-tips-how-to-play-an-audio-file/"/>
		<id>http://merbist.com/?p=570</id>
		<updated>2009-10-07T03:00:08+00:00</updated>
		<content type="html">&lt;p&gt;Let&amp;#8217;s say you would like to play an audio file in your MacRuby app/script, how would you do?&lt;br /&gt;
It&amp;#8217;s actually pretty simple, you just need to use NSSound. If we wanted to use a system sound we could do:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;NSSound.&lt;span&gt;soundNamed&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;'Basso'&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;.&lt;span&gt;play&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;But let&amp;#8217;s look at a more advanced example with some Cocoa patterns. We will loop through all the audio files in a folder and we will play them one after the other.&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;#!/usr/bin/env macruby&lt;/span&gt;
framework &lt;span&gt;'Cocoa'&lt;/span&gt;
&lt;span&gt;# Cocoa documentation reference:&lt;/span&gt;
&lt;span&gt;# http://developer.apple.com/mac/library/documentation/Cocoa/Reference/ApplicationKit/Classes/NSSound_Class/Reference/Reference.html&lt;/span&gt;
&amp;nbsp;
&lt;span&gt;def&lt;/span&gt; play_sound
  &lt;span&gt;if&lt;/span&gt; &lt;span&gt;@sounds&lt;/span&gt;.&lt;span&gt;empty&lt;/span&gt;?
    NSApplication.&lt;span&gt;sharedApplication&lt;/span&gt;.&lt;span&gt;terminate&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;nil&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;
  &lt;span&gt;else&lt;/span&gt;
    sound_file = &lt;span&gt;@sounds&lt;/span&gt;.&lt;span&gt;shift&lt;/span&gt;
    s = NSSound.&lt;span&gt;alloc&lt;/span&gt;.&lt;span&gt;initWithContentsOfFile&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;sound_file, byReference: &lt;span&gt;false&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;
    &lt;span&gt;puts&lt;/span&gt; &lt;span&gt;&amp;quot;previewing #{sound_file}&amp;quot;&lt;/span&gt;
    s.&lt;span&gt;delegate&lt;/span&gt; = &lt;span&gt;self&lt;/span&gt;
    s.&lt;span&gt;play&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
&lt;span&gt;# This is a delegate method called by the sound object&lt;/span&gt;
&lt;span&gt;def&lt;/span&gt; sound&lt;span&gt;&amp;#40;&lt;/span&gt;sound, didFinishPlaying: state&lt;span&gt;&amp;#41;&lt;/span&gt;
  play_sound &lt;span&gt;if&lt;/span&gt; state
&lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
&lt;span&gt;# Delegate method called when the app finished loading&lt;/span&gt;
&lt;span&gt;def&lt;/span&gt; applicationDidFinishLaunching&lt;span&gt;&amp;#40;&lt;/span&gt;notification&lt;span&gt;&amp;#41;&lt;/span&gt;
  &lt;span&gt;@sounds&lt;/span&gt; = &lt;span&gt;Dir&lt;/span&gt;.&lt;span&gt;glob&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;&amp;quot;/System/Library/Sounds/*.aiff&amp;quot;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;
  play_sound
&lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
&lt;span&gt;# We are delegating the application to self so the script will know when&lt;/span&gt;
&lt;span&gt;# it finished loading&lt;/span&gt;
NSApplication.&lt;span&gt;sharedApplication&lt;/span&gt;.&lt;span&gt;delegate&lt;/span&gt; = &lt;span&gt;self&lt;/span&gt;
NSApplication.&lt;span&gt;sharedApplication&lt;/span&gt;.&lt;span&gt;run&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;On my machine, I get the following output:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;shell&quot;&gt;$ macruby macrubysound.rb
previewing /System/Library/Sounds/Basso.aiff
previewing /System/Library/Sounds/Blow.aiff
previewing /System/Library/Sounds/Bottle.aiff
previewing /System/Library/Sounds/Frog.aiff
previewing /System/Library/Sounds/Funk.aiff
previewing /System/Library/Sounds/Glass.aiff
previewing /System/Library/Sounds/Hero.aiff
previewing /System/Library/Sounds/Morse.aiff
previewing /System/Library/Sounds/Ping.aiff
previewing /System/Library/Sounds/Pop.aiff
previewing /System/Library/Sounds/Purr.aiff
previewing /System/Library/Sounds/Sosumi.aiff
previewing /System/Library/Sounds/Submarine.aiff
previewing /System/Library/Sounds/Tink.aiff&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Cocoa delegation might seem a bit strange at first when you come from Ruby. In Ruby we rarely do any type of async delegation so let&amp;#8217;s quickly look at what&amp;#8217;s going on in this script.&lt;/p&gt;
&lt;p&gt;The first thing we do is loading the cocoa framework which makes total sense, then we define a bunch of methods and finally we get to:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;NSApplication.&lt;span&gt;sharedApplication&lt;/span&gt;.&lt;span&gt;delegate&lt;/span&gt; = &lt;span&gt;self&lt;/span&gt;
NSApplication.&lt;span&gt;sharedApplication&lt;/span&gt;.&lt;span&gt;run&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We are setting the run loop to delegate to set, which means that when an event is triggered, the delegation method will be called on self (our script).&lt;br /&gt;
Once that done, we are starting out run loop.&lt;/p&gt;
&lt;p&gt;Once the application is loaded the applicationDidFinishLaunching delegate is being triggered (line 24). At this point, we are looking for all the sound files in the sound system folder and storing them in an instance variable (line 25). Finally, we are calling the play_sound method (line 26).&lt;/p&gt;
&lt;p&gt;The play_sound method checks that we have some audio files left to play (line 7), otherwise it quits the app. (line 8 ) If we still have some files in the queue, we get the first one (line 10) and use it to create an instance of NSSound (line 11).&lt;/p&gt;
&lt;p&gt;Before playing the NSSound instance, we are setting its delegate to our script (self) (line 13). If you read &lt;a href=&quot;http://developer.apple.com/mac/library/documentation/Cocoa/Reference/ApplicationKit/Classes/NSSound_Class/Reference/Reference.html&quot; target=&quot;_blank&quot;&gt;NSSound Cocoa documentation&lt;/a&gt; for #play, you will notice that #play &amp;#8220;initiates playback asynchronously and returns control to your application. Therefore, your application can continue doing work while the audio is playing.&amp;#8221; In our case, we don&amp;#8217;t want to do that, otherwise all the sounds will play at the same time.&lt;/p&gt;
&lt;p&gt;The documentation also mentions a delegation you can use to avoid that. This is exactly what we did when we implemented:  def sound(sound, didFinishPlaying: state) (line 19)&lt;/p&gt;
&lt;p&gt;Rubyists might be surprised by the method signature. MacRuby extended Ruby to support Objective-C selectors.&lt;/p&gt;
&lt;p&gt;When the sound is playing, this delegate gets called, we check on the state of the sound, if it finished playing then we call play_sound again.&lt;/p&gt;
&lt;p&gt;That&amp;#8217;s it!  It&amp;#8217;s a very elegant implementation giving us the benefits of both Ruby and Cocoa.&lt;/p&gt;</content>
		<author>
			<name>Matt Aimonetti (mattetti)</name>
			<uri>http://merbist.com</uri>
		</author>
		<source>
			<title type="html">Merbist</title>
			<subtitle type="html">Rails 3, Ruby &amp;amp; MacRuby news - tutorials - consulting - training</subtitle>
			<link rel="self" href="http://merbist.com/feed/"/>
			<id>http://merbist.com/feed/</id>
			<updated>2010-03-13T12:20:05+00:00</updated>
		</source>
	</entry>

	<entry xml:lang="en">
		<title type="html">MacRuby tips: embed a custom font</title>
		<link href="http://merbist.com/2009/10/06/macruby-tips-embed-a-custom-font/"/>
		<id>http://merbist.com/?p=561</id>
		<updated>2009-10-06T17:23:44+00:00</updated>
		<content type="html">&lt;p&gt;Let say you want to release your MacRuby app and use a custom embedded font?&lt;br /&gt;
You probably don&amp;#8217;t want to force your users to install the font.&lt;br /&gt;
Well, don&amp;#8217;t worry, just put the font file in your resources folder and use the following code:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;font_location = NSBundle.&lt;span&gt;mainBundle&lt;/span&gt;.&lt;span&gt;pathForResource&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;'MyCustomFont'&lt;/span&gt;, ofType: &lt;span&gt;'ttf'&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;
font_url = NSURL.&lt;span&gt;fileURLWithPath&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;font_location&lt;span&gt;&amp;#41;&lt;/span&gt;
&lt;span&gt;# in MacRuby, always make sure that cocoa constants start by an uppercase&lt;/span&gt;
CTFontManagerRegisterFontsForURL&lt;span&gt;&amp;#40;&lt;/span&gt;font_url, KCTFontManagerScopeProcess, &lt;span&gt;nil&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;That&amp;#8217;s it, now your custom font is available and you can change your textfield instance&amp;#8217;s font like that:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;text_field.&lt;span&gt;font&lt;/span&gt; = NSFont.&lt;span&gt;fontWithName&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;'MyCustomFont'&lt;/span&gt;, size:&lt;span&gt;24&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The only tricky things here were to know the Cocoa API call and to know that even if the Cocoa API references the constant to use as kCTFontManagerScopeProcess, because in Ruby, constants start by an uppercase, you need to convert it to: KCTFontManagerScopeProcess.&lt;/p&gt;</content>
		<author>
			<name>Matt Aimonetti (mattetti)</name>
			<uri>http://merbist.com</uri>
		</author>
		<source>
			<title type="html">Merbist</title>
			<subtitle type="html">Rails 3, Ruby &amp;amp; MacRuby news - tutorials - consulting - training</subtitle>
			<link rel="self" href="http://merbist.com/feed/"/>
			<id>http://merbist.com/feed/</id>
			<updated>2010-03-13T12:20:05+00:00</updated>
		</source>
	</entry>

	<entry xml:lang="en">
		<title type="html">MacRuby soon to reach a new milestone</title>
		<link href="http://merbist.com/2009/10/05/macruby-soon-to-reach-a-new-milestone/"/>
		<id>http://merbist.com/?p=556</id>
		<updated>2009-10-06T07:58:53+00:00</updated>
		<content type="html">&lt;p&gt;Laurent just posted a &lt;a title=&quot;MacRuby Status Update&quot; href=&quot;http://bit.ly/2YPwRT&quot; target=&quot;_blank&quot;&gt;MacRuby status update&lt;/a&gt; on the mailing list and the first official beta of MacRuby 0.5 should be released pretty soon.&lt;/p&gt;
&lt;p&gt;Let&amp;#8217;s quickly look at Laurent&amp;#8217;s report:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Early backtracing support.&lt;/li&gt;
&lt;li&gt;Much better AOT compilation. Parts of the standard library are now pre-compiled for testing.&lt;/li&gt;
&lt;li&gt;Migrated to LLVM top of tree.&lt;/li&gt;
&lt;li&gt;Dispatcher performance is now back to normal (we lost about 30% due to gcc not inlining code).&lt;/li&gt;
&lt;li&gt;Many bug fixes.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In lay terms, backtracing is what you see when your app crashes or has a problem, it&amp;#8217;s the list of methods called before the exception was raised and the line where the error happened. Currently the backtrace is similar to what you would have with Ruby 1.9, however objective-c exceptions are not supported and there is still some work to do.&lt;/p&gt;
&lt;p&gt;AOT compilation or Ahead Of Time compilation is the process of compiling a script into machine code. I already covered that &lt;a href=&quot;http://merbist.com/2009/07/12/compiled-hello-world-with-macruby/&quot; target=&quot;_blank&quot;&gt;feature earlier&lt;/a&gt;. Progress has been made and now some Ruby standard libraries are now pre-compiled in MacRuby. The two main advantages of doing AOT compilation are startup speed and obfuscation. Two important features for desktop applications or for when you want to license your server app.&lt;/p&gt;
&lt;p&gt;Updating LLVM doesn&amp;#8217;t mean much for end users. With the support of the MacRuby team, &lt;a href=&quot;http://www.icoretech.org/&quot; target=&quot;_blank&quot;&gt;Claudio&lt;/a&gt; setup a nightly build bot &lt;a title=&quot;MacRuby Nightly&quot; href=&quot;http://macruby.icoretech.org/&quot; target=&quot;_blank&quot;&gt;http://macruby.icoretech.org/&lt;/a&gt; allowing you to download nightly builds for SnowLeopard. The app is a &lt;a href=&quot;http://sinatrarb.com&quot; target=&quot;_blank&quot;&gt;Sinatra app&lt;/a&gt; that could can checkout on github:  &lt;a href=&quot;http://github.com/masterkain/macruby-nightlies-web&quot; target=&quot;_blank&quot;&gt;http://github.com/masterkain/macruby-nightlies-web&lt;/a&gt; What&amp;#8217;s interesting is that Apple is sponsoring both LLVM and MacRuby which will hopefully bring some synergy and help both projects.&lt;/p&gt;
&lt;p&gt;Fixed dispatcher, this is a just a perf bug fix only affecting people on trunk.&lt;/p&gt;
&lt;p&gt;I personally used MacRuby quite a lot recently. I have been writing a 2D video game for my &lt;a href=&quot;http://rubyconf.org/talks/153-writing-2-d-games-for-the-osx-platform-in-ruby&quot; target=&quot;_blank&quot;&gt;RubyConf talk&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;img class=&quot;aligncenter&quot; title=&quot;MacRuby Demo Game&quot; src=&quot;http://img.skitch.com/20091006-kj64ix4up5q8dh4yin38hjrjcp.jpg&quot; alt=&quot;&quot; width=&quot;680&quot; height=&quot;462&quot; /&gt;It&amp;#8217;s a simple game only using Ruby and Cocoa. It&amp;#8217;s not a fancy game and, no, it doesn&amp;#8217;t run on the iPhone yet &lt;img src=&quot;http://merbist.com/wp-includes/images/smilies/icon_wink.gif&quot; alt=&quot;;)&quot; class=&quot;wp-smiley&quot; /&gt; &lt;/p&gt;
&lt;p&gt;I never wrote a game in Cocoa and I decided not to use an existing framework like Gosu or cocos2d, instead I decided to write everything from scratch. Using CoreAnimation instead of OpenGL, the task wasn&amp;#8217;t that hard at all. in less than 1,000 LOC (before refactoring), I have a fully working game.&lt;/p&gt;
&lt;p&gt;I will be previewing the game at &lt;a href=&quot;http://www.railssummit.com.br/&quot; target=&quot;_blank&quot;&gt;RailsSummit&lt;/a&gt; and will show the code and explain how to get there at &lt;a href=&quot;http://rubyconf.org/talks/153-writing-2-d-games-for-the-osx-platform-in-ruby&quot; target=&quot;_blank&quot;&gt;RubyConf&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;When I started getting involved with MacRuby, I really did not think I would write a video game in Ruby and actually enjoy it. At the end of the day, I will more than likely use MacRuby for desktop/mobile/server apps more than games, but it&amp;#8217;s awesome to be able to use your favorite language to do other things than what you are usually paid to do.&lt;/p&gt;
&lt;p&gt;MacRuby is coming along very nicely, and I&amp;#8217;m really excited about the multitude of new options offered to Ruby developers on OSX and can&amp;#8217;t wait for 0.5 final and see what people will do with it.&lt;/p&gt;</content>
		<author>
			<name>Matt Aimonetti (mattetti)</name>
			<uri>http://merbist.com</uri>
		</author>
		<source>
			<title type="html">Merbist</title>
			<subtitle type="html">Rails 3, Ruby &amp;amp; MacRuby news - tutorials - consulting - training</subtitle>
			<link rel="self" href="http://merbist.com/feed/"/>
			<id>http://merbist.com/feed/</id>
			<updated>2010-03-13T12:20:05+00:00</updated>
		</source>
	</entry>

	<entry xml:lang="en">
		<title type="html">Inherited Templates With Rails</title>
		<link href="http://yehudakatz.com/2009/10/04/inherited-templates-with-rails/"/>
		<id>http://yehudakatz.com/?p=352</id>
		<updated>2009-10-05T03:14:36+00:00</updated>
		<content type="html">&lt;p&gt;A nice feature of some template languages (for instance, Django&amp;#8217;s) is the ability to create templates that can be &amp;#8220;inherited&amp;#8221; by other templates. In effect, the goal is to create a template that has some missing content, and let &amp;#8220;inheritors&amp;#8221; fill in that content downstream.&lt;/p&gt;
&lt;p&gt;It&amp;#8217;s not terribly difficult to add this feature to Rails; let&amp;#8217;s first design the API. First, the parent template (&lt;code&gt;_parent.html.erb&lt;/code&gt;):&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;rhtml&quot;&gt;Before in parent
&amp;lt;%= first_from_child %&amp;gt;
&amp;lt;%= second_from_child %&amp;gt;
After in parent&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We just use normal partial locals for the parent API, because they&amp;#8217;re a well-understood construct and pretty easy to work with. Next, the &amp;#8220;subclass&amp;#8221; (&lt;code&gt;_child.html.erb&lt;/code&gt;):&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;rhtml&quot;&gt;Before in child
&amp;lt;% override_template(&amp;quot;parent&amp;quot;) do |template| -%&amp;gt;
  &amp;lt;% template.set_content(:first_from_child) do -%&amp;gt;first_from_child&amp;lt;% end -%&amp;gt;
  &amp;lt;% template.set_content(:second_from_child) do -%&amp;gt;second_from_child&amp;lt;% end -%&amp;gt;
&amp;lt;% end -%&amp;gt;
After in child&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Here, we use Ruby&amp;#8217;s block syntax to create a context for supplying the content for the template. We expect the following output:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;html&quot;&gt;Before in child
Before in parent
first_from_child
second_from_child
After in parent
After in child&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;There are two parts to this implementation. First, let&amp;#8217;s create the implementation for the &lt;code&gt;override_template&lt;/code&gt; method that will be exposed onto &lt;code&gt;ActionView::Base&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;module&lt;/span&gt; ActionView
  &lt;span&gt;module&lt;/span&gt; InheritedTemplates
    &lt;span&gt;class&lt;/span&gt; Collector
    &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
    &lt;span&gt;def&lt;/span&gt; override_template&lt;span&gt;&amp;#40;&lt;/span&gt;name, &lt;span&gt;&amp;amp;&lt;/span&gt;block&lt;span&gt;&amp;#41;&lt;/span&gt;
      locals = Collector.&lt;span&gt;collect&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;self&lt;/span&gt;, &lt;span&gt;&amp;amp;&lt;/span&gt;block&lt;span&gt;&amp;#41;&lt;/span&gt;
      concat&lt;span&gt;&amp;#40;&lt;/span&gt;render &lt;span&gt;:partial&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; name, &lt;span&gt;:locals&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; locals&lt;span&gt;&amp;#41;&lt;/span&gt;
    &lt;span&gt;end&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;class&lt;/span&gt; Base
    &lt;span&gt;include&lt;/span&gt; InheritedTemplates
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;To start, we create the method, deferring the heavy lifting to the &lt;code&gt;Collector.collect&lt;/code&gt; method. The API we&amp;#8217;ve designed asks &lt;code&gt;#collect&lt;/code&gt; to hand back a Hash of locals, which we can pass unmodified into the &amp;#8220;super&amp;#8221; template.&lt;/p&gt;
&lt;p&gt;The collector is a pretty straight-forward implementation, but it deserves some explanation:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;class&lt;/span&gt; Collector
  &lt;span&gt;def&lt;/span&gt; &lt;span&gt;self&lt;/span&gt;.&lt;span&gt;collect&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;view&lt;span&gt;&amp;#41;&lt;/span&gt;
    collector = new&lt;span&gt;&amp;#40;&lt;/span&gt;view&lt;span&gt;&amp;#41;&lt;/span&gt;
    &lt;span&gt;yield&lt;/span&gt; collector
    collector.&lt;span&gt;content&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
  attr_reader &lt;span&gt;:content&lt;/span&gt;
  &lt;span&gt;def&lt;/span&gt; initialize&lt;span&gt;&amp;#40;&lt;/span&gt;view&lt;span&gt;&amp;#41;&lt;/span&gt;
    &lt;span&gt;@view&lt;/span&gt; = view
    &lt;span&gt;@content&lt;/span&gt; = &lt;span&gt;&amp;#123;&lt;/span&gt;&lt;span&gt;&amp;#125;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;def&lt;/span&gt; set_content&lt;span&gt;&amp;#40;&lt;/span&gt;name, &lt;span&gt;&amp;amp;&lt;/span&gt;block&lt;span&gt;&amp;#41;&lt;/span&gt;
    &lt;span&gt;@content&lt;/span&gt;&lt;span&gt;&amp;#91;&lt;/span&gt;name&lt;span&gt;&amp;#93;&lt;/span&gt; = &lt;span&gt;@view&lt;/span&gt;.&lt;span&gt;capture&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;block&lt;span&gt;&amp;#41;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
&lt;span&gt;def&lt;/span&gt; override_template&lt;span&gt;&amp;#40;&lt;/span&gt;name, &lt;span&gt;&amp;amp;&lt;/span&gt;block&lt;span&gt;&amp;#41;&lt;/span&gt;
  locals = Collector.&lt;span&gt;collect&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;self&lt;/span&gt;, &lt;span&gt;&amp;amp;&lt;/span&gt;block&lt;span&gt;&amp;#41;&lt;/span&gt;
  concat&lt;span&gt;&amp;#40;&lt;/span&gt;render &lt;span&gt;:partial&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; name, &lt;span&gt;:locals&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; locals&lt;span&gt;&amp;#41;&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The first thing we do is define the &lt;code&gt;collect&lt;/code&gt; method. It&amp;#8217;s a basic DSL implementation, creating an instance of itself and evaluating the block in its context. In order to fully understand what&amp;#8217;s going on here, you need to see what the block looks like, once it&amp;#8217;s compiled from ERB into pure-Ruby:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;override_template&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;&amp;quot;parent&amp;quot;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt; &lt;span&gt;do&lt;/span&gt; &lt;span&gt;|&lt;/span&gt;template&lt;span&gt;|&lt;/span&gt;
  template.&lt;span&gt;set_content&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;:first_from_child&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt; &lt;span&gt;do&lt;/span&gt;; &lt;span&gt;@output_buffer&lt;/span&gt; &lt;span&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span&gt;&amp;quot;first_from_child&amp;quot;&lt;/span&gt;; &lt;span&gt;end&lt;/span&gt;
  template.&lt;span&gt;set_content&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;:second_from_child&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt; &lt;span&gt;do&lt;/span&gt;; &lt;span&gt;@output_buffer&lt;/span&gt; &lt;span&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span&gt;&amp;quot;second_from_child&amp;quot;&lt;/span&gt;; &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We start by creating a new collector, yielding the collector to the block passed to &lt;code&gt;override_template&lt;/code&gt;. Inside the block, &lt;code&gt;template.set_content&lt;/code&gt; is called twice; each call sets a key and value in the &lt;code&gt;@content&lt;/code&gt; Hash. When done, the Hash will look like &lt;code&gt;{:first_from_child =&amp;gt; &quot;first_from_child&quot;, :second_from_child =&amp;gt; &quot;second_from_child&quot;}&lt;/code&gt;. The collector then returns the content Hash.&lt;/p&gt;
&lt;p&gt;When that&amp;#8217;s done, &lt;code&gt;override_template&lt;/code&gt; simply passes that locals Hash through to &lt;code&gt;render :partial&lt;/code&gt;, and concats the output to the buffer.&lt;/p&gt;
&lt;h2&gt;Afterthought&lt;/h2&gt;
&lt;p&gt;Those following my blog might remember that I showed how we can improve block helpers by smarter compilation. If that fix was in place, here&amp;#8217;s what &lt;code&gt;_child.html.erb&lt;/code&gt; would look like:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;rhtml&quot;&gt;Before in child
&amp;lt;%= override_template(&amp;quot;parent&amp;quot;) do |template| -%&amp;gt;
  &amp;lt;% template.set_content(:first_from_child) do -%&amp;gt;first_from_child&amp;lt;% end -%&amp;gt;
  &amp;lt;% template.set_content(:second_from_child) do -%&amp;gt;second_from_child&amp;lt;% end -%&amp;gt;
&amp;lt;% end -%&amp;gt;
After in child&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;More importantly, here&amp;#8217;s what &lt;code&gt;override_template&lt;/code&gt; would look like:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;def&lt;/span&gt; override_template&lt;span&gt;&amp;#40;&lt;/span&gt;name, &lt;span&gt;&amp;amp;&lt;/span&gt;block&lt;span&gt;&amp;#41;&lt;/span&gt;
  locals = Collector.&lt;span&gt;collect&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;self&lt;/span&gt;, &lt;span&gt;&amp;amp;&lt;/span&gt;block&lt;span&gt;&amp;#41;&lt;/span&gt;
  render &lt;span&gt;:partial&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; name, &lt;span&gt;:locals&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; locals
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We&amp;#8217;re actually saved a bit of pain here in the unfixed case, because override_template can only be called from inside templates, so we don&amp;#8217;t need to use block_called_from_erb? to disambiguate. Had this been a normal block helper, override_template would have looked like:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;def&lt;/span&gt; override_template&lt;span&gt;&amp;#40;&lt;/span&gt;name, &lt;span&gt;&amp;amp;&lt;/span&gt;block&lt;span&gt;&amp;#41;&lt;/span&gt;
  locals = Collector.&lt;span&gt;collect&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;self&lt;/span&gt;, &lt;span&gt;&amp;amp;&lt;/span&gt;block&lt;span&gt;&amp;#41;&lt;/span&gt;
  response = render&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;:partial&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; name, &lt;span&gt;:locals&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; locals&lt;span&gt;&amp;#41;&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;if&lt;/span&gt; block_called_from_erb?&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;block&lt;span&gt;&amp;#41;&lt;/span&gt;
    concat&lt;span&gt;&amp;#40;&lt;/span&gt;response&lt;span&gt;&amp;#41;&lt;/span&gt;
  &lt;span&gt;else&lt;/span&gt;
    response
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Of course, realizing all of this is non-trivial, and the reason we&amp;#8217;re going to be moving toward the simple block helper syntax above.&lt;/p&gt;</content>
		<author>
			<name>wycats</name>
			<uri>http://yehudakatz.com</uri>
		</author>
		<source>
			<title type="html">Katz Got Your Tongue?</title>
			<subtitle type="html">Random Geek-Related Thoughts</subtitle>
			<link rel="self" href="http://yehudakatz.com/feed/atom/"/>
			<id>http://yehudakatz.com/feed/atom/</id>
			<updated>2010-03-13T08:20:07+00:00</updated>
		</source>
	</entry>

	<entry xml:lang="en">
		<title type="html">Emulating Smalltalk&amp;#8217;s Conditionals in Ruby</title>
		<link href="http://yehudakatz.com/2009/10/04/emulating-smalltalks-conditionals-in-ruby/"/>
		<id>http://yehudakatz.com/?p=350</id>
		<updated>2009-10-04T08:27:07+00:00</updated>
		<content type="html">&lt;p&gt;If you follow my blog, you know that I enjoy emulating language features from other languages (like Python) in pure-Ruby. On the flight back from London, I read through Smalltalk Best Practice Patterns, and was reminded that Smalltalk doesn&amp;#8217;t have built-in conditionals.&lt;/p&gt;
&lt;p&gt;Instead, they use method calls (aka message sends) to do the heavy lifting:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;smalltalk&quot;&gt;anObject isNil
  ifTrue:  &lt;span&gt;&amp;#91;&lt;/span&gt; &lt;span&gt;Transcript&lt;/span&gt; show: &lt;span&gt;'true'&lt;/span&gt;. &lt;span&gt;&amp;#93;&lt;/span&gt;
  ifFalse: &lt;span&gt;&amp;#91;&lt;/span&gt; &lt;span&gt;Transcript&lt;/span&gt; show: &lt;span&gt;'false'&lt;/span&gt;. &lt;span&gt;&amp;#93;&lt;/span&gt;.&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In contrast, the typical way to do that in Ruby is:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;if&lt;/span&gt; an_object.&lt;span&gt;nil&lt;/span&gt;?
  &lt;span&gt;puts&lt;/span&gt; &lt;span&gt;&amp;quot;true&amp;quot;&lt;/span&gt;
&lt;span&gt;else&lt;/span&gt;
  &lt;span&gt;puts&lt;/span&gt; &lt;span&gt;&amp;quot;false&amp;quot;&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;However, the Smalltalk approach is fully compatible with Ruby&amp;#8217;s pure-OO approach (which is, in fact, inherited from Smalltalk in the first place). So why not be able to do:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;an_object.&lt;span&gt;nil&lt;/span&gt;?&lt;span&gt;&amp;#41;&lt;/span&gt;.
  &lt;span&gt;if_true&lt;/span&gt;  &lt;span&gt;&amp;#123;&lt;/span&gt; &lt;span&gt;puts&lt;/span&gt; &lt;span&gt;&amp;quot;true&amp;quot;&lt;/span&gt; &lt;span&gt;&amp;#125;&lt;/span&gt;.
  &lt;span&gt;if_false&lt;/span&gt; &lt;span&gt;&amp;#123;&lt;/span&gt; &lt;span&gt;puts&lt;/span&gt; &lt;span&gt;&amp;quot;false&amp;quot;&lt;/span&gt; &lt;span&gt;&amp;#125;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In Ruby, unlike Smalltalk, message sends use a &amp;#8220;&lt;code&gt;.&lt;/code&gt;&amp;#8220;, so we need to use periods between the message sends to link them together. With that exception, the semantics of the two languages are roughly identical. And, as usual, the implementation in Ruby is pretty straight-forward:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;class&lt;/span&gt; &lt;span&gt;Object&lt;/span&gt;
  &lt;span&gt;def&lt;/span&gt; if_true
    &lt;span&gt;yield&lt;/span&gt;
    &lt;span&gt;self&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;def&lt;/span&gt; if_false
    &lt;span&gt;self&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
&lt;span&gt;class&lt;/span&gt; &lt;span&gt;NilClass&lt;/span&gt;
  &lt;span&gt;def&lt;/span&gt; if_true
    &lt;span&gt;self&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;def&lt;/span&gt; if_false
    &lt;span&gt;yield&lt;/span&gt;
    &lt;span&gt;self&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
&lt;span&gt;class&lt;/span&gt; &lt;span&gt;FalseClass&lt;/span&gt;
  &lt;span&gt;def&lt;/span&gt; if_true
    &lt;span&gt;self&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;def&lt;/span&gt; if_false
    &lt;span&gt;yield&lt;/span&gt;
    &lt;span&gt;self&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In Ruby, &lt;code&gt;false&lt;/code&gt; and &lt;code&gt;nil&lt;/code&gt; are &amp;#8220;falsy&amp;#8221;, while all other values are &amp;#8220;truthy&amp;#8221;. Since everything in Ruby inherits from Object, we can simply define &lt;code&gt;if_true&lt;/code&gt; and &lt;code&gt;if_false&lt;/code&gt; on &lt;code&gt;Object&lt;/code&gt;, &lt;code&gt;FalseClass&lt;/code&gt;, and &lt;code&gt;NilClass&lt;/code&gt;. If the block should be called (&lt;code&gt;if_true&lt;/code&gt; on truthy values, or &lt;code&gt;if_false&lt;/code&gt; on falsy values), we yield. In all cases, we return &amp;#8220;self&amp;#8221; so that we can chain &lt;code&gt;if_true&lt;/code&gt; to &lt;code&gt;if_false&lt;/code&gt;. And that&amp;#8217;s it.&lt;/p&gt;
&lt;p&gt;Of course, this means extending core classes, which some people are shy about doing, but it does mirror the semantics of Smalltalk.&lt;/p&gt;</content>
		<author>
			<name>wycats</name>
			<uri>http://yehudakatz.com</uri>
		</author>
		<source>
			<title type="html">Katz Got Your Tongue?</title>
			<subtitle type="html">Random Geek-Related Thoughts</subtitle>
			<link rel="self" href="http://yehudakatz.com/feed/atom/"/>
			<id>http://yehudakatz.com/feed/atom/</id>
			<updated>2010-03-13T08:20:07+00:00</updated>
		</source>
	</entry>

	<entry xml:lang="en">
		<title type="html">&amp;#8220;The Bar for Success in Our Industry&amp;#8221; &amp;#8212; A Quibble</title>
		<link href="http://yehudakatz.com/2009/09/02/the-bar-for-success-in-our-industry-a-quibble/"/>
		<id>http://yehudakatz.com/?p=342</id>
		<updated>2009-09-03T02:04:32+00:00</updated>
		<content type="html">&lt;p&gt;I read &lt;a href=&quot;http://37signals.com/svn/posts/1890-the-bar-for-success-in-our-industry-is-too-low&quot;&gt;The bar for success in our industry is too low&lt;/a&gt;  with interest. I generally like and agree with the argument that as an entrepreneur, people should build businesses around sustainable practices that involve actually making money in the short-term. The fact that the advice comes from a successful business like 37 Signals, which has actually made sustainable, long-term money, makes it further credible.&lt;/p&gt;
&lt;p&gt;I also agree that the analysis of Evernote as a &amp;#8220;success&amp;#8221; on the basis of projected income earned incrementally (a month at a time) over a year from now is a pretty bizarre way to define present success.&lt;/p&gt;
&lt;p&gt;That said, I think their success blinds them to alternative, more risky ways of being successful with technology. In Jason&amp;#8217;s post, he analogizes web businesses to stores:&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;If there was an airline that flew more passengers than anyone else, but lost money on each one, would we call it a success? If there was a restaurant that served more people than anyone else, but lost money on each meal served, would we call it a success? If there was a store that sold more product than anyone else, but took a loss on each one, would we call it a success? Would the business press hold these companies up as business model successes? Would anyone? Interesting, maybe. Promising, sure. But successful? Then what the hell is going on with the coverage of our industry?&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;There are, however, an entirely different mode of doing business that is well-represented in the traditional sphere. While it is certainly more risky, large companies often sink millions or billions of dollars into research, hoping it will pay off by providing them a sellable product. Microsoft, for instance, spent more than 9 billion dollars on research in FY 2008, almost double what it spent on income tax. &lt;a href=&quot;http://finance.yahoo.com/q/is?s=MSFT&amp;#038;annual&quot;&gt;Around one in six dollars that comes in to Microsoft in revenue are spent on research&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;By Jason&amp;#8217;s argument, Microsoft Research is not &amp;#8220;successful&amp;#8221;, and if someone says it is, they are using an unacceptably low bar for success. It would be more appropriate to think of Microsoft as aggregating significant amounts of risky propositions into a single department, on the gamble that enough of those propositions will be successful to make the expenditure worth it, even if it takes a while for those propositions to pay off.&lt;/p&gt;
&lt;p&gt;In effect, venture capitalists are making the exact same gamble. The underlying business model is still the same: plunk down a bunch of cash on research projects that, if successful, will result in significant payoff. The difference is that Internet businesses are very amenable to a more distributed model, where individual folks with ideas do the research, shouldering some of the risk of failure and some of the reward for success.&lt;/p&gt;
&lt;p&gt;In other words, these sorts of businesses are simply accumulating value&amp;#8211;in followers, users, or even eyeballs. They will be successful when they can sell that value (usually, all at once), just as Microsoft will be successful when they can ship products based on years of research (usually, all at once). According to Jason&amp;#8217;s analysis, Twitter is not a success. That is a fundamentally flawed analysis as they have built enough value at this point to be converted into a lump-sum payoff, even if they have not yet cashed in.&lt;/p&gt;
&lt;p&gt;However, none of this is to say that as an entrepreneur, you should build your business around the hope of such a payoff. It&amp;#8217;s a highly risky way to build a business, and is subsidizing venture capitalists, who don&amp;#8217;t actually share in the risk (like Microsoft, they are aggregating risk into a larger research pool). But if taking big risks is your thing, taking on a long-term research project is a perfectly reasonable way to build a business.&lt;/p&gt;</content>
		<author>
			<name>wycats</name>
			<uri>http://yehudakatz.com</uri>
		</author>
		<source>
			<title type="html">Katz Got Your Tongue?</title>
			<subtitle type="html">Random Geek-Related Thoughts</subtitle>
			<link rel="self" href="http://yehudakatz.com/feed/atom/"/>
			<id>http://yehudakatz.com/feed/atom/</id>
			<updated>2010-03-13T08:20:07+00:00</updated>
		</source>
	</entry>

	<entry xml:lang="en">
		<title type="html">Simplifying Rails Block Helpers (With a Side of Rubinius)</title>
		<link href="http://feedproxy.google.com/~r/KatzGotYourTongue/~3/sRwDyaDt_Wk/"/>
		<id>http://yehudakatz.com/?p=332</id>
		<updated>2009-08-31T06:09:49+00:00</updated>
		<content type="html">&lt;p&gt;We all know that &lt;code&gt;&amp;lt;%= string %&amp;gt;&lt;/code&gt; emits a String in ERB. And &lt;code&gt;&amp;lt;% string %&amp;gt;&lt;/code&gt; runs Ruby code, but does not emit a String. When starting working with Rails, you almost expect the syntax for block helpers to be:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;&amp;lt;%&lt;/span&gt;= content_tag&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;:div&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt; &lt;span&gt;do&lt;/span&gt; &lt;span&gt;%&amp;gt;&lt;/span&gt;
  The content
&lt;span&gt;&amp;lt;%&lt;/span&gt; &lt;span&gt;end&lt;/span&gt; &lt;span&gt;%&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Why doesn&amp;#8217;t it work that way?&lt;/p&gt;
&lt;p&gt;It has to do with how the ERB parser works, looking at each line individually. When it sees &lt;code&gt;&amp;lt;% %&amp;gt;&lt;/code&gt;, it evaluates the code as a line of Ruby. When it sees &lt;code&gt;&amp;lt;%= %&amp;gt;&lt;/code&gt;, it evaluates the inside of the ERB tag, and calls &lt;code&gt;to_s&lt;/code&gt; on it.&lt;/p&gt;
&lt;p&gt;This:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;erb&quot;&gt;&amp;lt;% form_for(@object) do %&amp;gt;
Stuff
&amp;lt;% end %&amp;gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;gets effectively converted to:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;form_for&lt;span&gt;&amp;#40;&lt;/span&gt;@object&lt;span&gt;&amp;#41;&lt;/span&gt; &lt;span&gt;do&lt;/span&gt;
_buf &lt;span&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;&amp;quot;Stuff&amp;quot;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;.&lt;span&gt;to_s&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;On the other hand, this:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;erb&quot;&gt;&amp;lt;%= form_for(@object) do %&amp;gt;
Stuff
&amp;lt;% end %&amp;gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;gets converted to:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;_buf &lt;span&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span&gt;&amp;#40;&lt;/span&gt;form_for&lt;span&gt;&amp;#40;&lt;/span&gt;@object&lt;span&gt;&amp;#41;&lt;/span&gt; &lt;span&gt;do&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;.&lt;span&gt;to_s&lt;/span&gt;
_buf &lt;span&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;&amp;quot;Stuff&amp;quot;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;.&lt;span&gt;to_s&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;which isn&amp;#8217;t valid Ruby. So we use the first approach, and then let the helper itself, rather than ERB, be responsible for concatenating to the buffer. Sadly, it leads to significantly more complex helpers.&lt;/p&gt;
&lt;p&gt;Let&amp;#8217;s take a look at the implementation of &lt;code&gt;content_tag&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;def&lt;/span&gt; content_tag&lt;span&gt;&amp;#40;&lt;/span&gt;name, content_or_options_with_block = &lt;span&gt;nil&lt;/span&gt;, options = &lt;span&gt;nil&lt;/span&gt;, escape = &lt;span&gt;true&lt;/span&gt;, &lt;span&gt;&amp;amp;&lt;/span&gt;block&lt;span&gt;&amp;#41;&lt;/span&gt;
  &lt;span&gt;if&lt;/span&gt; block_given?
    options = content_or_options_with_block &lt;span&gt;if&lt;/span&gt; content_or_options_with_block.&lt;span&gt;is_a&lt;/span&gt;?&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;Hash&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;
    content_tag = content_tag_string&lt;span&gt;&amp;#40;&lt;/span&gt;name, capture&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;block&lt;span&gt;&amp;#41;&lt;/span&gt;, options, escape&lt;span&gt;&amp;#41;&lt;/span&gt;
&amp;nbsp;
    &lt;span&gt;if&lt;/span&gt; block_called_from_erb?&lt;span&gt;&amp;#40;&lt;/span&gt;block&lt;span&gt;&amp;#41;&lt;/span&gt;
      concat&lt;span&gt;&amp;#40;&lt;/span&gt;content_tag&lt;span&gt;&amp;#41;&lt;/span&gt;
    &lt;span&gt;else&lt;/span&gt;
      content_tag
    &lt;span&gt;end&lt;/span&gt;
  &lt;span&gt;else&lt;/span&gt;
    content_tag_string&lt;span&gt;&amp;#40;&lt;/span&gt;name, content_or_options_with_block, options, escape&lt;span&gt;&amp;#41;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The important chunk here is the middle, inside of the &lt;code&gt;if block_given?&lt;/code&gt; section. The first few lines just get the actual contents, using the &lt;code&gt;capture&lt;/code&gt; helper to pull out the contents of the block. But then you get this:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;if&lt;/span&gt; block_called_from_erb?&lt;span&gt;&amp;#40;&lt;/span&gt;block&lt;span&gt;&amp;#41;&lt;/span&gt;
  concat&lt;span&gt;&amp;#40;&lt;/span&gt;content_tag&lt;span&gt;&amp;#41;&lt;/span&gt;
&lt;span&gt;else&lt;/span&gt;
  content_tag
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This is actually a requirement for writing a block helper of any kind in Rails. First, Rails checks to see if the block is being called from ERB. If so, it takes care of concatenating to the buffer. Otherwise, the caller simply wants a String back, so it returns it.&lt;/p&gt;
&lt;p&gt;Worse, here&amp;#8217;s the implementation of &lt;code&gt;block_called_from_erb?&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;BLOCK_CALLED_FROM_ERB = 'defined? __in_erb_template'
&amp;nbsp;
# Check whether we're called from an erb template.
# We'd return a string in any other case, but erb &lt;span&gt;&amp;lt;%&lt;/span&gt;= ... &lt;span&gt;%&amp;gt;&lt;/span&gt;
# can't take an &lt;span&gt;&amp;lt;%&lt;/span&gt; &lt;span&gt;end&lt;/span&gt; &lt;span&gt;%&amp;gt;&lt;/span&gt; later on, so we have to use &lt;span&gt;&amp;lt;%&lt;/span&gt; ... &lt;span&gt;%&amp;gt;&lt;/span&gt;
# and implicitly concat.
def block_called_from_erb?(block)
  block &amp;amp;&amp;amp; eval(BLOCK_CALLED_FROM_ERB, block)
end&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;So every time you use a block helper in Rails, or use a helper which uses a block helper, Rails is forced to eval into the block to determine what the context is.&lt;/p&gt;
&lt;p&gt;In Merb, we solved this problem by using this syntax:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;erb&quot;&gt;&amp;lt;%= form_for(@object) do %&amp;gt;
Stuff
&amp;lt;% end =%&amp;gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;And while everyone agrees that the opening &lt;code&gt;&amp;lt;%=&lt;/code&gt; is a reasonable change, the closing &lt;code&gt;=%&amp;gt;&lt;/code&gt; is a bit grating. However, it allows us to compile the above code into:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;_buf &lt;span&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span&gt;&amp;#40;&lt;/span&gt;form_for&lt;span&gt;&amp;#40;&lt;/span&gt;@object&lt;span&gt;&amp;#41;&lt;/span&gt; &lt;span&gt;do&lt;/span&gt;
_buf &lt;span&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;&amp;quot;Stuff&amp;quot;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;.&lt;span&gt;to_s&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;.&lt;span&gt;to_s&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;That&amp;#8217;s because we tag the &lt;code&gt;end&lt;/code&gt; with a special ERB tag that allows us to attach a &lt;code&gt;).to_s&lt;/code&gt; to the end. We use Erubis, which lets us control the compilation process more finely, to hook into this process.&lt;/p&gt;
&lt;p&gt;Rails 3 will use Erubis regardless of this problem to implement on-by-default XSS protection, but I needed a solution that didn&amp;#8217;t require the closing &lt;code&gt;=%&amp;gt;&lt;/code&gt; (ideally).&lt;/p&gt;
&lt;p&gt;Evan (lead on Rubinius) hit upon a rather ingenious idea: use Ruby operator precedence to get around the need to know where the &lt;code&gt;end&lt;/code&gt; was. Effectively, compile into the following:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;_buf &lt;span&gt;&amp;lt;&amp;lt;&lt;/span&gt; capture_obj &lt;span&gt;&amp;lt;&amp;lt;&lt;/span&gt; form_for&lt;span&gt;&amp;#40;&lt;/span&gt;@object&lt;span&gt;&amp;#41;&lt;/span&gt; &lt;span&gt;do&lt;/span&gt;
_buf &lt;span&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;&amp;quot;Stuff&amp;quot;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;.&lt;span&gt;to_s&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;where &lt;code&gt;capture_obj&lt;/code&gt; is:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;class&lt;/span&gt; CaptureObject
  &lt;span&gt;def&lt;/span&gt; &lt;span&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;obj&lt;span&gt;&amp;#41;&lt;/span&gt;
    &lt;span&gt;@object&lt;/span&gt; = obj
    &lt;span&gt;self&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;def&lt;/span&gt; to_str
    &lt;span&gt;@object&lt;/span&gt;.&lt;span&gt;to_s&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;def&lt;/span&gt; to_s
    &lt;span&gt;@object&lt;/span&gt;.&lt;span&gt;to_s&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Unfortunately, with one hand Ruby operator precedence giveth, and with one hand it taketh away. In order to test this, I tried using a helper that returned an object, rather than a String (valid in ERB). In ERB, this would call to_s on the object. When I tried to run this code with the &lt;code&gt;CaptureObject&lt;/code&gt;, I got:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;text&quot;&gt;template template:1:in `&amp;lt;&amp;lt;': can't convert Object into String (TypeError)
   from template template:1:in `template'
   from helper_spike.rb:48&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Evan and I were both a bit baffled by this (although it retrospect we probably shouldn&amp;#8217;t have been), and we hit on the idea to try running the code through Rubinius and look at its backtrace:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;text&quot;&gt;An exception occurred running helper_spike.rb
    Coercion error: #&amp;lt;Object:0x60a&amp;gt;.to_str =&amp;gt; String failed:
(No method 'to_str' on an instance of Object.) (TypeError)
&amp;nbsp;
Backtrace:
                       Type.coerce_to at kernel/common/type.rb:22
           Kernel(String)#StringValue at kernel/common/kernel.rb:82
                            String#&amp;lt;&amp;lt; at kernel/common/string.rb:93
                   MyContext#template at template template:1
                      main.__script__ at helper_spike.rb:48&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;By looking at Rubinius&amp;#8217; backtrace, we quickly realized that the order of operations was wrong, and &lt;code&gt;to_str&lt;/code&gt; was getting called on the return value from the helper, rather than the &lt;code&gt;CaptureObject&lt;/code&gt;. As I tweeted immediately thereafter, the information available in Rubinius&amp;#8217; backtrace is just phenomenal, exposing enough information to really see what&amp;#8217;s going on. Because the internals of Rubinius are written in Ruby, the Ruby backtrace goes all the way through to the &lt;code&gt;Type.coerce_to&lt;/code&gt; method.&lt;/p&gt;
&lt;p&gt;After realizing that, we changed the implementation of &lt;code&gt;CaptureObject&lt;/code&gt; to take the buffer in its initializer, and have it handle concatenating to the buffer. The compiled code now looks like:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;capture_obj &lt;span&gt;&amp;lt;&amp;lt;&lt;/span&gt; form_for&lt;span&gt;&amp;#40;&lt;/span&gt;@object&lt;span&gt;&amp;#41;&lt;/span&gt; &lt;span&gt;do&lt;/span&gt;
_buf &lt;span&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;&amp;quot;Stuff&amp;quot;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;.&lt;span&gt;to_s&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;and the &lt;code&gt;CaptureObject&lt;/code&gt; looks like:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;class&lt;/span&gt; CaptureObject
  &lt;span&gt;def&lt;/span&gt; initialize&lt;span&gt;&amp;#40;&lt;/span&gt;buf&lt;span&gt;&amp;#41;&lt;/span&gt;
    &lt;span&gt;@buf&lt;/span&gt; = buf
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;def&lt;/span&gt; &lt;span&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;obj&lt;span&gt;&amp;#41;&lt;/span&gt;
    &lt;span&gt;@buf&lt;/span&gt; &lt;span&gt;&amp;lt;&amp;lt;&lt;/span&gt; obj.&lt;span&gt;to_s&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now, Ruby&amp;#8217;s operator precedence will bind the &lt;code&gt;do&lt;/code&gt; to the &lt;code&gt;form_for&lt;/code&gt;, and the return value of &lt;code&gt;form_for&lt;/code&gt; will be &lt;code&gt;to_s&lt;/code&gt;&amp;#8216;ed and concatenated to the buffer.&lt;/p&gt;
&lt;p&gt;And the best thing is the implementation of content_tag once that&amp;#8217;s done:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;def&lt;/span&gt; content_tag&lt;span&gt;&amp;#40;&lt;/span&gt;name, content = &lt;span&gt;nil&lt;/span&gt;, options = &lt;span&gt;nil&lt;/span&gt;, escape = &lt;span&gt;true&lt;/span&gt;, &lt;span&gt;&amp;amp;&lt;/span&gt;block&lt;span&gt;&amp;#41;&lt;/span&gt;
  &lt;span&gt;if&lt;/span&gt; block_given?
    options = content &lt;span&gt;if&lt;/span&gt; content.&lt;span&gt;is_a&lt;/span&gt;?&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;Hash&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;
    content = capture&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;block&lt;span&gt;&amp;#41;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
  content_tag_string&lt;span&gt;&amp;#40;&lt;/span&gt;name, content, options, escape&lt;span&gt;&amp;#41;&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We can simply return a String and ERB handles the concatenation work. That&amp;#8217;s the important part: helper writers should be able to think of block helpers the same way they think about traditional helpers. Somewhat less importantly, we&amp;#8217;ll be able to eliminate &lt;code&gt;eval&lt;/code&gt;ing into untold numbers of blocks at runtime.&lt;/p&gt;
&lt;p&gt;This was only an experiment, and the specific details still need to be worked out (how do we do this without breaking untold numbers of existing applications), I&amp;#8217;m very happy with this solution, which provides the simplicity and performance enhancement of the Merb solution without the ugly &lt;code&gt;=%&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;feedflare&quot;&gt;
&lt;a href=&quot;http://feeds.feedburner.com/~ff/KatzGotYourTongue?a=sRwDyaDt_Wk:JXKMZZpEdsg:yIl2AUoC8zA&quot;&gt;&lt;img src=&quot;http://feeds.feedburner.com/~ff/KatzGotYourTongue?d=yIl2AUoC8zA&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src=&quot;http://feeds.feedburner.com/~r/KatzGotYourTongue/~4/sRwDyaDt_Wk&quot; height=&quot;1&quot; width=&quot;1&quot; /&gt;</content>
		<author>
			<name>wycats</name>
			<uri>http://yehudakatz.com</uri>
		</author>
		<source>
			<title type="html">Katz Got Your Tongue?</title>
			<subtitle type="html">Random Geek-Related Thoughts</subtitle>
			<link rel="self" href="http://yehudakatz.com/feed/atom/"/>
			<id>http://yehudakatz.com/feed/atom/</id>
			<updated>2010-03-13T08:20:07+00:00</updated>
		</source>
	</entry>

	<entry xml:lang="en">
		<title type="html">Blog status</title>
		<link href="http://merbist.com/2009/08/30/blog-status/"/>
		<id>http://merbist.com/?p=549</id>
		<updated>2009-08-31T00:35:01+00:00</updated>
		<content type="html">&lt;p&gt;I had people asking me how come I was not blogging as much lately. Well, on top of being really busy, I have been blogging on other blogs such as the &lt;a title=&quot;Ruby on Rails blog&quot; href=&quot;http://weblog.rubyonrails.org/&quot; target=&quot;_blank&quot;&gt;official Rails blog&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;There aren&amp;#8217;t a lot of Merb news, we are waiting for Carl and Yehuda to sign out on the 1.1 release and will hopefully soon start the migration work to Rails3. If you want more news about Rails3, check &lt;a title=&quot;Yehuda Katz&quot; href=&quot;http://yehudakatz.com/&quot; target=&quot;_blank&quot;&gt;Yehuda&amp;#8217;s blog&lt;/a&gt; and the &lt;a title=&quot;Rails&quot; href=&quot;http://weblog.rubyonrails.com/&quot; target=&quot;_blank&quot;&gt;official Rails blog&lt;/a&gt;. Lots of exciting things are coming up.&lt;/p&gt;
&lt;p&gt;Finally, if you are planning on upgrading your Mac to Snow Leopard, &lt;a title=&quot;Upgrading to Snow Leopard&quot; href=&quot;http://weblog.rubyonrails.org/2009/8/30/upgrading-to-snow-leopard&quot; target=&quot;_blank&quot;&gt;these instructions&lt;/a&gt; are also valid for all Ruby developers on an Intel mac.&lt;/p&gt;</content>
		<author>
			<name>Matt Aimonetti (mattetti)</name>
			<uri>http://merbist.com</uri>
		</author>
		<source>
			<title type="html">Merbist</title>
			<subtitle type="html">Rails 3, Ruby &amp;amp; MacRuby news - tutorials - consulting - training</subtitle>
			<link rel="self" href="http://merbist.com/feed/"/>
			<id>http://merbist.com/feed/</id>
			<updated>2010-03-13T12:20:05+00:00</updated>
		</source>
	</entry>

	<entry xml:lang="en">
		<title type="html">How to Build Sinatra on Rails 3</title>
		<link href="http://feedproxy.google.com/~r/KatzGotYourTongue/~3/1H4hqEo0QEA/"/>
		<id>http://yehudakatz.com/?p=317</id>
		<updated>2009-08-27T19:13:28+00:00</updated>
		<content type="html">&lt;p&gt;In Ruby, we have the great fortune to have one major framework (Rails) and a number of minor frameworks that drive innovation forward. One of the great minor frameworks which has been getting a lot of traction recently is Sinatra, primarily because it exposes a great DSL for writing small, single-purpose apps.&lt;/p&gt;
&lt;p&gt;Here&amp;#8217;s an example of a simple Sinatra application.&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;class&lt;/span&gt; MyApp &lt;span&gt;&amp;lt;&lt;/span&gt; &lt;span&gt;Sinatra::Base&lt;/span&gt;
  set &lt;span&gt;:views&lt;/span&gt;, &lt;span&gt;File&lt;/span&gt;.&lt;span&gt;dirname&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;__FILE__&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;
  enable &lt;span&gt;:sessions&lt;/span&gt;
&amp;nbsp;
  before &lt;span&gt;do&lt;/span&gt;
    halt &lt;span&gt;if&lt;/span&gt; session&lt;span&gt;&amp;#91;&lt;/span&gt;&lt;span&gt;:fail&lt;/span&gt;&lt;span&gt;&amp;#93;&lt;/span&gt; == &lt;span&gt;true&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
  get &lt;span&gt;&amp;quot;/hello&amp;quot;&lt;/span&gt; &lt;span&gt;do&lt;/span&gt;
    &lt;span&gt;&amp;quot;Hello world&amp;quot;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
  get &lt;span&gt;&amp;quot;/world&amp;quot;&lt;/span&gt; &lt;span&gt;do&lt;/span&gt;
    &lt;span&gt;@name&lt;/span&gt; = &lt;span&gt;&amp;quot;Carl&amp;quot;&lt;/span&gt;
    erb &lt;span&gt;:awesomesauce&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
  get &lt;span&gt;&amp;quot;/fail&amp;quot;&lt;/span&gt; &lt;span&gt;do&lt;/span&gt;
    session&lt;span&gt;&amp;#91;&lt;/span&gt;&lt;span&gt;:fail&lt;/span&gt;&lt;span&gt;&amp;#93;&lt;/span&gt; = &lt;span&gt;true&lt;/span&gt;
    &lt;span&gt;&amp;quot;You failed&amp;quot;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;There&amp;#8217;s a lot of functionality packed into this little package. You can declare some code to be run before all actions, declare actions and the URL they should be routed from, use rendering semantics, and even use sessions.&lt;/p&gt;
&lt;p&gt;We&amp;#8217;ve been saying that Rails 3 is flexible enough to use as a framework toolkit&amp;#8211;let&amp;#8217;s prove it by using Rails to build the subset of the Sinatra DSL described above.&lt;/p&gt;
&lt;p&gt;Let&amp;#8217;s start with a very tiny subset of the DSL:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;class&lt;/span&gt; MyApp &lt;span&gt;&amp;lt;&lt;/span&gt; &lt;span&gt;Sinatra::Base&lt;/span&gt;
  get &lt;span&gt;&amp;quot;/hello&amp;quot;&lt;/span&gt; &lt;span&gt;do&lt;/span&gt;
    &lt;span&gt;&amp;quot;HELLO World&amp;quot;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
  post &lt;span&gt;&amp;quot;/world&amp;quot;&lt;/span&gt; &lt;span&gt;do&lt;/span&gt;
    &lt;span&gt;&amp;quot;Hello WORLD&amp;quot;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The first step is to declare the Sinatra base class:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;module&lt;/span&gt; Sinatra
  &lt;span&gt;class&lt;/span&gt; Base &lt;span&gt;&amp;lt;&lt;/span&gt; &lt;span&gt;ActionController::Metal&lt;/span&gt;
    &lt;span&gt;include&lt;/span&gt; &lt;span&gt;ActionController::RackConvenience&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We start off by making Sinatra::Base a subclass of the bare metal ActionController implementation, which provides just enough infrastructure to get going. We also include the RackConvenience module, which provides &lt;code&gt;request&lt;/code&gt; and &lt;code&gt;response&lt;/code&gt; and handles some basic Rack tasks for us.&lt;/p&gt;
&lt;p&gt;Next, let&amp;#8217;s add support for the GET and POST method:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;class&lt;/span&gt; &lt;span&gt;Sinatra::Base&lt;/span&gt;
  &lt;span&gt;def&lt;/span&gt; &lt;span&gt;self&lt;/span&gt;.&lt;span&gt;inherited&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;klass&lt;span&gt;&amp;#41;&lt;/span&gt;
    klass.&lt;span&gt;class_eval&lt;/span&gt; &lt;span&gt;&amp;#123;&lt;/span&gt; &lt;span&gt;@_routes&lt;/span&gt; = &lt;span&gt;&amp;#91;&lt;/span&gt;&lt;span&gt;&amp;#93;&lt;/span&gt; &lt;span&gt;&amp;#125;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;class&lt;/span&gt; &lt;span&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span&gt;self&lt;/span&gt;
    &lt;span&gt;def&lt;/span&gt; get&lt;span&gt;&amp;#40;&lt;/span&gt;uri, options = &lt;span&gt;&amp;#123;&lt;/span&gt;&lt;span&gt;&amp;#125;&lt;/span&gt;, &lt;span&gt;&amp;amp;&lt;/span&gt;block&lt;span&gt;&amp;#41;&lt;/span&gt;  route&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;:get&lt;/span&gt;,  uri, options, &lt;span&gt;&amp;amp;&lt;/span&gt;block&lt;span&gt;&amp;#41;&lt;/span&gt; &lt;span&gt;end&lt;/span&gt;
    &lt;span&gt;def&lt;/span&gt; post&lt;span&gt;&amp;#40;&lt;/span&gt;uri, options = &lt;span&gt;&amp;#123;&lt;/span&gt;&lt;span&gt;&amp;#125;&lt;/span&gt;, &lt;span&gt;&amp;amp;&lt;/span&gt;block&lt;span&gt;&amp;#41;&lt;/span&gt; route&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;:post&lt;/span&gt;, uri, options, &lt;span&gt;&amp;amp;&lt;/span&gt;block&lt;span&gt;&amp;#41;&lt;/span&gt; &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
    &lt;span&gt;def&lt;/span&gt; route&lt;span&gt;&amp;#40;&lt;/span&gt;http_method, uri, options, &lt;span&gt;&amp;amp;&lt;/span&gt;block&lt;span&gt;&amp;#41;&lt;/span&gt;
      action_name = &lt;span&gt;&amp;quot;[#{http_method}] #{uri}&amp;quot;&lt;/span&gt;
      &lt;span&gt;@_routes&lt;/span&gt; &lt;span&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span&gt;&amp;#123;&lt;/span&gt;:method &lt;span&gt;=&amp;gt;&lt;/span&gt; http_method.&lt;span&gt;to_s&lt;/span&gt;.&lt;span&gt;upcase&lt;/span&gt;, &lt;span&gt;:uri&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; uri,
                   &lt;span&gt;:action&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; action_name, &lt;span&gt;:options&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; options&lt;span&gt;&amp;#125;&lt;/span&gt;
      define_method&lt;span&gt;&amp;#40;&lt;/span&gt;action_name, &lt;span&gt;&amp;amp;&lt;/span&gt;block&lt;span&gt;&amp;#41;&lt;/span&gt;
    &lt;span&gt;end&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We&amp;#8217;ve simply defined some class methods on the Sinatra::Base to store off routing details for the &lt;code&gt;get&lt;/code&gt; and &lt;code&gt;post&lt;/code&gt; methods, and creating a new method named &lt;code&gt;[GET] /hello&lt;/code&gt;. This is a bit of an interesting Ruby trick; while the &lt;code&gt;def&lt;/code&gt; keyword has strict semantics for method names, &lt;code&gt;define_method&lt;/code&gt; allows any string.&lt;/p&gt;
&lt;p&gt;Now we need to wire up the actual routing. There are a number of options, including the Rails router (rack-mount, rack-router, and usher are all new, working Rails-like routers). We&amp;#8217;ll use Usher, a fast Rails-like router written by Josh Hull.&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;class&lt;/span&gt; &lt;span&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span&gt;Sinatra::Base&lt;/span&gt;
  &lt;span&gt;def&lt;/span&gt; to_app
    routes, controller = &lt;span&gt;@_routes&lt;/span&gt;, &lt;span&gt;self&lt;/span&gt;
&amp;nbsp;
    &lt;span&gt;Usher::Interface&lt;/span&gt;.&lt;span&gt;for&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;:rack&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt; &lt;span&gt;do&lt;/span&gt;
      routes.&lt;span&gt;each&lt;/span&gt; &lt;span&gt;do&lt;/span&gt; &lt;span&gt;|&lt;/span&gt;route&lt;span&gt;|&lt;/span&gt;
        add&lt;span&gt;&amp;#40;&lt;/span&gt;route&lt;span&gt;&amp;#91;&lt;/span&gt;&lt;span&gt;:uri&lt;/span&gt;&lt;span&gt;&amp;#93;&lt;/span&gt;, &lt;span&gt;:conditions&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; &lt;span&gt;&amp;#123;&lt;/span&gt;:method &lt;span&gt;=&amp;gt;&lt;/span&gt; route&lt;span&gt;&amp;#91;&lt;/span&gt;&lt;span&gt;:method&lt;/span&gt;&lt;span&gt;&amp;#93;&lt;/span&gt;&lt;span&gt;&amp;#125;&lt;/span&gt;.&lt;span&gt;merge&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;route&lt;span&gt;&amp;#91;&lt;/span&gt;&lt;span&gt;:options&lt;/span&gt;&lt;span&gt;&amp;#93;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;.
          &lt;span&gt;to&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;controller.&lt;span&gt;action&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;route&lt;span&gt;&amp;#91;&lt;/span&gt;&lt;span&gt;:action&lt;/span&gt;&lt;span&gt;&amp;#93;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;
      &lt;span&gt;end&lt;/span&gt;
    &lt;span&gt;end&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Here, we define to_app, which is used by Rack to convert a parameter to &lt;code&gt;run&lt;/code&gt; into a valid Rack application. We create a new Usher interface, and add a route for each route created by Sinatra. &lt;code&gt;Because Usher::Interface.for&lt;/code&gt; uses instance_eval for its DSL, we store off the routes and controller in local variables that will still be available in the closure.&lt;/p&gt;
&lt;p&gt;One little detail here: In Rails 3, each action in a controller is a valid rack endpoint. You get the endpoint by doing &lt;code&gt;ControllerName.action(method_name)&lt;/code&gt;. Here, we&amp;#8217;re simply pulling out the action named &amp;#8220;[GET] /hello&amp;#8221; that we created in &lt;code&gt;route&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The final piece of the puzzle is covering the action processing in the controller itself. For this, we will mostly reuse the default action processing, with a small change:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;class&lt;/span&gt; &lt;span&gt;Sinatra::Base&lt;/span&gt;
  &lt;span&gt;def&lt;/span&gt; process_action&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;*&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;
    &lt;span&gt;self&lt;/span&gt;.&lt;span&gt;response_body&lt;/span&gt; = &lt;span&gt;super&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;What&amp;#8217;s happening here is that Rails does not treat the return value of the action as significant, instead expecting it to be set using &lt;code&gt;render&lt;/code&gt;, but Sinatra treats the returned string as significant. As a result, we set the &lt;code&gt;response_body&lt;/code&gt; to the return value of the action.&lt;/p&gt;
&lt;p&gt;Next, let&amp;#8217;s add session support.&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;class&lt;/span&gt; &lt;span&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span&gt;Sinatra::Base&lt;/span&gt;
  &lt;span&gt;def&lt;/span&gt; set&lt;span&gt;&amp;#40;&lt;/span&gt;name, value&lt;span&gt;&amp;#41;&lt;/span&gt;
    send&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;&amp;quot;_set_#{name}&amp;quot;&lt;/span&gt;, value&lt;span&gt;&amp;#41;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;def&lt;/span&gt; enable&lt;span&gt;&amp;#40;&lt;/span&gt;name&lt;span&gt;&amp;#41;&lt;/span&gt;
    set&lt;span&gt;&amp;#40;&lt;/span&gt;name, &lt;span&gt;true&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;def&lt;/span&gt; _set_sessions&lt;span&gt;&amp;#40;&lt;/span&gt;value&lt;span&gt;&amp;#41;&lt;/span&gt;
    &lt;span&gt;@_sessions&lt;/span&gt; = value
    &lt;span&gt;include&lt;/span&gt; &lt;span&gt;ActionController::Session&lt;/span&gt; &lt;span&gt;if&lt;/span&gt; value
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;def&lt;/span&gt; to_app
    routes, controller = &lt;span&gt;@_routes&lt;/span&gt;, &lt;span&gt;self&lt;/span&gt;
&amp;nbsp;
    app = &lt;span&gt;Usher::Interface&lt;/span&gt;.&lt;span&gt;for&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;:rack&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt; &lt;span&gt;do&lt;/span&gt;
      routes.&lt;span&gt;each&lt;/span&gt; &lt;span&gt;do&lt;/span&gt; &lt;span&gt;|&lt;/span&gt;route&lt;span&gt;|&lt;/span&gt;
        add&lt;span&gt;&amp;#40;&lt;/span&gt;route&lt;span&gt;&amp;#91;&lt;/span&gt;&lt;span&gt;:uri&lt;/span&gt;&lt;span&gt;&amp;#93;&lt;/span&gt;, &lt;span&gt;:conditions&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; &lt;span&gt;&amp;#123;&lt;/span&gt;:method &lt;span&gt;=&amp;gt;&lt;/span&gt; route&lt;span&gt;&amp;#91;&lt;/span&gt;&lt;span&gt;:method&lt;/span&gt;&lt;span&gt;&amp;#93;&lt;/span&gt;&lt;span&gt;&amp;#125;&lt;/span&gt;.&lt;span&gt;merge&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;route&lt;span&gt;&amp;#91;&lt;/span&gt;&lt;span&gt;:options&lt;/span&gt;&lt;span&gt;&amp;#93;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;.
          &lt;span&gt;to&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;controller.&lt;span&gt;action&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;route&lt;span&gt;&amp;#91;&lt;/span&gt;&lt;span&gt;:action&lt;/span&gt;&lt;span&gt;&amp;#93;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;
      &lt;span&gt;end&lt;/span&gt;
    &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
    &lt;span&gt;if&lt;/span&gt; &lt;span&gt;@_sessions&lt;/span&gt;
      app = &lt;span&gt;ActionDispatch::Session::CookieStore&lt;/span&gt;.&lt;span&gt;new&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;app, &lt;span&gt;&amp;#123;&lt;/span&gt;:key &lt;span&gt;=&amp;gt;&lt;/span&gt; &lt;span&gt;&amp;quot;_secret_key&amp;quot;&lt;/span&gt;,
        &lt;span&gt;:secret&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; &lt;span&gt;Digest::SHA2&lt;/span&gt;.&lt;span&gt;hexdigest&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;Time&lt;/span&gt;.&lt;span&gt;now&lt;/span&gt;.&lt;span&gt;to_s&lt;/span&gt; &lt;span&gt;+&lt;/span&gt; &lt;span&gt;rand&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;100&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;.&lt;span&gt;to_s&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;&lt;span&gt;&amp;#125;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;
    &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
    app
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;There&amp;#8217;s a few things going on here. First, Sinatra provides an API for setting options: &lt;code&gt;set :option, :value&lt;/code&gt;. In Sinatra, &lt;code&gt;enable :option&lt;/code&gt; is equivalent to &lt;code&gt;set :option, true&lt;/code&gt;. To simplify adding new options, we just delegate &lt;code&gt;set :whatever, value&lt;/code&gt; to a call to &lt;code&gt;_set_whatever(value)&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;We then implement &lt;code&gt;_set_sessions(value)&lt;/code&gt; to include &lt;code&gt;ActionController::Session&lt;/code&gt;, which provides the &lt;code&gt;session&lt;/code&gt; helper. In &lt;code&gt;to_app&lt;/code&gt;, we wrap the original application in an &lt;code&gt;ActionDispatch::Session::CookieStore&lt;/code&gt; if sessions were set.&lt;/p&gt;
&lt;p&gt;Next, we want to add in support for callbacks (&lt;code&gt;before do&lt;/code&gt;). It&amp;#8217;s only a few lines:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;class&lt;/span&gt; &lt;span&gt;Sinatra::Base&lt;/span&gt;
  &lt;span&gt;include&lt;/span&gt; &lt;span&gt;AbstractController::Callbacks&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
&lt;span&gt;class&lt;/span&gt; &lt;span&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span&gt;Sinatra::Base&lt;/span&gt;
  &lt;span&gt;alias&lt;/span&gt; before before_filter
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Basically, we pull in the normal Rails callback code, and then rename &lt;code&gt;before_filter&lt;/code&gt; to &lt;code&gt;before&lt;/code&gt; and we&amp;#8217;re good to go.&lt;/p&gt;
&lt;p&gt;Finally, let&amp;#8217;s dig into rendering.&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;class&lt;/span&gt; &lt;span&gt;Sinatra::Base&lt;/span&gt;
  &lt;span&gt;include&lt;/span&gt; &lt;span&gt;ActionController::RenderingController&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;def&lt;/span&gt; sinatra_render_file&lt;span&gt;&amp;#40;&lt;/span&gt;name&lt;span&gt;&amp;#41;&lt;/span&gt;
    render &lt;span&gt;:template&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; name.&lt;span&gt;to_s&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;def&lt;/span&gt; sinatra_render_inline&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;string&lt;/span&gt;, type&lt;span&gt;&amp;#41;&lt;/span&gt;
    render &lt;span&gt;:inline&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; &lt;span&gt;string&lt;/span&gt;, &lt;span&gt;:type&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; type
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;%&lt;/span&gt;w&lt;span&gt;&amp;#40;&lt;/span&gt;haml erb builder&lt;span&gt;&amp;#41;&lt;/span&gt;.&lt;span&gt;each&lt;/span&gt; &lt;span&gt;do&lt;/span&gt; &lt;span&gt;|&lt;/span&gt;type&lt;span&gt;|&lt;/span&gt;
    define_method&lt;span&gt;&amp;#40;&lt;/span&gt;type&lt;span&gt;&amp;#41;&lt;/span&gt; &lt;span&gt;do&lt;/span&gt; &lt;span&gt;|&lt;/span&gt;thing&lt;span&gt;|&lt;/span&gt;
      &lt;span&gt;return&lt;/span&gt; sinatra_render_inline&lt;span&gt;&amp;#40;&lt;/span&gt;thing, type&lt;span&gt;&amp;#41;&lt;/span&gt; &lt;span&gt;if&lt;/span&gt; thing.&lt;span&gt;is_a&lt;/span&gt;?&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;String&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;
      &lt;span&gt;return&lt;/span&gt; sinatra_render_file&lt;span&gt;&amp;#40;&lt;/span&gt;thing&lt;span&gt;&amp;#41;&lt;/span&gt;
    &lt;span&gt;end&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
&lt;span&gt;class&lt;/span&gt; &lt;span&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span&gt;Sinatra::Base&lt;/span&gt;
  &lt;span&gt;alias&lt;/span&gt; _set_views append_view_path
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We include the &lt;code&gt;RenderController&lt;/code&gt; module, which provides rendering support. Sinatra supports a few different syntaxes for rendering. It supports &lt;code&gt;erb :template_name&lt;/code&gt; which renders the ERB template named &lt;code&gt;template_name&lt;/code&gt;. It also supports &lt;code&gt;erb &quot;Some String&quot;&lt;/code&gt;, which renders the string uses the ERB engine.&lt;/p&gt;
&lt;p&gt;Rails supports both of those via &lt;code&gt;render :template&lt;/code&gt; and &lt;code&gt;render :inline&lt;/code&gt;, so we simply defer to that functionality in each case. We also handle Sinatra&amp;#8217;s &lt;code&gt;set :views, view_path&lt;/code&gt; by delegating to append_view_path.&lt;/p&gt;
&lt;p&gt;You can check out the full repository at https://github.com/wycats/railsnatra/&lt;/p&gt;
&lt;p&gt;So there you have it, a large subset of the Sinatra DSL written in Rails in under 100 lines of code. And if you want to add in more advanced Rails features, like layouts, flash, respond_to, file streaming, or conditional get support, it&amp;#8217;s just a simple module inclusion away.&lt;/p&gt;

&lt;div class=&quot;feedflare&quot;&gt;
&lt;a href=&quot;http://feeds.feedburner.com/~ff/KatzGotYourTongue?a=1H4hqEo0QEA:o2hhOBrcZjk:yIl2AUoC8zA&quot;&gt;&lt;img src=&quot;http://feeds.feedburner.com/~ff/KatzGotYourTongue?d=yIl2AUoC8zA&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src=&quot;http://feeds.feedburner.com/~r/KatzGotYourTongue/~4/1H4hqEo0QEA&quot; height=&quot;1&quot; width=&quot;1&quot; /&gt;</content>
		<author>
			<name>wycats</name>
			<uri>http://yehudakatz.com</uri>
		</author>
		<source>
			<title type="html">Katz Got Your Tongue?</title>
			<subtitle type="html">Random Geek-Related Thoughts</subtitle>
			<link rel="self" href="http://yehudakatz.com/feed/atom/"/>
			<id>http://yehudakatz.com/feed/atom/</id>
			<updated>2010-03-13T08:20:07+00:00</updated>
		</source>
	</entry>

	<entry xml:lang="en-US">
		<title type="html">Why I Won't Be Eating a Dozen Krispy Kremes Today</title>
		<link href="http://gweezlebur.com/2009/08/27/krispy-kreme.html"/>
		<id>http://gweezlebur.com/2009/08/27/krispy-kreme</id>
		<updated>2009-08-27T05:00:00+00:00</updated>
		<content type="html">&lt;p&gt;Tuesday night, in a discussion with Don, Jennifer, Shalini and Brad, I made the statement that I did not believe it would be that challenging to eat a dozen hot Krispy Kremes in a single sitting.&lt;/p&gt;

&lt;p&gt;I stand by that assertion.&lt;/p&gt;

&lt;p&gt;However, I will not be making any attempt to prove that I can do this. One dozen Krispy Kreme doughnuts contains 2400 calories and 144g of fat. While I believe it would not be difficult to consume this much, that doesn&amp;#8217;t mean it&amp;#8217;s a good idea. There are many things that I could do, and might even enjoy doing, that I do not do, because they&amp;#8217;re not healthy or in my best interests.&lt;/p&gt;

&lt;p&gt;If, in the future, I feel a desire to eat a dozen Krispy Kremes, I&amp;#8217;ll document the experience.&lt;/p&gt;</content>
		<author>
			<name>Michael Ivey</name>
			<email>ivey@gweezlebur.com</email>
			<uri>http://gweezlebur.com/</uri>
		</author>
		<source>
			<title type="html">Michael Ivey: blogging since '99</title>
			<link rel="self" href="http://gweezlebur.com/atom.xml"/>
			<id>http://gweezlebur.com/</id>
			<updated>2010-01-31T16:20:06+00:00</updated>
		</source>
	</entry>

	<entry xml:lang="en">
		<title type="html">My 10 Favorite Things About the Ruby Language</title>
		<link href="http://feedproxy.google.com/~r/KatzGotYourTongue/~3/SWU31FCevnc/"/>
		<id>http://yehudakatz.com/?p=311</id>
		<updated>2009-08-24T16:57:44+00:00</updated>
		<content type="html">&lt;p&gt;I work with Ruby every single day, and over time have come to really enjoy using it. Here&amp;#8217;s a list of some specific things that I really like about Ruby. Some of them are obvious, and some are shared with other languages. The purpose is to share things I like about Ruby, not to compare and contrast with any specific language.&lt;/p&gt;
&lt;h2&gt;1. Dynamic Typing&lt;/h2&gt;
&lt;p&gt;There are very good things about statically typed languages, such as compile-time verifiability and IDE support. However, in my experience, dynamic typing really helps get projects bootstrapped and smooths along changes, especially in the early to middle stages of a project.&lt;/p&gt;
&lt;p&gt;I&amp;#8217;m very happy that I don&amp;#8217;t need to create a formal interface for my new objects to implement simply to enable me to easily swap out that class for another later on.&lt;/p&gt;
&lt;h2&gt;2. Duck Typing&lt;/h2&gt;
&lt;p&gt;This is effectively just an extension of Dynamic Typing. In Ruby, methods that expect to be able to operate on String objects don&amp;#8217;t do checks for &lt;code&gt;is_a?(String)&lt;/code&gt;. They check whether the object &lt;code&gt;respond_to?(:to_str)&lt;/code&gt; and then calls &lt;code&gt;to_str&lt;/code&gt; on the Object if it does. Similarly, objects that represent Paths in Ruby can implement a &lt;code&gt;to_path&lt;/code&gt; method to provide the path representation.&lt;/p&gt;
&lt;p&gt;In Rails, we use this technique for objects that have &amp;#8220;model&amp;#8221; characteristics by expecting them to &lt;code&gt;respond_to?(:to_model)&lt;/code&gt;. This allows us to support any object in relevant contexts, provided those objects can supply us with a &amp;#8220;model&amp;#8221; representation of themselves.&lt;/p&gt;
&lt;h2&gt;3. Awesome Modules&lt;/h2&gt;
&lt;p&gt;Ruby provides a language feature similar to &amp;#8220;traits&amp;#8221; in Scala, Squeak, and Perl. Effectively, Ruby modules allow the dynamic addition of new elements of the class hierarchy at runtime. The use of &lt;code&gt;super&lt;/code&gt; is dynamically evaluated at runtime to take into consideration any modules that might have been added, making it easy to extend functionality on a superclass as many times as desired, without being forced to decide where super will land at class declaration time.&lt;/p&gt;
&lt;p&gt;Additionally, Ruby modules provide the lifecycle hooks &lt;code&gt;append_features&lt;/code&gt; and &lt;code&gt;included&lt;/code&gt;, which make it possible to use modules robustly to isolate extensions from one another and to dynamically extend classes on the basis of feature inclusion.&lt;/p&gt;
&lt;h2&gt;4. Class Bodies Aren&amp;#8217;t Special&lt;/h2&gt;
&lt;p&gt;In Ruby, class bodies aren&amp;#8217;t a special context. They&amp;#8217;re simply a context where &lt;code&gt;self&lt;/code&gt; points at the class object. If you&amp;#8217;ve used Rails, you&amp;#8217;ve probably seen code like this:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;class&lt;/span&gt; Comment &lt;span&gt;&amp;lt;&lt;/span&gt; &lt;span&gt;ActiveRecord::Base&lt;/span&gt;
  validates_presence_of &lt;span&gt;:post_id&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;It may look like &lt;code&gt;validates_presence_of&lt;/code&gt; is a language feature, but it&amp;#8217;s actually a method being called on &lt;code&gt;Comment&lt;/code&gt; that is provided by ActiveRecord::Base.&lt;/p&gt;
&lt;p&gt;That method can execute arbitrary code, also in the context of the class, including creating new methods, executing other pieces of code, or updating a class instance variable. Unlike Java annotations, which must be run at compile-time, Ruby class bodies can take runtime information into consideration, such as dynamically supplied options or the results of evaluating other code.&lt;/p&gt;
&lt;h2&gt;5. String Eval&lt;/h2&gt;
&lt;p&gt;This is likely a heresy. I&amp;#8217;m not referring to arbitrary runtime String eval here, but rather String eval that is used to create methods early in the boot process of a Ruby application.&lt;/p&gt;
&lt;p&gt;This can make it possible to take Ruby-defined structures, like Rails routes or AOP-definitions, and compile them into Ruby methods. Of course, it is possible to implement these things as add-ons to other languages, but Ruby makes it possible to implement these sorts of things in pure Ruby. It is, to a large degree, a self-hosting language.&lt;/p&gt;
&lt;h2&gt;6. Blocks and Lambdas&lt;/h2&gt;
&lt;p&gt;I&amp;#8217;ve said this a few times and I&amp;#8217;ll repeat myself: I don&amp;#8217;t consider languages without anonymous lambdas to be powerful enough for me to use day-to-day. These constructs are actually extremely common, and found in languages as diverse as Ruby, JavaScript, Scala, Clojure, and of course Lisp.&lt;/p&gt;
&lt;p&gt;They make it possible to implement block-scoped constructs that look like language features. The most common example usage is for File operations. In languages without lambdas, users are forced to use an inline &amp;#8220;ensure&amp;#8221; block every in the same lexical scope that they originally opened the file in, to ensure that the resource is closed.&lt;/p&gt;
&lt;p&gt;In Java:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;java&quot;&gt;&lt;span&gt;static&lt;/span&gt; &lt;span&gt;void&lt;/span&gt; run&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;String&lt;/span&gt; in&lt;span&gt;&amp;#41;&lt;/span&gt; 
&lt;span&gt;throws&lt;/span&gt; &lt;span&gt;FileNotFoundException&lt;/span&gt; &lt;span&gt;&amp;#123;&lt;/span&gt;
  &lt;span&gt;File&lt;/span&gt; input &lt;span&gt;=&lt;/span&gt; &lt;span&gt;new&lt;/span&gt; &lt;span&gt;File&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;in&lt;span&gt;&amp;#41;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;
  &lt;span&gt;String&lt;/span&gt; line&lt;span&gt;;&lt;/span&gt; Scanner reader &lt;span&gt;=&lt;/span&gt; &lt;span&gt;null&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;
  &lt;span&gt;try&lt;/span&gt; &lt;span&gt;&amp;#123;&lt;/span&gt;
    reader &lt;span&gt;=&lt;/span&gt; &lt;span&gt;new&lt;/span&gt; Scanner&lt;span&gt;&amp;#40;&lt;/span&gt;input&lt;span&gt;&amp;#41;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;
    &lt;span&gt;while&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;reader.&lt;span&gt;hasNextLine&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt; &lt;span&gt;&amp;#123;&lt;/span&gt;
      &lt;span&gt;System&lt;/span&gt;.&lt;span&gt;out&lt;/span&gt;.&lt;span&gt;println&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;reader.&lt;span&gt;nextLine&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;
    &lt;span&gt;&amp;#125;&lt;/span&gt;
  &lt;span&gt;&amp;#125;&lt;/span&gt; &lt;span&gt;finally&lt;/span&gt; &lt;span&gt;&amp;#123;&lt;/span&gt; reader.&lt;span&gt;close&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt; &lt;span&gt;&amp;#125;&lt;/span&gt;
&lt;span&gt;&amp;#125;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Among other things, the Java version needs to wrap the creation of the Scanner in a &lt;code&gt;try&lt;/code&gt; block so it can be guaranteed to be closed. In contrast, the Ruby version:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;def&lt;/span&gt; run&lt;span&gt;&amp;#40;&lt;/span&gt;input&lt;span&gt;&amp;#41;&lt;/span&gt;
  &lt;span&gt;File&lt;/span&gt;.&lt;span&gt;open&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;input, &lt;span&gt;&amp;quot;r&amp;quot;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt; &lt;span&gt;do&lt;/span&gt; &lt;span&gt;|&lt;/span&gt;f&lt;span&gt;|&lt;/span&gt;
    f.&lt;span&gt;each_line&lt;/span&gt; &lt;span&gt;&amp;#123;&lt;/span&gt;&lt;span&gt;|&lt;/span&gt;line&lt;span&gt;|&lt;/span&gt; &lt;span&gt;puts&lt;/span&gt; line &lt;span&gt;&amp;#125;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Because of the existence of blocks, it is possible to abstract away the need to close the File in a single location, minimizing programmer error and reducing duplication.&lt;/p&gt;
&lt;h2&gt;7. Combo Attack: Self-Hosting Language&lt;/h2&gt;
&lt;p&gt;The combination of several of the above features produce real-life examples of ways that we can &amp;#8220;extend&amp;#8221; the Ruby language in Rails. Consider the following:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;  respond_to &lt;span&gt;do&lt;/span&gt; &lt;span&gt;|&lt;/span&gt;format&lt;span&gt;|&lt;/span&gt;
    &lt;span&gt;if&lt;/span&gt; &lt;span&gt;@user&lt;/span&gt;.&lt;span&gt;save&lt;/span&gt;
      flash&lt;span&gt;&amp;#91;&lt;/span&gt;&lt;span&gt;:notice&lt;/span&gt;&lt;span&gt;&amp;#93;&lt;/span&gt; = &lt;span&gt;'User was successfully created.'&lt;/span&gt;
      &lt;span&gt;format&lt;/span&gt;.&lt;span&gt;html&lt;/span&gt; &lt;span&gt;&amp;#123;&lt;/span&gt; redirect_to&lt;span&gt;&amp;#40;&lt;/span&gt;@user&lt;span&gt;&amp;#41;&lt;/span&gt; &lt;span&gt;&amp;#125;&lt;/span&gt;
      &lt;span&gt;format&lt;/span&gt;.&lt;span&gt;xml&lt;/span&gt; &lt;span&gt;&amp;#123;&lt;/span&gt; render &lt;span&gt;:xml&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; &lt;span&gt;@user&lt;/span&gt;, &lt;span&gt;:status&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt;ted, &lt;span&gt;:location&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; &lt;span&gt;@user&lt;/span&gt; &lt;span&gt;&amp;#125;&lt;/span&gt;
    &lt;span&gt;else&lt;/span&gt;
      &lt;span&gt;format&lt;/span&gt;.&lt;span&gt;html&lt;/span&gt; &lt;span&gt;&amp;#123;&lt;/span&gt; render &lt;span&gt;:action&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; &lt;span&gt;&amp;quot;new&amp;quot;&lt;/span&gt; &lt;span&gt;&amp;#125;&lt;/span&gt;
      &lt;span&gt;format&lt;/span&gt;.&lt;span&gt;xml&lt;/span&gt; &lt;span&gt;&amp;#123;&lt;/span&gt; render &lt;span&gt;:xml&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; &lt;span&gt;@user&lt;/span&gt;.&lt;span&gt;errors&lt;/span&gt;, &lt;span&gt;:status&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; &lt;span&gt;:unprocessable_entity&lt;/span&gt; &lt;span&gt;&amp;#125;&lt;/span&gt;
    &lt;span&gt;end&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In this case, we&amp;#8217;re able to seamlessly mix methods (&lt;code&gt;respond_to&lt;/code&gt;) with normal Ruby code (&lt;code&gt;if&lt;/code&gt; and &lt;code&gt;else&lt;/code&gt;) to produce a new block-scoped construct. Ruby&amp;#8217;s semantics for blocks allow us to &lt;code&gt;return&lt;/code&gt; or &lt;code&gt;yield&lt;/code&gt; from inside the block, further blending the boundaries of the code-block and language constructs like &lt;code&gt;if&lt;/code&gt; or &lt;code&gt;while&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;In Rails 3, we introduced:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;class&lt;/span&gt; PeopleController &lt;span&gt;&amp;lt;&lt;/span&gt; ApplicationController
  respond_to &lt;span&gt;:html&lt;/span&gt;, &lt;span&gt;:xml&lt;/span&gt;, &lt;span&gt;:json&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;def&lt;/span&gt; index
    &lt;span&gt;@people&lt;/span&gt; = Person.&lt;span&gt;find&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;:all&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;
    respond_with&lt;span&gt;&amp;#40;&lt;/span&gt;@people&lt;span&gt;&amp;#41;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Here, respond_to is provided at the class-level. It tells Rails that respond_with (in &lt;code&gt;index&lt;/code&gt;) should accept HTML, XML, or JSON as response formats. If the user asked for a different format, we automatically return a 406 error (Not Acceptable).&lt;/p&gt;
&lt;p&gt;If you dig in a bit deeper, you can see that the respond_to method is defined as:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;def&lt;/span&gt; respond_to&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;*&lt;/span&gt;mimes&lt;span&gt;&amp;#41;&lt;/span&gt;
  options = mimes.&lt;span&gt;extract_options&lt;/span&gt;!
&amp;nbsp;
  only_actions   = &lt;span&gt;Array&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;options.&lt;span&gt;delete&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;:only&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;
  except_actions = &lt;span&gt;Array&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;options.&lt;span&gt;delete&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;:except&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;
&amp;nbsp;
  mimes.&lt;span&gt;each&lt;/span&gt; &lt;span&gt;do&lt;/span&gt; &lt;span&gt;|&lt;/span&gt;mime&lt;span&gt;|&lt;/span&gt;
    mime = mime.&lt;span&gt;to_sym&lt;/span&gt;
    mimes_for_respond_to&lt;span&gt;&amp;#91;&lt;/span&gt;mime&lt;span&gt;&amp;#93;&lt;/span&gt;          = &lt;span&gt;&amp;#123;&lt;/span&gt;&lt;span&gt;&amp;#125;&lt;/span&gt;
    mimes_for_respond_to&lt;span&gt;&amp;#91;&lt;/span&gt;mime&lt;span&gt;&amp;#93;&lt;/span&gt;&lt;span&gt;&amp;#91;&lt;/span&gt;&lt;span&gt;:only&lt;/span&gt;&lt;span&gt;&amp;#93;&lt;/span&gt;   = only_actions   &lt;span&gt;unless&lt;/span&gt; only_actions.&lt;span&gt;empty&lt;/span&gt;?
    mimes_for_respond_to&lt;span&gt;&amp;#91;&lt;/span&gt;mime&lt;span&gt;&amp;#93;&lt;/span&gt;&lt;span&gt;&amp;#91;&lt;/span&gt;&lt;span&gt;:except&lt;/span&gt;&lt;span&gt;&amp;#93;&lt;/span&gt; = except_actions &lt;span&gt;unless&lt;/span&gt; except_actions.&lt;span&gt;empty&lt;/span&gt;?
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This method is defined on the ActionController::MimeResponds::ClassMethods module, which is pulled into ActionController::Base. Additionally, mimes_for_respond_to is defined using &lt;code&gt;class_inheritable_reader&lt;/code&gt; in the &lt;code&gt;included&lt;/code&gt; lifecycle hook for the module. The class_inheritable_reader method (macro?) uses &lt;code&gt;class_eval&lt;/code&gt; to add methods onto the class in question to emulate the built-in &lt;code&gt;attr_accessor&lt;/code&gt; functionality.&lt;/p&gt;
&lt;p&gt;Understanding all of the details isn&amp;#8217;t important. What&amp;#8217;s important is that using the Ruby features we described above, it&amp;#8217;s possible to create layers of abstraction that can appear to add features to the Ruby language. &lt;/p&gt;
&lt;p&gt;A developer looking at &lt;code&gt;ActionController::MimeResponds&lt;/code&gt; need not understand how &lt;code&gt;class_inheritable_reader&lt;/code&gt; works&amp;#8211;he just needs to understand the basic functionality. And a developer looking at the API documentation need not understand how the class-level &lt;code&gt;respond_to&lt;/code&gt; is implemented&amp;#8211;she just needs to understand the provided functionality. With that said, peeling back each layer leads to simple abstractions that build on other abstractions. There&amp;#8217;s no need to peel back the whole curtain at once.&lt;/p&gt;
&lt;h2&gt;8. Nice Literals&lt;/h2&gt;
&lt;p&gt;I often forget about this when programming in Ruby, only to crash back down to earth when using a language with fewer, less expressive literals.&lt;/p&gt;
&lt;p&gt;Ruby has literals for just about everything:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Strings: single-line, double-line, interpolated&lt;/li&gt;
&lt;li&gt;Numbers: binary, octal, decimal, hex&lt;/li&gt;
&lt;li&gt;Null: nil&lt;/li&gt;
&lt;li&gt;Boolean: true, false&lt;/li&gt;
&lt;li&gt;Arrays: [1,2], %w(each word is element)&lt;/li&gt;
&lt;li&gt;Hashes: {key =&gt; value} and in Ruby 1.9 {key: value}&lt;/li&gt;
&lt;li&gt;Regular expressions: /hello/, %r{hello/path}, %r{hello#{interpolated}}&lt;/li&gt;
&lt;li&gt;Symbols: :name and :&amp;#8221;weird string&amp;#8221;&lt;/li&gt;
&lt;li&gt;Block: { block literal }&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;And I think I&amp;#8217;m missing some. While it may seem academic, good, readable literals can increase the programmer&amp;#8217;s ability to write short but extremely expressive code. It&amp;#8217;s of course possible to achieve the same sorts of things as you can with literal Hashes by instantiating a new Hash object and pushing the keys and values on one at a time, but it reduces their utility as method parameters, for instance.&lt;/p&gt;
&lt;p&gt;The terseness of the Hash literal has allowed Ruby programmers to effectively add a limited keyword argument feature to the language without having to get approval by the language designers. Yet another small example of self-hosting.&lt;/p&gt;
&lt;h2&gt;9. Everything is an Object, and All Code is Executed and Has a &lt;code&gt;self&lt;/code&gt;&lt;/h2&gt;
&lt;p&gt;I showed this to some degree earlier, but a lot of the reason that Class bodies work the way they do is a consequence of the unfailing object orientation of the Ruby language. Inside of a class body, Ruby is simply executing code with a &lt;code&gt;self&lt;/code&gt; pointing at the class. Additionally, nothing is special about the class context; it is possible to evaluate code in a class&amp;#8217; context from any location. Consider:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;module&lt;/span&gt; Util
  &lt;span&gt;def&lt;/span&gt; &lt;span&gt;self&lt;/span&gt;.&lt;span&gt;evaluate&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;klass&lt;span&gt;&amp;#41;&lt;/span&gt;
    klass.&lt;span&gt;class_eval&lt;/span&gt; &lt;span&gt;do&lt;/span&gt;
      &lt;span&gt;def&lt;/span&gt; hello
        &lt;span&gt;puts&lt;/span&gt; &lt;span&gt;&amp;quot;#{self} says Hello!&amp;quot;&lt;/span&gt; 
      &lt;span&gt;end&lt;/span&gt;
    &lt;span&gt;end&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
&lt;span&gt;class&lt;/span&gt; PersonName &lt;span&gt;&amp;lt;&lt;/span&gt; &lt;span&gt;String&lt;/span&gt;
  Util.&lt;span&gt;evaluate&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;self&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This is exactly equivalent to:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;class&lt;/span&gt; PersonName &lt;span&gt;&amp;lt;&lt;/span&gt; &lt;span&gt;String&lt;/span&gt;
  &lt;span&gt;def&lt;/span&gt; hello
    &lt;span&gt;puts&lt;/span&gt; &lt;span&gt;&amp;quot;#{self} says Hello!&amp;quot;&lt;/span&gt; 
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;By removing the artificial boundaries between code in different locations, Ruby reduces the conceptual overhead of creating abstractions. And this is the result of a strong, consistent object model.&lt;/p&gt;
&lt;p&gt;One more example on this topic. This idiom is quite common in Ruby: &lt;code&gt;possibly_nil &amp;amp;&amp;amp; possibly_nil.method_name&lt;/code&gt;. Since nil is just an object in Ruby, sending it a message it does not understand will result in a &lt;code&gt;NoMethodError&lt;/code&gt;. Some developers suggested the following syntax: &lt;code&gt;possibly_nil.try(:method_name)&lt;/code&gt;. This can be implemented in Ruby as follows:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;class&lt;/span&gt; &lt;span&gt;Object&lt;/span&gt;
  alias_method &lt;span&gt;:try&lt;/span&gt;, &lt;span&gt;:__send__&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
&lt;span&gt;class&lt;/span&gt; &lt;span&gt;NilClass&lt;/span&gt;
  &lt;span&gt;def&lt;/span&gt; try
    &lt;span&gt;nil&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Essentially, this adds the method &lt;code&gt;try&lt;/code&gt; to every Object. When the Object is &lt;code&gt;nil&lt;/code&gt;, try simply returns &lt;code&gt;nil&lt;/code&gt;. When the object is not &lt;code&gt;nil&lt;/code&gt;, &lt;code&gt;try&lt;/code&gt; just calls the method in question.&lt;/p&gt;
&lt;p&gt;Using targeted application of Ruby&amp;#8217;s open classes, combined with the fact that everything in Ruby, including &lt;code&gt;nil&lt;/code&gt;, is an object, we were able to create a new Ruby feature. Again, this isn&amp;#8217;t such a big deal, but it&amp;#8217;s another case where the right choices in the language can allow us to create useful abstractions.&lt;/p&gt;
&lt;h2&gt;10. Rack&lt;/h2&gt;
&lt;p&gt;I&amp;#8217;m going to cheat a little bit since Rack isn&amp;#8217;t part of the Ruby language, but it does demonstrate some useful things about it. First of all, the Rack library only hit 1.0 earlier this year, and already every single Ruby web framework is Rack compliant. If you use a Ruby framework, you can be guaranteed that it uses Rack, and any standard Rack middleware will work.&lt;/p&gt;
&lt;p&gt;This was all done without any backward compatibility sacrifices, a tribute to the flexibility of the Ruby language.&lt;/p&gt;
&lt;p&gt;Rack itself also leverages Ruby features to do its work. The Rack API looks like this:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;Rack::Builder&lt;/span&gt;.&lt;span&gt;new&lt;/span&gt; &lt;span&gt;do&lt;/span&gt;
  use &lt;span&gt;Some::Middleware&lt;/span&gt;, param
  use &lt;span&gt;Some::Other::Middleware&lt;/span&gt;
  run Application
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In this brief code snippet, a number of things are at work. First, a block is passed to Rack::Builder. Second, that block is evaluated in the context of a new instance of Rack::Builder, which gives it access to the &lt;code&gt;use&lt;/code&gt; and &lt;code&gt;run&lt;/code&gt; methods. Third, the parameter passed to use and run is a class literal, which in Ruby is a simple object. This allows Rack to call &lt;code&gt;passed_in_middleware.new(app, param)&lt;/code&gt;, where new is just a method call on the Class object &lt;code&gt;Some::Middleware&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;And in case you think the code to implement that would be heinous, here it is:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;class&lt;/span&gt; &lt;span&gt;Rack::Builder&lt;/span&gt;
  &lt;span&gt;def&lt;/span&gt; initialize&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;block&lt;span&gt;&amp;#41;&lt;/span&gt;
    &lt;span&gt;@ins&lt;/span&gt; = &lt;span&gt;&amp;#91;&lt;/span&gt;&lt;span&gt;&amp;#93;&lt;/span&gt;
    instance_eval&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;block&lt;span&gt;&amp;#41;&lt;/span&gt; &lt;span&gt;if&lt;/span&gt; block_given?
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;def&lt;/span&gt; use&lt;span&gt;&amp;#40;&lt;/span&gt;middleware, &lt;span&gt;*&lt;/span&gt;args, &lt;span&gt;&amp;amp;&lt;/span&gt;block&lt;span&gt;&amp;#41;&lt;/span&gt;
    &lt;span&gt;@ins&lt;/span&gt; &lt;span&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span&gt;lambda&lt;/span&gt; &lt;span&gt;&amp;#123;&lt;/span&gt; &lt;span&gt;|&lt;/span&gt;app&lt;span&gt;|&lt;/span&gt; middleware.&lt;span&gt;new&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;app, &lt;span&gt;*&lt;/span&gt;args, &lt;span&gt;&amp;amp;&lt;/span&gt;block&lt;span&gt;&amp;#41;&lt;/span&gt; &lt;span&gt;&amp;#125;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;def&lt;/span&gt; run&lt;span&gt;&amp;#40;&lt;/span&gt;app&lt;span&gt;&amp;#41;&lt;/span&gt;
    &lt;span&gt;@ins&lt;/span&gt; &lt;span&gt;&amp;lt;&amp;lt;&lt;/span&gt; app &lt;span&gt;#lambda { |nothing| app }&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This is all that&amp;#8217;s needed to implement the code I showed above the creates a new Rack application. Instantiating the middleware chain is a simple affair as well:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;def&lt;/span&gt; to_app
  inner_app = &lt;span&gt;@ins&lt;/span&gt;.&lt;span&gt;last&lt;/span&gt;
  &lt;span&gt;@ins&lt;/span&gt;&lt;span&gt;&amp;#91;&lt;/span&gt;0...&lt;span&gt;-&lt;/span&gt;&lt;span&gt;1&lt;/span&gt;&lt;span&gt;&amp;#93;&lt;/span&gt;.&lt;span&gt;reverse_each&lt;/span&gt; &lt;span&gt;&amp;#123;&lt;/span&gt; &lt;span&gt;|&lt;/span&gt;app&lt;span&gt;|&lt;/span&gt; inner_app = app.&lt;span&gt;call&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;inner_app&lt;span&gt;&amp;#41;&lt;/span&gt; &lt;span&gt;&amp;#125;&lt;/span&gt;
  inner_app
&lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
&lt;span&gt;def&lt;/span&gt; call&lt;span&gt;&amp;#40;&lt;/span&gt;env&lt;span&gt;&amp;#41;&lt;/span&gt;
  to_app.&lt;span&gt;call&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;env&lt;span&gt;&amp;#41;&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;First, we take the last element from the chain, which is our endpoint. We then loop over the remaining elements in reverse, instantiating each middleware with the next element in the chain, and return the resulting object.&lt;/p&gt;
&lt;p&gt;Finally, we define a call method on the Builder, which is required by the Rack specifically, that calls to_app and passes the environment through, kicking off the chain.&lt;/p&gt;
&lt;p&gt;Through the use of a number of the techniques described in the post, we were able to create a Rack-compliant application that supports Rack middleware in under two dozen lines of code.&lt;/p&gt;

&lt;div class=&quot;feedflare&quot;&gt;
&lt;a href=&quot;http://feeds.feedburner.com/~ff/KatzGotYourTongue?a=SWU31FCevnc:1WnsvTF3nds:yIl2AUoC8zA&quot;&gt;&lt;img src=&quot;http://feeds.feedburner.com/~ff/KatzGotYourTongue?d=yIl2AUoC8zA&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src=&quot;http://feeds.feedburner.com/~r/KatzGotYourTongue/~4/SWU31FCevnc&quot; height=&quot;1&quot; width=&quot;1&quot; /&gt;</content>
		<author>
			<name>wycats</name>
			<uri>http://yehudakatz.com</uri>
		</author>
		<source>
			<title type="html">Katz Got Your Tongue?</title>
			<subtitle type="html">Random Geek-Related Thoughts</subtitle>
			<link rel="self" href="http://yehudakatz.com/feed/atom/"/>
			<id>http://yehudakatz.com/feed/atom/</id>
			<updated>2010-03-13T08:20:07+00:00</updated>
		</source>
	</entry>

	<entry xml:lang="en">
		<title type="html">Ruby, Rack and CouchDB = lots of awesomeness</title>
		<link href="http://merbist.com/2009/07/27/ruby-rack-and-couchdb-lots-of-awesomeness/"/>
		<id>http://merbist.com/?p=536</id>
		<updated>2009-07-27T21:49:20+00:00</updated>
		<content type="html">&lt;p&gt;Over the weekend, I spent some time working on a Ruby + Rack +CouchDB project. Three technologies that I know quite well but that I never put to work together at the same time, at least not directly.  Let&amp;#8217;s call this Part I.&lt;/p&gt;
&lt;p&gt;Before we get started, let me introduce each component:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a id=&quot;aptureLink_uhrplaQzyi&quot; href=&quot;http://en.wikipedia.org/wiki/Ruby%20%28programming%20language%29&quot;&gt;Ruby&lt;/a&gt; : if you are reading this blog, you more than likely know at least a little bit about, what I consider, one of the most enjoyable programming language out there. It&amp;#8217;s also a very flexible language that lets us do some interesting things. I could have chosen Python to do the same project but that&amp;#8217;s a whole different topic. For this project we will do something Ruby excels at: reopening existing classes and injecting more code.&lt;/li&gt;
&lt;li&gt;&lt;a id=&quot;aptureLink_2DDeKXxZsP&quot; href=&quot;http://rack.rubyforge.org/&quot;&gt;Rack&lt;/a&gt;: a webserver interface written in Ruby and inspired by &lt;a id=&quot;aptureLink_Jchqx9HIiw&quot; href=&quot;http://www.wsgi.org/wsgi/&quot;&gt;Python&amp;#8217;s WSGI&lt;/a&gt;. Basically, it&amp;#8217;s a defined API to interact between webservers and web frameworks. &lt;span&gt;It&amp;#8217;s used by most common Ruby web frameworks, from Sinatra to Rails&lt;/span&gt; (btw, Rails3 is going to be even more Rack-focused than it already is). So, very simply put, the webserver receives a request, passes it to Rack, that converts it, passes it to your web framework and the web framework sends a response in the expected format (more on Rack later).&lt;/li&gt;
&lt;li&gt;&lt;a id=&quot;aptureLink_LN4oL6D0oH&quot; href=&quot;http://couchdb.apache.org/&quot;&gt;CouchDB&lt;/a&gt;: Apache&amp;#8217;s document-oriented database. RESTful API, schema-less, written in Erlang with built-in support for map/reduce. For this project, I&amp;#8217;m using &lt;a id=&quot;aptureLink_tlTW2NkUta&quot; href=&quot;http://github.com/mattetti/couchrest&quot;&gt;CouchRest&lt;/a&gt;, a Ruby wrapper for Couch.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Goal: Log Couch requests and analyze data&lt;/h2&gt;
&lt;p&gt;Let&amp;#8217;s say we have a Rails, Sinatra or Merb application and we are using CouchRest (maybe we are using CouchRest and ActiveRecord, but let&amp;#8217;s ignore that for now).&lt;/p&gt;
&lt;p&gt;Everything works fine but we would like to profile our app a little and maybe optimize the DB usage. The default framework loggers don&amp;#8217;t support Couch. The easy way would be to tail the Couch logs or look at the logs in &lt;a id=&quot;aptureLink_W2ygfTNmX0&quot; href=&quot;http://janl.github.com/couchdbx/&quot;&gt;CouchDBX&lt;/a&gt;. Now, while that works, we can&amp;#8217;t really see what DB calls are made per action, so it makes any optimization work a bit tedious. (Note that Rails3 will have some better conventions for logging, making things even easier)&lt;/p&gt;
&lt;p&gt;So, let&amp;#8217;s see how to fix that. Let&amp;#8217;s start by looking at Rack.&lt;/p&gt;
&lt;h2&gt;Rack Middleware&lt;/h2&gt;
&lt;p&gt;Instead of hacking a web framework specific solution, let&amp;#8217;s use Rack. Rack is dead simple, you just need to write a class that has a &lt;em&gt;call&lt;/em&gt; method.&lt;br /&gt;
 In our case, we don&amp;#8217;t care about modifying the response, we just want to instrument our app. We just want our middleware to be transparent and let our webserver deal with it normally.    &lt;/p&gt;
&lt;p&gt;Here we go &amp;#8230; that wasn&amp;#8217;t hard, was it? We keep the application reference in the @app variable when a new instance of the middleware is created. Then when the middleware is called, we just call the rest of the chain and pretend nothing happened.&lt;/p&gt;
&lt;p&gt; As you can see, we just added some logging info around the request. Let&amp;#8217;s do one better and save the logs in CouchDB:  &lt;/p&gt;
&lt;p&gt;Again, nothing complicated. In our rackup file we defined which Couch database to use and we passed it to our middleware (we change our initialize method signature to take the DB).&lt;br /&gt;
Finally, instead of printing out the logs, we are saving them to the database.&lt;/p&gt;
&lt;p&gt;W00t! At this point all our requests have been saved in the DB with all the data there, ready to be manipulated by some map/reduce views we will write. For the record, you might want to use the bulk_save approach in CouchDB which will wait for X amount of records to save them in the DB all at once. Couch also let&amp;#8217;s you send new documents, but only save it to the DB every X documents or X seconds.&lt;/p&gt;
&lt;p&gt;&lt;img class=&quot;alignnone&quot; title=&quot;log document&quot; src=&quot;http://img.skitch.com/20090726-ebmpgjtrc6x8239ia69kmri1rt.jpg&quot; alt=&quot;&quot; width=&quot;715&quot; height=&quot;505&quot; /&gt;&lt;/p&gt;
&lt;p&gt;As you can see, our document contains the timestamps and the full environment as a hash.&lt;/p&gt;
&lt;p&gt;All of that is nice, but even though we get a lot of information, we could not actually see any of the DB calls made in each request. Let&amp;#8217;s fix that and inject our logger in CouchRest (you could apply the same approach to any adapter).&lt;/p&gt;
&lt;p&gt;Let&amp;#8217;s reopen the HTTP Abstraction layer class used by CouchRest and inject some instrumentation:&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Again, nothing fancy, we are just opening the module, reopening the methods and wrapping our code around the &lt;em&gt;super&lt;/em&gt; call (for those who don&amp;#8217;t know, &lt;em&gt;super&lt;/em&gt; calls the original method).&lt;/p&gt;
&lt;p&gt;This is all for Part I. In Part II, we&amp;#8217;ll see how to process the logs and make all that data useful.&lt;/p&gt;
&lt;p&gt;By the way, if you make it to &lt;a id=&quot;aptureLink_aF4OSFnjjA&quot; href=&quot;http://www.railssummit.com.br/&quot;&gt;RailsSummit&lt;/a&gt;, I will be giving a talk on Rails3 and the new exciting stuff you will be able to do including Rack based stuff, CouchDB, MongoDB, new DataMapper etc..&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://railssummit.com.br/&quot;&gt;&lt;img class=&quot;aligncenter&quot; title=&quot;RailsSummit 2009&quot; src=&quot;http://railssummit.com.br/images/banners/en_souPalestrante_210x60.jpg&quot; alt=&quot;&quot; width=&quot;210&quot; height=&quot;60&quot; /&gt;&lt;/a&gt;&lt;/p&gt;</content>
		<author>
			<name>Matt Aimonetti (mattetti)</name>
			<uri>http://merbist.com</uri>
		</author>
		<source>
			<title type="html">Merbist</title>
			<subtitle type="html">Rails 3, Ruby &amp;amp; MacRuby news - tutorials - consulting - training</subtitle>
			<link rel="self" href="http://merbist.com/feed/"/>
			<id>http://merbist.com/feed/</id>
			<updated>2010-03-13T12:20:05+00:00</updated>
		</source>
	</entry>

	<entry xml:lang="en">
		<title type="html">Rubygems Good Practice</title>
		<link href="http://feedproxy.google.com/~r/KatzGotYourTongue/~3/dDdH4ZKpX6w/"/>
		<id>http://yehudakatz.com/?p=307</id>
		<updated>2009-07-24T14:40:49+00:00</updated>
		<content type="html">&lt;p&gt;Rubygems provides two things for the Ruby community.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;A remote repository/packaging format and installer&lt;/li&gt;
&lt;li&gt;A runtime dependency manager&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The key to good rubygems practice is to treat these two elements of Rubygems as separate from each other. Someone might use the Rubygems packaging format and the Rubygems distribution but not want to use the Rubygems runtime.&lt;/p&gt;
&lt;p&gt;And why should they? The Rubygems runtime is mainly responsible for setting up the appropriate load-paths, and if you are able to get the load paths set up correctly, why should you care about Rubygems at all?&lt;/p&gt;
&lt;p&gt;In other words, you should write your libraries so that their only requirement is being in the load path. Users might then use Rubygems to get your library in the load path, or they might check it out of git and add it themselves.&lt;/p&gt;
&lt;p&gt;It sounds pretty straight-forward but there are a few common pitfalls:&lt;/p&gt;
&lt;h2&gt;Using &lt;code&gt;gem&lt;/code&gt; inside your gems&lt;/h2&gt;
&lt;p&gt;It&amp;#8217;s reasonably common to see code like this inside of a gem:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;gem &lt;span&gt;&amp;quot;extlib&amp;quot;&lt;/span&gt;, &lt;span&gt;&amp;quot;&amp;gt;= 1.0.8&amp;quot;&lt;/span&gt;
&lt;span&gt;require&lt;/span&gt; &lt;span&gt;&amp;quot;extlib&amp;quot;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This should be entirely unnecessary. While using &lt;code&gt;Kernel.gem&lt;/code&gt; in an application makes perfect sense, gems themselves should use their gem specification to provide dependent versions. When used with Rubygems, Rubygems will automatically add the appropriate dependencies to the load path. When not using Rubygems, the users can add the dependencies themselves.&lt;/p&gt;
&lt;p&gt;Keep in mind that whether or not you use Rubygems, you can use &lt;code&gt;require&lt;/code&gt; and it will do the right thing. If the file is in the load path (because you put it there or because Rubygems put it there), it will just work. If it&amp;#8217;s not in the loadpath, Rubygems will look for a matching gem to add to the load path (by overriding require).&lt;/p&gt;
&lt;h2&gt;Rescuing from Gem::LoadError&lt;/h2&gt;
&lt;p&gt;This idiom is also reasonably common:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;begin&lt;/span&gt;
  gem &lt;span&gt;&amp;quot;my_gem&amp;quot;&lt;/span&gt;, &lt;span&gt;&amp;quot;&amp;gt;= 1.0.6&amp;quot;&lt;/span&gt;
  &lt;span&gt;require&lt;/span&gt; &lt;span&gt;&amp;quot;my_gem&amp;quot;&lt;/span&gt;
&lt;span&gt;rescue&lt;/span&gt; &lt;span&gt;Gem::LoadError&lt;/span&gt;
  &lt;span&gt;# handle the error somehow&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The right solution here is to avoid the &lt;code&gt;gem&lt;/code&gt; call, as I said above, and rescue from plain &lt;code&gt;LoadError&lt;/code&gt;. The Rubygems runtime sometimes raises &lt;code&gt;Gem::LoadError&lt;/code&gt;, but that inherits from regular &lt;code&gt;LoadError&lt;/code&gt;, so you&amp;#8217;re free to rescue from that and catch cases with and without the rubygems runtime.&lt;/p&gt;
&lt;h2&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;Declare you gem version dependencies in your gem specification and use simple requires in your library. If you need to catch the case where the dependency could not be found, rescue from LoadError.&lt;/p&gt;
&lt;p&gt;And that&amp;#8217;s all there is to it. Your library will work fine with or without the Rubygems runtime :)&lt;/p&gt;

&lt;div class=&quot;feedflare&quot;&gt;
&lt;a href=&quot;http://feeds.feedburner.com/~ff/KatzGotYourTongue?a=dDdH4ZKpX6w:CxVoLkE_DTc:yIl2AUoC8zA&quot;&gt;&lt;img src=&quot;http://feeds.feedburner.com/~ff/KatzGotYourTongue?d=yIl2AUoC8zA&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src=&quot;http://feeds.feedburner.com/~r/KatzGotYourTongue/~4/dDdH4ZKpX6w&quot; height=&quot;1&quot; width=&quot;1&quot; /&gt;</content>
		<author>
			<name>wycats</name>
			<uri>http://yehudakatz.com</uri>
		</author>
		<source>
			<title type="html">Katz Got Your Tongue?</title>
			<subtitle type="html">Random Geek-Related Thoughts</subtitle>
			<link rel="self" href="http://yehudakatz.com/feed/atom/"/>
			<id>http://yehudakatz.com/feed/atom/</id>
			<updated>2010-03-13T08:20:07+00:00</updated>
		</source>
	</entry>

	<entry xml:lang="en">
		<title type="html">Rails 3: The Great Decoupling</title>
		<link href="http://yehudakatz.com/2009/07/19/rails-3-the-great-decoupling/"/>
		<id>http://yehudakatz.com/?p=300</id>
		<updated>2009-07-20T07:06:58+00:00</updated>
		<content type="html">&lt;p&gt;In working on Rails 3 over the past 6 months, I have focsed rather extensively on decoupling components from each other.&lt;/p&gt;
&lt;p&gt;Why should ActionController care whether it&amp;#8217;s talking to ActionView or just something that duck-types like ActionView? Of course, the key to making this work well is to keep the interfaces between components as small as possible, so that implementing an ActionView lookalike is a matter of implementing just a few methods, not dozens.&lt;/p&gt;
&lt;p&gt;While I was preparing for my talk at &lt;a href=&quot;http://rubykaigi.org/&quot;&gt;RubyKaigi&lt;/a&gt;, I was trying to find the smallest possible examples that demonstrate some of this stuff. It went really well, but I noticed a few areas that could be improved even further, producing an even more compelling demonstration.&lt;/p&gt;
&lt;p&gt;This weekend, I focused on cleaning up those interfaces, so we have small and clearly documented mechanisms for interfacing with Rails components. I want to focus on ActionView in this post, which I&amp;#8217;ll demonstrate with an example.&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;$:.&lt;span&gt;push&lt;/span&gt; &lt;span&gt;&amp;quot;rails/activesupport/lib&amp;quot;&lt;/span&gt;
$:.&lt;span&gt;push&lt;/span&gt; &lt;span&gt;&amp;quot;rails/actionpack/lib&amp;quot;&lt;/span&gt;
&amp;nbsp;
&lt;span&gt;require&lt;/span&gt; &lt;span&gt;&amp;quot;action_controller&amp;quot;&lt;/span&gt;
&amp;nbsp;
&lt;span&gt;class&lt;/span&gt; Kaigi &lt;span&gt;&amp;lt;&lt;/span&gt; &lt;span&gt;ActionController::Http&lt;/span&gt;
  &lt;span&gt;include&lt;/span&gt; &lt;span&gt;AbstractController::Callbacks&lt;/span&gt;
  &lt;span&gt;include&lt;/span&gt; &lt;span&gt;ActionController::RackConvenience&lt;/span&gt;
  &lt;span&gt;include&lt;/span&gt; &lt;span&gt;ActionController::Renderer&lt;/span&gt;
  &lt;span&gt;include&lt;/span&gt; &lt;span&gt;ActionController::Layouts&lt;/span&gt;
  &lt;span&gt;include&lt;/span&gt; &lt;span&gt;ActionView::Context&lt;/span&gt;
&amp;nbsp;
  before_filter &lt;span&gt;:set_name&lt;/span&gt;
  append_view_path &lt;span&gt;&amp;quot;views&amp;quot;&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;def&lt;/span&gt; _action_view
    &lt;span&gt;self&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;def&lt;/span&gt; controller
    &lt;span&gt;self&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
  DEFAULT_LAYOUT = &lt;span&gt;Object&lt;/span&gt;.&lt;span&gt;new&lt;/span&gt;.&lt;span&gt;tap&lt;/span&gt; &lt;span&gt;&amp;#123;&lt;/span&gt;&lt;span&gt;|&lt;/span&gt;l&lt;span&gt;|&lt;/span&gt; &lt;span&gt;def&lt;/span&gt; l.&lt;span&gt;render&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;*&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt; &lt;span&gt;yield&lt;/span&gt; &lt;span&gt;end&lt;/span&gt; &lt;span&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;def&lt;/span&gt; _render_template_from_controller&lt;span&gt;&amp;#40;&lt;/span&gt;template, layout = DEFAULT_LAYOUT, options = &lt;span&gt;&amp;#123;&lt;/span&gt;&lt;span&gt;&amp;#125;&lt;/span&gt;, partial = &lt;span&gt;false&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;
    ret = template.&lt;span&gt;render&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;self&lt;/span&gt;, &lt;span&gt;&amp;#123;&lt;/span&gt;&lt;span&gt;&amp;#125;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;
    layout.&lt;span&gt;render&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;self&lt;/span&gt;, &lt;span&gt;&amp;#123;&lt;/span&gt;&lt;span&gt;&amp;#125;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt; &lt;span&gt;&amp;#123;&lt;/span&gt; ret &lt;span&gt;&amp;#125;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;def&lt;/span&gt; index
    render &lt;span&gt;:template&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; &lt;span&gt;&amp;quot;template&amp;quot;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;def&lt;/span&gt; alt
    render &lt;span&gt;:template&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; &lt;span&gt;&amp;quot;template&amp;quot;&lt;/span&gt;, &lt;span&gt;:layout&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; &lt;span&gt;&amp;quot;alt&amp;quot;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
  private
  &lt;span&gt;def&lt;/span&gt; set_name
    &lt;span&gt;@name&lt;/span&gt; = params&lt;span&gt;&amp;#91;&lt;/span&gt;&lt;span&gt;:name&lt;/span&gt;&lt;span&gt;&amp;#93;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
app = &lt;span&gt;Rack::Builder&lt;/span&gt;.&lt;span&gt;new&lt;/span&gt; &lt;span&gt;do&lt;/span&gt;
  map&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;&amp;quot;/kaigi&amp;quot;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt; &lt;span&gt;&amp;#123;&lt;/span&gt;  run Kaigi.&lt;span&gt;action&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;:index&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt; &lt;span&gt;&amp;#125;&lt;/span&gt;
  map&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;&amp;quot;/kaigi/alt&amp;quot;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt; &lt;span&gt;&amp;#123;&lt;/span&gt; run Kaigi.&lt;span&gt;action&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;:alt&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt; &lt;span&gt;&amp;#125;&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;.&lt;span&gt;to_app&lt;/span&gt;
&amp;nbsp;
&lt;span&gt;Rack::Handler::Mongrel&lt;/span&gt;.&lt;span&gt;run&lt;/span&gt; app, &lt;span&gt;:Port&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; &lt;span&gt;3000&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;There&amp;#8217;s a bunch going on here, but the important thing is that you can run this file with just ruby, and it&amp;#8217;ll serve up &lt;code&gt;/kaigi&lt;/code&gt; and &lt;code&gt;/kaigi/alt&lt;/code&gt;. It will serve templates from the local &amp;#8220;/views&amp;#8221; directory, and correctly handle before filters just fine.&lt;/p&gt;
&lt;p&gt;Let&amp;#8217;s look at this a piece at a time:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;$:.&lt;span&gt;push&lt;/span&gt; &lt;span&gt;&amp;quot;rails/activesupport/lib&amp;quot;&lt;/span&gt;
$:.&lt;span&gt;push&lt;/span&gt; &lt;span&gt;&amp;quot;rails/actionpack/lib&amp;quot;&lt;/span&gt;
&amp;nbsp;
&lt;span&gt;require&lt;/span&gt; &lt;span&gt;&amp;quot;action_controller&amp;quot;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This is just boilerplace. I symlinked rails to a directory under this file and required action_controller. Note that simply requiring ActionController is extremely cheap &amp;#8212; no features have been used yet&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;class&lt;/span&gt; Kaigi &lt;span&gt;&amp;lt;&lt;/span&gt; &lt;span&gt;ActionController::Http&lt;/span&gt;
  &lt;span&gt;include&lt;/span&gt; &lt;span&gt;AbstractController::Callbacks&lt;/span&gt;
  &lt;span&gt;include&lt;/span&gt; &lt;span&gt;ActionController::RackConvenience&lt;/span&gt;
  &lt;span&gt;include&lt;/span&gt; &lt;span&gt;ActionController::Renderer&lt;/span&gt;
  &lt;span&gt;include&lt;/span&gt; &lt;span&gt;ActionController::Layouts&lt;/span&gt;
  &lt;span&gt;include&lt;/span&gt; &lt;span&gt;ActionView::Context&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;I inherited my class from ActionController::Http. I then included a number of features, include Rack convenience methods (request/response), the Renderer, and Layouts. I also made the controller itself the view context. I will discuss this more in just a moment.&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;  before_filter &lt;span&gt;:set_name&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This is the normal Rail before_filter. I didn&amp;#8217;t need to do anything else to get this functionality other than include AbstractController::Callbacks&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;  append_view_path &lt;span&gt;&amp;quot;views&amp;quot;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Because we&amp;#8217;re not in a Rails app, our view paths haven&amp;#8217;t been pre-populated. No problem: it&amp;#8217;s just a one-liner to set them ourselves.&lt;/p&gt;
&lt;p&gt;The next part is the interesting part. In Rails 3, while ActionView::Base remains the default view context, the interface between ActionController and ActionView is extremely well defined. Specifically:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A view context must include ActionView::Context. This just adds the compiled templates, so they can be called from the context&lt;/li&gt;
&lt;li&gt;A view context must provide a _render_template_from_controller method, which takes a template object, a layout, and additional options&lt;/li&gt;
&lt;li&gt;A view context may optionally also provide a _render_partial_from_controller, to handle &lt;code&gt;render :partial =&amp;gt; @some_object&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;In order to use ActionView::Helpers, a view context must have a pointer back to its original controller&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;That&amp;#8217;s it! That&amp;#8217;s the entire ActionController=&gt;ActionView interface.&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;  &lt;span&gt;def&lt;/span&gt; _action_view
    &lt;span&gt;self&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;def&lt;/span&gt; controller
    &lt;span&gt;self&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Here, we specify that the view context is just &lt;code&gt;self&lt;/code&gt;, and define &lt;code&gt;controller&lt;/code&gt;, required by view contexts. Effectively, we have merged the controller and view context (mainly just to see if it could be done ;) )&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;  DEFAULT_LAYOUT = &lt;span&gt;Object&lt;/span&gt;.&lt;span&gt;new&lt;/span&gt;.&lt;span&gt;tap&lt;/span&gt; &lt;span&gt;&amp;#123;&lt;/span&gt;&lt;span&gt;|&lt;/span&gt;l&lt;span&gt;|&lt;/span&gt; &lt;span&gt;def&lt;/span&gt; l.&lt;span&gt;render&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;*&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt; &lt;span&gt;yield&lt;/span&gt; &lt;span&gt;end&lt;/span&gt; &lt;span&gt;&amp;#125;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Next, we make a default layout. This is just a simple proc that provides a render method that yields to the block. It will simplify:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;  &lt;span&gt;def&lt;/span&gt; _render_template_from_controller&lt;span&gt;&amp;#40;&lt;/span&gt;template, layout = DEFAULT_LAYOUT, options = &lt;span&gt;&amp;#123;&lt;/span&gt;&lt;span&gt;&amp;#125;&lt;/span&gt;, partial = &lt;span&gt;false&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;
    ret = template.&lt;span&gt;render&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;self&lt;/span&gt;, &lt;span&gt;&amp;#123;&lt;/span&gt;&lt;span&gt;&amp;#125;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt;
    layout.&lt;span&gt;render&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;self&lt;/span&gt;, &lt;span&gt;&amp;#123;&lt;/span&gt;&lt;span&gt;&amp;#125;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt; &lt;span&gt;&amp;#123;&lt;/span&gt; ret &lt;span&gt;&amp;#125;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Here, we supply the required _render_template_from_controller. The template object that is passed in is a standard Rails Template which has a render method on it. That method takes the view context and any locals. For this example, we pass in &lt;code&gt;self&lt;/code&gt; as the view context, and do not provide any locals. Next, we call render on the layout, passing in the return value of template.render. The reason we created a default is to make the case of a layout identical to the case without.&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;  &lt;span&gt;def&lt;/span&gt; index
    render &lt;span&gt;:template&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; &lt;span&gt;&amp;quot;template&amp;quot;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span&gt;def&lt;/span&gt; alt
    render &lt;span&gt;:template&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; &lt;span&gt;&amp;quot;template&amp;quot;&lt;/span&gt;, &lt;span&gt;:layout&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; &lt;span&gt;&amp;quot;alt&amp;quot;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;
&amp;nbsp;
  private
  &lt;span&gt;def&lt;/span&gt; set_name
    &lt;span&gt;@name&lt;/span&gt; = params&lt;span&gt;&amp;#91;&lt;/span&gt;&lt;span&gt;:name&lt;/span&gt;&lt;span&gt;&amp;#93;&lt;/span&gt;
  &lt;span&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This is a standard Rails controller.&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;app = &lt;span&gt;Rack::Builder&lt;/span&gt;.&lt;span&gt;new&lt;/span&gt; &lt;span&gt;do&lt;/span&gt;
  map&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;&amp;quot;/kaigi&amp;quot;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt; &lt;span&gt;&amp;#123;&lt;/span&gt;  run Kaigi.&lt;span&gt;action&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;:index&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt; &lt;span&gt;&amp;#125;&lt;/span&gt;
  map&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;&amp;quot;/kaigi/alt&amp;quot;&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt; &lt;span&gt;&amp;#123;&lt;/span&gt; run Kaigi.&lt;span&gt;action&lt;/span&gt;&lt;span&gt;&amp;#40;&lt;/span&gt;&lt;span&gt;:alt&lt;/span&gt;&lt;span&gt;&amp;#41;&lt;/span&gt; &lt;span&gt;&amp;#125;&lt;/span&gt;
&lt;span&gt;end&lt;/span&gt;.&lt;span&gt;to_app&lt;/span&gt;
&amp;nbsp;
&lt;span&gt;Rack::Handler::Mongrel&lt;/span&gt;.&lt;span&gt;run&lt;/span&gt; app, &lt;span&gt;:Port&lt;/span&gt; &lt;span&gt;=&amp;gt;&lt;/span&gt; &lt;span&gt;3000&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Finally, rather than use the Rails router, we just wire the controller up directly using Rack. In Rails 3, ControllerName.action(:action_name) returns a rack-compatible endpoint, so we can wire them up directly.&lt;/p&gt;
&lt;p&gt;And that&amp;#8217;s all there is to it!&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; I&amp;#8217;m not sure if I still need to say this, but stuff like this is purely a demonstration of the power of the internals, and does &lt;strong&gt;not&lt;/strong&gt; reflect changes to the public API or the way people use Rails by default. Everyone on the Rails team is strongly committed to retaining the same excellent startup experience and set of good conventional defaults. That will not be changing in 3.0.&lt;/p&gt;</content>
		<author>
			<name>wycats</name>
			<uri>http://yehudakatz.com</uri>
		</author>
		<source>
			<title type="html">Katz Got Your Tongue?</title>
			<subtitle type="html">Random Geek-Related Thoughts</subtitle>
			<link rel="self" href="http://yehudakatz.com/feed/atom/"/>
			<id>http://yehudakatz.com/feed/atom/</id>
			<updated>2010-03-13T08:20:07+00:00</updated>
		</source>
	</entry>

	<entry>
		<title type="html">getting started with ruby 1.9</title>
		<link href="http://jackndempsey.blogspot.com/2009/07/getting-started-with-ruby-19.html"/>
		<id>tag:blogger.com,1999:blog-4467999944823309015.post-9112655588276600342</id>
		<updated>2009-07-18T14:19:19+00:00</updated>
		<content type="html">Yehuda Katz has a &lt;a href=&quot;http://yehudakatz.com/2009/07/17/what-do-we-need-to-get-on-ruby-1-9/&quot;&gt;nice post&lt;/a&gt; over at his blog talking about Ruby 1.9 and what we need to do to move our community towards it. &lt;br /&gt;&lt;br /&gt;Lots of great points are brought up, but for me, the biggest issue is just not having the time to fiddle with things, setup nice little aliases, etc. Thankfully, one of the nice guys over at &lt;a href=&quot;http://thinkrelevance.com/&quot;&gt;Relevance&lt;/a&gt; has done this for us: &lt;a href=&quot;http://github.com/spicycode/spicy-config/tree/master&quot;&gt;spicy-config&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;On a quick side note, Chad's configs also helped me get up and running with zsh, and that's been a whole other fun trip. &lt;br /&gt;&lt;br /&gt;You'll want to clone his repo and take a look in at his .zshrc file along with the ruby_installer and ruby_switcher files inside .zsh. All you have to do is source the files to get the functions defined, and run what you're interested in. I did set things up a little different given that I don't use the Leopard install of ruby...but if you're reading this, I assume you can see what you'd want to change if need be. &lt;br /&gt;&lt;br /&gt;That's about it. I can now say &quot;use_ruby_191&quot; and it quickly switches me over, and then 'use_ruby' to jump back to 1.8.6. &lt;br /&gt;&lt;br /&gt;Great stuff Chad, thanks!&lt;div class=&quot;blogger-post-footer&quot;&gt;&lt;img width=&quot;1&quot; height=&quot;1&quot; src=&quot;https://blogger.googleusercontent.com/tracker/4467999944823309015-9112655588276600342?l=jackndempsey.blogspot.com&quot; alt=&quot;&quot; /&gt;&lt;/div&gt;</content>
		<author>
			<name>Jack Dempsey</name>
			<email>noreply@blogger.com</email>
			<uri>http://jackndempsey.blogspot.com/</uri>
		</author>
		<source>
			<title type="html">class &amp;lt;&amp;lt; self</title>
			<subtitle type="html">An increasingly random set of various topics including ruby &amp;amp; rails, merb, clojure, startups...</subtitle>
			<link rel="self" href="http://jackndempsey.blogspot.com/feeds/posts/default"/>
			<id>tag:blogger.com,1999:blog-4467999944823309015</id>
			<updated>2010-02-11T20:20:07+00:00</updated>
		</source>
	</entry>

	<entry xml:lang="en">
		<title type="html">What do we need to get on Ruby 1.9?</title>
		<link href="http://yehudakatz.com/2009/07/17/what-do-we-need-to-get-on-ruby-1-9/"/>
		<id>http://yehudakatz.com/?p=298</id>
		<updated>2009-07-18T04:57:22+00:00</updated>
		<content type="html">&lt;p&gt;A year ago, I was very skeptical of Ruby 1.9. There were a lot of changes in it, and it seemed like it was going to be a mammoth job to get things running on it. The benefits did not seem to outweigh the costs of switching, especially since Ruby 1.9 was not yet adequately stable to justify the big switch.&lt;/p&gt;
&lt;p&gt;At this point, however, it seems as though Ruby 1.9 has stabilized (with 1.9.2 on the horizon), and there are some benefits that seem to obviously justify a switch (such as fast, integrated I18n, better performance in general, blocks that can have default arguments and take blocks, etc.).&lt;/p&gt;
&lt;p&gt;Perhaps more importantly though, Ruby&amp;#8217;s language implementors have shifted their focus to Ruby 1.9. It has become increasingly difficult to get enhancements in Ruby 1.8, because it is no longer trunk Ruby. Getting community momentum behind Ruby 1.9 would enable us to make productive suggestions to Matz and the other language implementors. Instead, we seem to get a new monthly patch fixing Ruby 1.8.&lt;/p&gt;
&lt;p&gt;So my question is: what do we as a community need to shift momentum to 1.9. I&amp;#8217;m don&amp;#8217;t want a generic answer, like &amp;#8220;we need to feel good about it&amp;#8221;. I&amp;#8217;m asking you what is stopping you today from using Ruby 1.9 for your next project. Is there a library that doesn&amp;#8217;t work? Is there a new language feature that causes so much disruption to your existing programming patterns to make a switch untenable?&lt;/p&gt;
&lt;p&gt;I suspect that we are all just comfortable in Ruby 1.8, but would actually be mostly fine upgrading to Ruby 1.9. I also suspect that there are small issues I&amp;#8217;m not personally aware of, but which have blocked some of you from upgrading. Rails 2.3 and 3.0 (edge) work fine on Ruby 1.9, and I&amp;#8217;d like to see what we can do to make Ruby 1.9 a good recommended option for new projects.&lt;/p&gt;
&lt;p&gt;Thoughts?&lt;/p&gt;</content>
		<author>
			<name>wycats</name>
			<uri>http://yehudakatz.com</uri>
		</author>
		<source>
			<title type="html">Katz Got Your Tongue?</title>
			<subtitle type="html">Random Geek-Related Thoughts</subtitle>
			<link rel="self" href="http://yehudakatz.com/feed/atom/"/>
			<id>http://yehudakatz.com/feed/atom/</id>
			<updated>2010-03-13T08:20:07+00:00</updated>
		</source>
	</entry>

	<entry>
		<title type="html">a couple quick git tips</title>
		<link href="http://jackndempsey.blogspot.com/2009/07/couple-quick-git-tips.html"/>
		<id>tag:blogger.com,1999:blog-4467999944823309015.post-5286312268447203234</id>
		<updated>2009-07-16T20:59:43+00:00</updated>
		<content type="html">The other day someone asked in #git how to fix something in the middle of an unsuccessful rebase. So, I thought I'd rebase some of my own code, reorder things so there must be a conflict, and then try to duplicate their question. &lt;br /&gt;&lt;br /&gt;Only problem was that there was no error, and I hadn't really paid attention to which commits I reordered. Thankfully, I got a bit of help from a gent named doener. &lt;br /&gt;&lt;br /&gt;I'm including a rather long gist showing the process. Basically, git reflog is your friend, and you should play with this at some point in a test repo like I've done here.&lt;br /&gt;&lt;br /&gt;Note: anytime you git reset --hard be very sure and confident of what you're doing. &lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;blogger-post-footer&quot;&gt;&lt;img width=&quot;1&quot; height=&quot;1&quot; src=&quot;https://blogger.googleusercontent.com/tracker/4467999944823309015-5286312268447203234?l=jackndempsey.blogspot.com&quot; alt=&quot;&quot; /&gt;&lt;/div&gt;</content>
		<author>
			<name>Jack Dempsey</name>
			<email>noreply@blogger.com</email>
			<uri>http://jackndempsey.blogspot.com/</uri>
		</author>
		<source>
			<title type="html">class &amp;lt;&amp;lt; self</title>
			<subtitle type="html">An increasingly random set of various topics including ruby &amp;amp; rails, merb, clojure, startups...</subtitle>
			<link rel="self" href="http://jackndempsey.blogspot.com/feeds/posts/default"/>
			<id>tag:blogger.com,1999:blog-4467999944823309015</id>
			<updated>2010-02-11T20:20:07+00:00</updated>
		</source>
	</entry>

	<entry xml:lang="en">
		<title type="html">Ruby authentication of CouchDB requests</title>
		<link href="http://merbist.com/2009/07/13/ruby-authentication-of-couchdb-requests/"/>
		<id>http://merbist.com/?p=526</id>
		<updated>2009-07-13T21:00:13+00:00</updated>
		<content type="html">&lt;p&gt;&lt;a id=&quot;aptureLink_Bt9pVZgkt6&quot; href=&quot;http://couchdb.apache.org/&quot;&gt;CouchDB&lt;/a&gt; is an awesome technology. I&amp;#8217;m lucky enough to work on quite a big project where we decided to switch from MySQL to Couch for various reasons.&lt;/p&gt;
&lt;p&gt;One of the many things I like with Couch is that it handles attachments and can replicate them as well as serve them for you using the &lt;a id=&quot;aptureLink_A3nCuwtmaZ&quot; href=&quot;http://en.wikipedia.org/wiki/Erlang%20%28programming%20language%29&quot;&gt;Erlang&lt;/a&gt; based builtin webserver. (you can load balance your dbs and do some other really cool stuff)&lt;/p&gt;
&lt;p&gt;Let&amp;#8217;s take a use case. Let&amp;#8217;s imagine that you have a web app with logged in users. Every user can have their own avatar.&lt;/p&gt;
&lt;p&gt;No big deal, you get the user to upload his/her avatar to your app and add it to the user document in the database. To serve it from the database, you just need to create a proxy in nginx/apache and redirect the virtual avatar url to the protected DB making sure the request is a GET request.&lt;/p&gt;
&lt;p&gt;Add to that a caching solution like &lt;a id=&quot;aptureLink_7B5BbhbrSj&quot; href=&quot;http://varnish.projects.linpro.no/&quot;&gt;varnish&lt;/a&gt; or &lt;a id=&quot;aptureLink_MulN3wi3Jx&quot; href=&quot;http://www.igvita.com/2008/02/11/nginx-and-memcached-a-400-boost/&quot;&gt;memcached module for nginx&lt;/a&gt; and all your db goodies get cached and served by the cache (server/client) until they get modified.&lt;/p&gt;
&lt;p&gt;Now, the problem is when you want to serve authorized attachments. Let&amp;#8217;s imagine that we want to let our users upload private files, files that should be accessible only by the owner or users designated by the owner.&lt;/p&gt;
&lt;p&gt;In this case, a simple &lt;a id=&quot;aptureLink_LIwhLKDqD9&quot; href=&quot;http://en.wikipedia.org/wiki/Nginx&quot;&gt;nginx&lt;/a&gt; rewrite wouldn&amp;#8217;t work. We need to authorize attachment requests. Here is a cool way of doing that using nginx and merb&amp;#8217;s router. (Expect Rails3 router to do the same).&lt;/p&gt;
&lt;p&gt;Let&amp;#8217;s start by setting up nginx and create a proxy for couchdb:&lt;/p&gt;
&lt;p&gt; Now that this is done, we are going to use Merb&amp;#8217;s awesome router to handle the incoming requests. The cool part of this is that we won&amp;#8217;t be dispatching requests so, going through the router is almost free. (check on the Merb router benchmarks for more info).  Let&amp;#8217;s edit our router and set a special route for our assets.  &lt;/p&gt;
&lt;p&gt;We are using a deferred route which gets executed instead of dispatching the request.&lt;/p&gt;
&lt;p&gt;If the attachment route is being matched then we are checking what environment we are currently running in. If we are in production or staging environment then we are sending back a rack response to the webserver. The response is just a forward to the proper couchdb document behind the proxy. Of course, before allowing that to happen, we could authenticate the logged in user, log the request and do a couple of other things. You have full access to your models from the router, so authenticating a session isn&amp;#8217;t a big deal. You could even create temporarily urls like AWS s3 does.&lt;/p&gt;
&lt;p&gt;If we are not in production or staging mode, then just redirect the request to couch since we assume you have access to the local db. This way, your asset urls will be working in production and dev. In real life, you&amp;#8217;ll want to apply the authorization before choosing how to deliver the document/attachment tho as you want it work the same way in development and production.&lt;/p&gt;</content>
		<author>
			<name>Matt Aimonetti (mattetti)</name>
			<uri>http://merbist.com</uri>
		</author>
		<source>
			<title type="html">Merbist</title>
			<subtitle type="html">Rails 3, Ruby &amp;amp; MacRuby news - tutorials - consulting - training</subtitle>
			<link rel="self" href="http://merbist.com/feed/"/>
			<id>http://merbist.com/feed/</id>
			<updated>2010-03-13T12:20:05+00:00</updated>
		</source>
	</entry>

</feed>
