<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Talk Like A Duck: Tag style</title>
    <link>http://talklikeaduck.denhaven2.com/articles/tag/style</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>In Ruby, it's not the dog, it's the tricks!</description>
    <item>
      <title>Block Your Privates!</title>
      <description>&lt;p&gt;&lt;img src="http://talklikeaduck.denhaven2.com/files/180px-Russian-Matroshka_no_bg.jpg" class="tease-image"/&gt;
I&amp;#8217;ve noticed that some rubyists like to use indentation to delineate method visibility.
&lt;p&gt;The first time I noticed this was when Marcel Molina Jr. used it during the 
&lt;a href="http://rubyhoedown2007.confreaks.com/session01.html"&gt;charity testing workshop at the Ruby Hoedown.&lt;/a&gt;. I just encountered it again in at least one piece of sample code from Rob Orsini&amp;#8217;s &amp;#8220;Rails Cookbook.&amp;#8221;&lt;/p&gt;
&lt;p&gt;During the testing workshop, Chad Fowler expressed displeasure with this style, and I&amp;#8217;ve got to agree.&lt;/p&gt;&lt;/p&gt;


&lt;p style="clear:both;"&gt;Here&amp;#8217;s an example of this style:&lt;/p&gt;
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="keyword"&gt;class &lt;/span&gt;&lt;span class="class"&gt;Gadget&lt;/span&gt;
  &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;framilize&lt;/span&gt;
    &lt;span class="ident"&gt;razzlitize&lt;/span&gt; &lt;span class="keyword"&gt;if&lt;/span&gt; &lt;span class="ident"&gt;options&lt;/span&gt;&lt;span class="punct"&gt;[&lt;/span&gt;&lt;span class="symbol"&gt;:secret_ingredient_6X&lt;/span&gt;&lt;span class="punct"&gt;]&lt;/span&gt;
  &lt;span class="keyword"&gt;end&lt;/span&gt;

  &lt;span class="ident"&gt;private&lt;/span&gt;
    &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;razzlitize&lt;/span&gt;
    &lt;span class="keyword"&gt;end&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The problem is that the indentation is &amp;#8220;artificial&amp;#8221;, there really is no nesting of evaluation scope here.
The way that Module#private and it&amp;#8217;s kin work when they are called without arguments is to set a marker onto
Ruby&amp;#8217;s eval stack which gets popped when the current evaluation context is exitted.  So the effects of 
the private method remain in effect until either the end of the current context, or one of the other methods
like public or protected, whichever comes first.  This means that if we extend the above example:&lt;/p&gt;
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="keyword"&gt;class &lt;/span&gt;&lt;span class="class"&gt;Gadget&lt;/span&gt;
  &lt;span class="comment"&gt;#...&lt;/span&gt;
  &lt;span class="ident"&gt;private&lt;/span&gt;
    &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;razzlitize&lt;/span&gt;
    &lt;span class="keyword"&gt;end&lt;/span&gt;

  &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;dazzlitize&lt;/span&gt;
  &lt;span class="keyword"&gt;end&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The indentation can trick us into thinking that dazzlitize is public when it&amp;#8217;s really private.&lt;/p&gt;
&lt;p&gt;I&amp;#8217;d always found the nature of methods like private without arguments to be a minor annoyance with
Ruby syntax. In my humble opinion they should have been able to take a block which delineated their effect.
What if we could write:
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="keyword"&gt;class &lt;/span&gt;&lt;span class="class"&gt;Gadget&lt;/span&gt;
  &lt;span class="comment"&gt;#...&lt;/span&gt;
  &lt;span class="ident"&gt;private&lt;/span&gt; &lt;span class="keyword"&gt;do&lt;/span&gt;
    &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;razzlitize&lt;/span&gt;
    &lt;span class="keyword"&gt;end&lt;/span&gt;
  &lt;span class="keyword"&gt;end&lt;/span&gt;

  &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;dazzlitize&lt;/span&gt;
  &lt;span class="keyword"&gt;end&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Well, actually we &lt;strong&gt;can&lt;/strong&gt; write that, but it doesn&amp;#8217;t work as expected, since the block 
is silently ignored, and the method doesn&amp;#8217;t even get defined.&lt;/p&gt;
&lt;p&gt;So I started doing a little metaprogramming, to fix this. It seemed obvious how to do this. You 
alias_method Module#private so you can extend it, redefine the method to check for a block and if none 
is given, call the original method, otherwise evaluate the block in a way which makes any methods defined
private.  And of course you use test driven design to do this.&lt;/p&gt;
&lt;p&gt;I made some progress but ran into some problems:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The obvious way to evaluate the block:
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="keyword"&gt;class &lt;/span&gt;&lt;span class="class"&gt;module&lt;/span&gt;
  &lt;span class="keyword"&gt;alias&lt;/span&gt; &lt;span class="symbol"&gt;:old_private&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="symbol"&gt;:private&lt;/span&gt;
  &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;private&lt;/span&gt;&lt;span class="punct"&gt;(*&lt;/span&gt;&lt;span class="ident"&gt;args&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="punct"&gt;&amp;amp;&lt;/span&gt;&lt;span class="ident"&gt;blk&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;
    &lt;span class="keyword"&gt;if&lt;/span&gt; &lt;span class="ident"&gt;blk_given?&lt;/span&gt;
       &lt;span class="ident"&gt;module_eval&lt;/span&gt; &lt;span class="keyword"&gt;do&lt;/span&gt;
         &lt;span class="ident"&gt;old_private&lt;/span&gt;
         &lt;span class="ident"&gt;blk&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;call&lt;/span&gt;
       &lt;span class="keyword"&gt;end&lt;/span&gt;
    &lt;span class="keyword"&gt;else&lt;/span&gt;
      &lt;span class="ident"&gt;old_private&lt;/span&gt;&lt;span class="punct"&gt;(*&lt;/span&gt;&lt;span class="ident"&gt;args&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;
    &lt;span class="keyword"&gt;end&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Doesn&amp;#8217;t seem to work.  The method isn&amp;#8217;t made private.  I had to resort to getting a list of
the methods before the block, then calling old_private with the difference between the new methods after
the block is evaluated, and those before.&lt;/p&gt;
&lt;p&gt;The module_eval now looked something like this:&lt;/p&gt;
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;module_eval do
  existing = instance_methods(false)
  blk.call
  new_methods = instance_methods(false)-existing
  old_private(new_methods.map {|m| m.to_sym}&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;There are some obvious shortcomings in this code, but at this point, the goal was to do the simplest
thing which could work for the test cases at hands, the complications would be dealt with later.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;To add insult to injury, calling old_private from within the new definition doesn&amp;#8217;t work either, 
the evaluation context gets marked as described above, but it gets popped off when we exit the context
of the new definition.  At this point I decided to punt, temporarily at least, and just define a new
method Module#with_private which took a block, and leave the existing Module#private alone.
&lt;li&gt;Then I decided that I should test changing the visibility of an existing method, in other words a test
case like this:
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;class Test
  protected
  def meth;end
  with_private do
     def meth;end
  end
end&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Test#meth should end up private, but my simple diff failed to notice the definition, things were starting
to get more complicated. It looked like I&amp;#8217;d need to hook Module#method_added and probably others. I began to
wonder whether it was worth it.
&lt;/ol&gt;
&lt;p&gt;Then I started to ponder the fact that the effect of private/protected/public gets popped when the 
evaluation context exits.  So I said to myself, &amp;#8220;Is there a way to take advantage of that?&amp;#8221;  Well, Virginia,
yes there is.
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="keyword"&gt;class &lt;/span&gt;&lt;span class="class"&gt;Gadget&lt;/span&gt;
  &lt;span class="comment"&gt;#...&lt;/span&gt;
  &lt;span class="ident"&gt;class_eval&lt;/span&gt; &lt;span class="keyword"&gt;do&lt;/span&gt;
    &lt;span class="ident"&gt;private&lt;/span&gt;

    &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;razzlitize&lt;/span&gt;
    &lt;span class="keyword"&gt;end&lt;/span&gt;

  &lt;span class="keyword"&gt;end&lt;/span&gt;

  &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;dazzlitize&lt;/span&gt;
  &lt;span class="keyword"&gt;end&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This seems to work as expected, here&amp;#8217;s a link to a 
&lt;a href="http://talklikeaduck.denhaven2.com/files/test_block_private.rb"&gt;test case file.&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Now I still prefer being able to give a block to Module#private and it&amp;#8217;s ilk, and I might still work
on that, but in the meantime, class_eval and module_eval seem to provide a way to actually do what I want.&lt;/p&gt;</description>
      <pubDate>Tue, 04 Sep 2007 11:28:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:a22bc9c4-17f0-42b2-93be-6ed10cc60a37</guid>
      <author>Rick DeNatale</author>
      <link>http://talklikeaduck.denhaven2.com/articles/2007/09/04/block-your-privates</link>
      <category>ruby</category>
      <category>best_practices</category>
      <category>style</category>
      <category>metaprogramming</category>
      <trackback:ping>http://talklikeaduck.denhaven2.com/articles/trackback/461</trackback:ping>
    </item>
    <item>
      <title>A Perl of Great Price</title>
      <description>&lt;p&gt;An old programming adage goes: &amp;#8220;You can write Fortran in any language.&amp;#8221;&lt;/p&gt;


	&lt;p&gt;Computer languages, like natural languages, tend to spawn their own cultures and &lt;a href="http://en.wikipedia.org/wiki/Parable_of_the_Pearl"&gt;religions&lt;/a&gt;. And it&amp;#8217;s not unexpected that the groups associated with these have &lt;a href="http://en.wikipedia.org/wiki/Pearl_of_Great_Price_%28Mormonism%29"&gt;different slants&lt;/a&gt; on things.&lt;/p&gt;


	&lt;p&gt;One of Ruby&amp;#8217;s strengths is that it stole from the best.  When I look at the language, it feels like Matz took the best features from a variety of languages including Smalltalk, Lisp, and not the least, Perl.&lt;/p&gt;


Two great features which Ruby inherited from Perl are integral support for regular expressions, and Perl&amp;#8217;s excellent statement modifiers which allow things like:
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="keyword"&gt;if&lt;/span&gt; &lt;span class="ident"&gt;test&lt;/span&gt;
   &lt;span class="ident"&gt;statement&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;to be replaced with the more succinct and usually clearer:
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="ident"&gt;statement&lt;/span&gt; &lt;span class="keyword"&gt;if&lt;/span&gt; &lt;span class="ident"&gt;test&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
However, &lt;span class="caps"&gt;IMHO&lt;/span&gt;, overuse of the Perl features in Ruby can make code &lt;b&gt;too&lt;/b&gt; Perl-like.

	&lt;p&gt;I&amp;#8217;ve seen my share of Ruby code which is hard-to read and understand, and a lot of that looks suspiciously like Perl code re-written in Ruby.&lt;/p&gt;


	&lt;p&gt;A ruby, being a gemstone which is normally cut before being put to use, has facets, unlike a pearl.&lt;/p&gt;


	&lt;p&gt;The facets of Ruby, the language, let indiduals approach it from different directions.  To me, as an old Smalltaker, it tends to look like Smalltalk. I tend to view it with Smalltalk-colored glasses.&lt;/p&gt;


	&lt;p&gt;I&amp;#8217;m sure that, to a programmer deeply steeped in Perl, Ruby looks like Perl with a cleaned-up view of object-oriented features.&lt;/p&gt;


&lt;h2&gt;Literate Programming&lt;/h2&gt;
I&amp;#8217;ve long been a fan of the goals of &lt;a href="http://www.literateprogramming.com/"&gt;literate programming&lt;/a&gt;. Source code should be viewed as literature, something which communcates to human readers, and not just fodder for a tool chain which produces something which is executable by a machine.
&lt;p/&gt;
&lt;p&gt;I read and write Perl on a level similar to how I converse in French.  I&amp;#8217;m told that I can speak French fairly well, and I can make myself understood.  On the other hand I have a hard time understanding spoken French.&lt;/p&gt;

	&lt;p&gt;Too often, Perl looks to me like a &lt;a href="http://catb.org/jargon/html/W/write-only-language.html"&gt;write-only language&lt;/a&gt;, an impression &lt;a href="http://en.wikipedia.org/wiki/Write-only_language"&gt;which is not uniquely mine&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;Some Perl programmers share a common predilection for something which users of another language often called read-only, &lt;span class="caps"&gt;APL&lt;/span&gt;, prided theselves on writing, one-liners. And this has inspired &lt;a href="http://www.fepus.net/ruby1line.txt"&gt;catalogs of Ruby one-liners&lt;/a&gt;&lt;/p&gt;


	&lt;p&gt;I&amp;#8217;ve found that a lot of the literature of Perl isn&amp;#8217;t very understandable.  I&amp;#8217;ve found that even Perl code which I&amp;#8217;ve written myself is impenetrable when I come back to it weeks, or even days after writing it.&lt;/p&gt;


	&lt;p&gt;I&amp;#8217;m sure that there are Perl programmers who don&amp;#8217;t find this to be true, just like my French friends seem to be able to converse without problems, but I continue to only muddle along in both languages.&lt;/p&gt;


	&lt;p&gt;The key indicator of Perl programs written in Ruby is the use of all those global variables with funny names like $*, $&amp;#38;, $0, etc.  Perl programmers love them since they are in Ruby for compatibility with Perl.  When I see them, my eyes glaze over, and my mind says &amp;#8221;$&amp;#38;^+_&amp;#8221;&lt;/p&gt;


	&lt;p&gt;I&amp;#8217;m not trying to antagonize the Perl programmers among the Ruby community, anymore than I try to antagonize my francophone friends, in fact I&amp;#8217;ve got great love for some particular francophones and perlographs.&lt;/p&gt;


	&lt;p&gt;But I&amp;#8217;ll probably never understand them as well as I&amp;#8217;d like to.&lt;/p&gt;


&lt;h2&gt;P.S. Not (Directly) Borrowed From Perl&lt;/h2&gt;
One feature of Perl is the use of &lt;a href="http://en.wikipedia.org/wiki/Sigil_%28computer_programming%29"&gt;sigils&lt;/a&gt; as markers on variable names.

	&lt;p&gt;Ruby uses sigils to mark some variables as being globals (there&amp;#8217;s that $ again, but the names don&amp;#8217;t have to be funny), instance variables, or class variables.&lt;/p&gt;


	&lt;p&gt;The difference is that Perl uses sigils to distinguish the types of variables, something which is unnecessary in Ruby since everything is an object.  Ruby instead uses them to find the variable binding in the case of a reference, or where to put it in the case of an initial definition.  A Ruby variable comes into existance the first time it is mentioned in the execution of a Ruby program, and it goes into the global variable &amp;#8220;pool&amp;#8221;, the current instance, or the current instances class depending on the sigil.  No declaration is necessary as compared to Smalltalk, for instance, where instance and class variables are &amp;#8216;declared&amp;#8217; by telling the class about them, and globals are defined by putting them in the system dictionary. While Perl variables aren&amp;#8217;t explicitly declared either, the real motivation for sigils in Perl is to associate a type with a variable.&lt;/p&gt;


	&lt;p&gt;Perl also makes use of the fact that all variables start with a sigil to automatically interpolate variables into strings. Since ruby variables don&amp;#8217;t all start with a sigil, Ruby uses #{} as a marker to interpolate an expression int to a string.  Note that if that expression is just a global, class or instance variable (all of which start with a sigil) a simple # will do the job.&lt;/p&gt;


	&lt;p&gt;So Perl uses sigils more as a way to express type, a static concept, in a dynamically executed language, and Ruby uses them to avoid static declarations, and make variable definition and use dynamic.&lt;/p&gt;</description>
      <pubDate>Wed, 16 Aug 2006 11:03:18 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:5abc0671-cdcc-47ee-b5fa-060f2172c170</guid>
      <author>Rick DeNatale</author>
      <link>http://talklikeaduck.denhaven2.com/articles/2006/08/16/a-perl-of-great-price</link>
      <category>ruby</category>
      <category>perl</category>
      <category>style</category>
      <category>peeves</category>
      <category>literateprogramming</category>
      <trackback:ping>http://talklikeaduck.denhaven2.com/articles/trackback/19</trackback:ping>
    </item>
  </channel>
</rss>
