<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Khaled alHabache's official blog &#187; Stackoverflow</title>
	<atom:link href="http://www.khelll.com/blog/category/stackoverflow/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.khelll.com/blog</link>
	<description>What web development means....</description>
	<lastBuildDate>Tue, 16 Mar 2010 23:21:41 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=abc</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>StackOverflow cool Ruby questions 4</title>
		<link>http://www.khelll.com/blog/ruby/stackoverflow-cool-ruby-questions-4/</link>
		<comments>http://www.khelll.com/blog/ruby/stackoverflow-cool-ruby-questions-4/#comments</comments>
		<pubDate>Tue, 16 Mar 2010 23:21:41 +0000</pubDate>
		<dc:creator>khelll</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Stackoverflow]]></category>

		<guid isPermaLink="false">http://www.khelll.com/blog/?p=623</guid>
		<description><![CDATA[Welcome to the fourth post of this series.  Just before proceeding to the questions, I would like to mention the great Metaprogramming Ruby: Program Like the Ruby Pros book by Paolo Perrotta, a very good book that explains the metaprogramming aspects of the Ruby language.
Now let&#8217;s get going!
Scope gates
Taken from the above mentioned book:
There [...]]]></description>
			<content:encoded><![CDATA[<p>Welcome to the fourth post of this <a href="http://www.khelll.com/blog/category/stackoverflow/">series</a>.  Just before proceeding to the questions, I would like to mention the great<a href="http://www.pragprog.com/titles/ppmetr/metaprogramming-ruby"> Metaprogramming Ruby: Program Like the Ruby Pros</a> book by Paolo Perrotta, a very good book that explains the metaprogramming aspects of the Ruby language.<br />
Now let&#8217;s get going!</p>
<h2>Scope gates</h2>
<p>Taken from the above mentioned book:</p>
<blockquote><p>There are exactly three places where a program leaves the previous scope behind and opens a new one:<br />
• Class definitions • Module definitions • Methods<br />
Scope changes whenever the program enters (or exits) a class or module definition or a method. These three borders are marked by the keywords class, module, and def, respectively. Each of these keywords acts like a Scope Gate.</p></blockquote>
<p><strong>The question title is</strong>: <a href="http://stackoverflow.com/questions/2289680/closure-doesnt-work">Closure doesn’t work</a></p>
<p><strong>The question text is</strong>:<br />
If block is a closure, why this code does not work? And how to make it work?</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"> <span style="color:#9966CC; font-weight:bold;">def</span> R<span style="color:#006600; font-weight:bold;">&#40;</span>arg<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">Class</span>.<span style="color:#9900CC;">new</span> <span style="color:#9966CC; font-weight:bold;">do</span>
        <span style="color:#9966CC; font-weight:bold;">def</span> foo
           <span style="color:#CC0066; font-weight:bold;">puts</span> arg
        <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> A <span style="color:#006600; font-weight:bold;">&lt;</span> R<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Hello!&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
A.<span style="color:#9900CC;">new</span>.<span style="color:#9900CC;">foo</span> <span style="color:#008000; font-style:italic;">#throws undefined local variable or method `arg' for #&lt;A:0x2840538&gt;</span></pre></div></div>

<p><strong>The answer is</strong>:<br />
Blocks are closures and arg is indeed available inside the <code>Class.new</code> block. It&#8217;s just not available inside the <code>foo</code> method because def starts a new scope. If you replace <code>def</code> with <code>define_method</code>, which takes a block, you&#8217;ll see the result you want:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> R<span style="color:#006600; font-weight:bold;">&#40;</span>arg<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">Class</span>.<span style="color:#9900CC;">new</span> <span style="color:#9966CC; font-weight:bold;">do</span>
        define_method<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:foo</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
           <span style="color:#CC0066; font-weight:bold;">puts</span> arg
        <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> A <span style="color:#006600; font-weight:bold;">&lt;</span> R<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Hello!&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
A.<span style="color:#9900CC;">new</span>.<span style="color:#9900CC;">foo</span> <span style="color:#008000; font-style:italic;"># Prints: Hello!</span></pre></div></div>

<h2>Almost everything in Ruby is an object</h2>
<p><strong>The question title is</strong>: <a href="http://stackoverflow.com/questions/416047/examples-of-things-that-are-not-objects-in-ruby">Examples of ‘Things’ that are not Objects in Ruby</a></p>
<p><strong>The question text is</strong>:<br />
&#8220;Everything is an object&#8221; was one of the first things I learned about Ruby, but in Peter Cooper&#8217;s Beginning Ruby: From Novice to Professional book, it is mentioned that &#8220;<strong>almost</strong> everything in Ruby is an object&#8221;.</p>
<p>Can you give me some <strong>examples of things that are not objects in Ruby</strong>?</p>
<p><strong>The answer is</strong>:<br />
he most obvious one that jumps into my head would be blocks. Blocks can be easily reified to a Proc object, either by using the &#038;block parameter form in a parameter list or by using lambda, proc, Proc.new or (in Ruby 1.9) the &#8220;stabby lambda&#8221; syntax. But on its own, they aren&#8217;t objects.</p>
<p>Another example are built in operators: while methods are objects, some operators are not implemented as methods (and, or &#038;&#038;, ||), so those operators aren&#8217;t objects.</p>
<h2>Hash keys</h2>
<p>The idea here is that whenever you want to define your own keys types for Ruby hashes, then you need to define 2 methods <a href="http://ruby-doc.org/core/classes/Object.html#M000347">#eq?</a> and <a href="http://ruby-doc.org/core/classes/Object.html#M000337">#hash</a> for those types.</p>
<p><strong>The question title is</strong>: <a href="http://stackoverflow.com/questions/2328685/how-to-make-object-instance-a-hash-key-in-ruby">How to make object instance a hash key in Ruby?</a></p>
<p><strong>The question text is</strong>:<br />
I have a class Foo with a few member variables. When all values in two instances of the class are equal I want the objects to be &#8216;equal&#8217;. I&#8217;d then like these objects to be keys in my hash. When I currently try this, the hash treats each instance as unequal.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">h = <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
f1 = Foo.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>a,b<span style="color:#006600; font-weight:bold;">&#41;</span>
f2 = Foo.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>a,b<span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>f1 and f2 should be equal at this point.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">h<span style="color:#006600; font-weight:bold;">&#91;</span>f1<span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#006666;">7</span>
h<span style="color:#006600; font-weight:bold;">&#91;</span>f2<span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#006666;">8</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> h<span style="color:#006600; font-weight:bold;">&#91;</span>f1<span style="color:#006600; font-weight:bold;">&#93;</span></pre></div></div>

<p>should print 8</p>
<p><strong>The answer is</strong>:<br />
See <a href="http://ruby-doc.org/core/classes/Hash.html">http://ruby-doc.org/core/classes/Hash.html</a></p>
<blockquote><p> Hash uses key.eql? to test keys for equality. If you need to use instances of your own classes as keys in a Hash, it is recommended that you define both the eql? and hash methods. The hash method must have the property that a.eql?(b) implies a.hash == b.hash.</p></blockquote>
<p>The eql? method is easy to implement: return true if all member variables are the same. For the hash method, use [@data1, @data2].hash</p>
<h2>Kernel#abort</h2>
<p><strong>The question title is</strong>: <a href="http://stackoverflow.com/questions/29539/ruby-exit-message">Ruby &#8211; Exit Message</a></p>
<p><strong>The question text is</strong>:<br />
Is there a one line function call that quits the program and displays a message? I know in Perl its as simple as this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">die<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Message goes here&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>Essentially I&#8217;m just tired of typing this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Message goes here&quot;</span>
<span style="color:#CC0066; font-weight:bold;">exit</span></pre></div></div>

<p><strong>The answer is</strong>:<br />
The &#8216;abort&#8217; function does this. For example:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">abort<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Message goes here&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.khelll.com/blog/ruby/stackoverflow-cool-ruby-questions-4/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>StackOverflow cool Ruby questions 3</title>
		<link>http://www.khelll.com/blog/ruby/stackoverflow-cool-ruby-questions-3/</link>
		<comments>http://www.khelll.com/blog/ruby/stackoverflow-cool-ruby-questions-3/#comments</comments>
		<pubDate>Fri, 29 Jan 2010 17:02:31 +0000</pubDate>
		<dc:creator>khelll</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Stackoverflow]]></category>

		<guid isPermaLink="false">http://www.khelll.com/blog/?p=611</guid>
		<description><![CDATA[Welcome to the third post of this ]]></description>
			<content:encoded><![CDATA[<p>Welcome to the third post of this <a href=http://www.khelll.com/blog/category/stackoverflow/">series</a>.  Just before diving into the new questions&#8217; set, I want to mention a little <a href="http://gist.github.com/255479">script</a> that I wrote to notify the user when a new question is posted on Stackoverflow, it works on Mac and uses growl.<br />
Now let&#8217;s proceed to questions:</p>
<h2>Relationship between the metaclass of base and derived classes</h2>
<p><strong>The question title is</strong>: <a href="http://stackoverflow.com/questions/2137853/what-is-the-relationship-between-the-metaclass-of-base-and-derived-class-in-ruby">What is the relationship between the metaclass of Base and Derived class in Ruby?</a></p>
<p><strong>The question text is</strong>:</p>
<p>In Ruby, we could use <code>super</code> within singleton method to call the corresponding super class&#8217;s singleton method, like the following code shows.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Base
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">class_method</span>
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Base class method&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> Derived <span style="color:#006600; font-weight:bold;">&lt;</span> Base
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">class_method</span>
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Derived class method&quot;</span>
    <span style="color:#9966CC; font-weight:bold;">super</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
Derived.<span style="color:#9900CC;">class_method</span>
<span style="color:#008000; font-style:italic;"># Derived class method</span>
<span style="color:#008000; font-style:italic;"># Base class method</span></pre></div></div>

<p>However, I don&#8217;t seem quite get how that call to <code>super</code> within <code>Derived.class_method</code> could reach <code>Base.class_method</code>. I&#8217;d assume that <code>class_method</code> is defined on their metaclass, does that mean their metaclass has parent/child relationship? (I can&#8217;t quite confirm that by experiments)</p>
<p>I&#8217;m asking this question because I remembered seeing somewhere there&#8217;s some kind of relationship between base and derived class&#8217; metaclass (but I can&#8217;t find it any more). In addition to know how actually <code>super</code> works, I&#8217;d also like to confirm whether the two metaclasses are totally separate or not.</p>
<p><strong>The answer is</strong>:<br />
There are four class objects in play here:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&lt;</span>Class<span style="color:#006600; font-weight:bold;">&gt;---</span>class<span style="color:#006600; font-weight:bold;">---&gt;&lt;</span>Class<span style="color:#006600; font-weight:bold;">&gt;</span>
Base               <span style="color:#008000; font-style:italic;">#Base</span>
   ^                  ^
   <span style="color:#006600; font-weight:bold;">|</span>                  <span style="color:#006600; font-weight:bold;">|</span>
   <span style="color:#006600; font-weight:bold;">|</span>                  <span style="color:#006600; font-weight:bold;">|</span>
 <span style="color:#9966CC; font-weight:bold;">super</span>              <span style="color:#9966CC; font-weight:bold;">super</span>
   <span style="color:#006600; font-weight:bold;">|</span>                  <span style="color:#006600; font-weight:bold;">|</span>
   <span style="color:#006600; font-weight:bold;">|</span>                  <span style="color:#006600; font-weight:bold;">|</span>
<span style="color:#006600; font-weight:bold;">&lt;</span>Class<span style="color:#006600; font-weight:bold;">&gt;</span>            <span style="color:#006600; font-weight:bold;">&lt;</span>Class<span style="color:#006600; font-weight:bold;">&gt;</span>
Derived<span style="color:#006600; font-weight:bold;">---</span>class<span style="color:#006600; font-weight:bold;">---&gt;</span><span style="color:#008000; font-style:italic;">#Derived</span></pre></div></div>

<p>Nomenclature:</p>
<ul>
<li><...> is the class of each object.</li>
<li>The name of the class is on the second line.</li>
<li>If the name starts with #, it&#8217;s the eigenclass (aka singleton class).</li>
<li>super points to a class&#8217;s superclass</li>
<li>class points to the class&#8217;s class.</li>
</ul>
<p>When you call Derived.class_method, Ruby follows the &#8220;right one and then up&#8221; rule: First go to the object&#8217;s class, then follow the superclass chain up, stopping when the method is found:</p>
<ul>
<li>The receiver of the &#8220;class_method&#8221; call is Derived. So follow the chain right to Derived&#8217;s class object, which is its eigenclass (#Derived).</li>
<li>  #Derived does not define the method, so Ruby follows the chain up the chain to #Derived&#8217;s superclass, which is #Base.</li>
<li>    The method is found there, so Ruby dispatches the message to #Base.class_method</li>
</ul>
<p>You don&#8217;t think I knew all this stuff off the top of my head, did you? Here&#8217;s where my brain got all this meta juju: <a href="http://pragprog.com/titles/ppmetr/metaprogramming-ruby">Metaprogramming Ruby</a>.</p>
<p><strong>Part 2.</strong> How to make an &#8220;eigenclass&#8221; (aka &#8220;singleton class&#8221;) come out of hiding</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#CC00FF; font-weight:bold;">Object</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> eigenclass
    <span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#0000FF; font-weight:bold;">self</span>
      <span style="color:#0000FF; font-weight:bold;">self</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>This method will return the eigenclass of any object. Now, what about classes? Those are objects, too.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">p</span> Derived.<span style="color:#9900CC;">eigenclass</span>               <span style="color:#008000; font-style:italic;"># =&gt; #&lt;Class:Derived&gt;</span>
<span style="color:#CC0066; font-weight:bold;">p</span> Derived.<span style="color:#9900CC;">eigenclass</span>.<span style="color:#9900CC;">superclass</span>    <span style="color:#008000; font-style:italic;"># =&gt; #&lt;Class:Base&gt;</span>
<span style="color:#CC0066; font-weight:bold;">p</span> Base.<span style="color:#9900CC;">eigenclass</span>                  <span style="color:#008000; font-style:italic;"># =&gt; #&lt;Class:Base&gt;</span></pre></div></div>

<p>Note: Above is from Ruby1.9. When run under Ruby 1.8, you get a surprise:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">p</span> Derived.<span style="color:#9900CC;">eigenclass</span>.<span style="color:#9900CC;">superclass</span>    <span style="color:#008000; font-style:italic;"># =&gt; #&lt;Class:Class&gt;</span></pre></div></div>

<h2>Idiomatic object creation</h2>
<p><strong>The question title is</strong>: <a href="http://stackoverflow.com/questions/1778638/idiomatic-object-creation-in-ruby">Idiomatic object creation in ruby</a></p>
<p><strong>The question text is</strong>:</p>
<p>In ruby, I often find myself writing the following:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Foo
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>bar, baz<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@bar</span> = bar
    <span style="color:#0066ff; font-weight:bold;">@baz</span> = baz
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> more stuff <span style="color:#006600; font-weight:bold;">&gt;&gt;</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>or even</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Foo
  attr_accessor <span style="color:#ff3333; font-weight:bold;">:bar</span>, <span style="color:#ff3333; font-weight:bold;">:baz</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>bar, baz<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@bar</span> = bar
    <span style="color:#0066ff; font-weight:bold;">@baz</span> = baz
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> more stuff <span style="color:#006600; font-weight:bold;">&gt;&gt;</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>I&#8217;m always keen to minimize boilerplate as much as possible &#8211; so is there a more idiomatic way of creating objects in ruby?</p>
<p><strong>The answer is:</strong></p>
<p>Struct objects are classes which do almost what you want. The only difference is, the initialize method has nil as default value for all it&#8217;s arguments. You use it like this</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">A= <span style="color:#CC00FF; font-weight:bold;">Struct</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:a</span>, <span style="color:#ff3333; font-weight:bold;">:b</span>, <span style="color:#ff3333; font-weight:bold;">:c</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>or</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> A <span style="color:#006600; font-weight:bold;">&lt;</span> Struc.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:a</span>, <span style="color:#ff3333; font-weight:bold;">:b</span>, <span style="color:#ff3333; font-weight:bold;">:c</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p><em>Struct has one big drawback. You can not inherit from another class.</em></p>
<h2>Block vs brackets</h2>
<p><strong>The question title is</strong>: <a href="http://stackoverflow.com/questions/2122380/using-do-block-vs-brackets">Using do block vs brackets {}</a></p>
<p><strong>The question text is</strong>:</p>
<p>New to ruby, put on your newbie gloves.</p>
<p>Is there any difference (obscure or practical) between the following two snippets?</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">my_array = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:uno</span>, <span style="color:#ff3333; font-weight:bold;">:dos</span>, <span style="color:#ff3333; font-weight:bold;">:tres</span><span style="color:#006600; font-weight:bold;">&#93;</span>
my_array.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>item<span style="color:#006600; font-weight:bold;">|</span> 
    <span style="color:#CC0066; font-weight:bold;">puts</span> item
<span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
my_array = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:uno</span>, <span style="color:#ff3333; font-weight:bold;">:dos</span>, <span style="color:#ff3333; font-weight:bold;">:tres</span><span style="color:#006600; font-weight:bold;">&#93;</span>
my_array.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>item<span style="color:#006600; font-weight:bold;">|</span> 
    <span style="color:#CC0066; font-weight:bold;">puts</span> item
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>I realize the bracket syntax would allow you to place the block on one line</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">my_array.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>item<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#CC0066; font-weight:bold;">puts</span> item <span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>but outside of that are there any compelling reasons to use one syntax over the other?</p>
<p><strong>The  answer is:</strong></p>
<p><a href="http://rads.stackoverflow.com/amzn/click/0596523696">Ruby cookbook</a> says bracket syntax has higher precedence order than <code>do..end</code><br />
Keep in mind that the bracket syntax has a higher precedence than the do..end syntax. Consider the following two snippets of code:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">1.<span style="color:#9900CC;">upto</span> <span style="color:#006666;">3</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> x
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
1.<span style="color:#9900CC;">upto</span> <span style="color:#006666;">3</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#CC0066; font-weight:bold;">puts</span> x <span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#008000; font-style:italic;"># SyntaxError: compile error</span></pre></div></div>

<p>Second example only works when parentheses is used, <code>1.upto(3) { |x| puts x }</code></p>
<h2>String and whitespace</h2>
<p><strong>The question title is</strong>: <a href="http://stackoverflow.com/questions/2130676/see-if-a-ruby-string-has-whitespace-in-it">See if a ruby string has whitespace in it</a></p>
<p><strong>The question text is</strong>:</p>
<p>I want to see if a string has any white space in it. What&#8217;s the most effective way of doing this in ruby?</p>
<p>Thanks</p>
<p><strong>The  answer is:</strong></p>
<p>If by &#8220;white space&#8221; you mean in the Regular Expression sense, which is any of space character, tab, newline, carriage return or (I think) form-feed, then any of the answers provided will work:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">s.<span style="color:#9900CC;">match</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>\s<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#41;</span>
s.<span style="color:#9900CC;">index</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>\s<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#41;</span>
s =~ <span style="color:#006600; font-weight:bold;">/</span>\s<span style="color:#006600; font-weight:bold;">/</span>
s<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">/</span>\s<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#93;</span></pre></div></div>

<h2>Index of the largest value in an array</h2>
<p><strong>The question title is</strong>: <a href="http://stackoverflow.com/questions/2149802/in-ruby-what-is-the-cleanest-way-of-obtaining-the-index-of-the-largest-value-in">In Ruby, what is the cleanest way of obtaining the index of the largest value in an array? </a></p>
<p><strong>The question text is</strong>:</p>
<p>If a is the array, I want a.index(a.max), but something more Ruby-like. It should be obvious, but I&#8217;m having trouble finding the answer at so and elsewhere. Obviously, I am new to Ruby.</p>
<p><strong>The  answer is:</strong></p>
<p>For Ruby 1.8.7 or above:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">a.<span style="color:#9900CC;">each_with_index</span>.<span style="color:#9900CC;">max</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span></pre></div></div>

<p>It does one iteration. Not entirely the most semantic thing ever, but if you find yourself doing this a lot, I would wrap it in an index_of_max method anyway.</p>
<p><code>each_with_index</code> without a block returns an enumerator that gives the item and its index. We then send <code>max</code> to this enumerator, which does the standard <code>max</code> algorithm on item-index pairs. <code>Array.<=></code> is implemented so that the first item determines the ordering (unless there&#8217;s a tie, in which case the second is compared, and so on), so this works basically the same as doing <code>max</code> on an array of the values themselves. Then to get the index, we ask for the second item of the result (since we got a series of <code>[value, index]</code> pairs from <code>each_with_index</code>)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.khelll.com/blog/ruby/stackoverflow-cool-ruby-questions-3/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>StackOverflow cool Ruby questions 2</title>
		<link>http://www.khelll.com/blog/ruby/stackoverflow-cool-ruby-questions-2/</link>
		<comments>http://www.khelll.com/blog/ruby/stackoverflow-cool-ruby-questions-2/#comments</comments>
		<pubDate>Sat, 17 Oct 2009 15:58:44 +0000</pubDate>
		<dc:creator>khelll</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Stackoverflow]]></category>

		<guid isPermaLink="false">http://www.khelll.com/blog/?p=587</guid>
		<description><![CDATA[Welcome in this second post of this series. Just before I list the questions of this one, I want to mention 2 things:

You can follow StackOverflow tagged Ruby questions on twitter using the user: sof_ruby 
Some ppl feel shy to ask or even to answer, my (little) experience in life taught me that If you [...]]]></description>
			<content:encoded><![CDATA[<p>Welcome in this second post of this <a href="http://www.khelll.com/blog/category/stackoverflow/">series</a>. Just before I list the questions of this one, I want to mention 2 things:</p>
<ul>
<li>You can follow StackOverflow tagged Ruby questions on twitter using the user: <a href="http://twitter.com/sof_ruby">sof_ruby</a> </li>
<li>Some ppl feel shy to ask or even to answer, my (little) experience in life taught me that If you don&#8217;t ask, you don&#8217;t learn. If you don&#8217;t participate, you don&#8217;t learn. On the other side that&#8217;s the benefit of pair programming, ain&#8217;t it?</li>
</ul>
<p>Now let&#8217;s proceed to questions:</p>
<h2>Hash Autovivification</h2>
<p>Hash Autovivication in simple words is the ability to do things like:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">a=<span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
a<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'b'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'c'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'d'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'e'</span><span style="color:#006600; font-weight:bold;">&#93;</span>=<span style="color:#996600;">'f'</span></pre></div></div>

<p>The cool idea here is that all intermediate-level hashs will be created automatically.<br />
We can&#8217;t do that directly in Ruby, a statement like the previous one will issue an error : <code>NoMethodError: undefined method `[]' for nil:NilClass<br />
</code></p>
<p><strong>The question title is</strong>: <a href="http://stackoverflow.com/questions/1485186/instance-eval-within-block">ruby hash autovivification (facets)</a></p>
<p><strong>The question text is</strong>:<br />
Here is a clever trick to enable hash autovivification in ruby (taken from facets):</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># Monkey patching Hash class:</span>
<span style="color:#008000; font-style:italic;"># File lib/core/facets/hash/autonew.rb, line 19</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">autonew</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>args<span style="color:#006600; font-weight:bold;">&#41;</span>
    leet = <span style="color:#CC0066; font-weight:bold;">lambda</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>hsh, key<span style="color:#006600; font-weight:bold;">|</span> hsh<span style="color:#006600; font-weight:bold;">&#91;</span>key<span style="color:#006600; font-weight:bold;">&#93;</span> = new<span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#006600; font-weight:bold;">&amp;</span>leet <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
    new<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>args,<span style="color:#006600; font-weight:bold;">&amp;</span>leet<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Although it works (of course), I find it really frustrating that I can&#8217;t figure out how this two liner does what it does.</p>
<p><strong>The answer is</strong>:<br />
The standard <a href="http://ruby-doc.org/core/classes/Hash.html#M002840">new method for Hash</a> accepts a block. This block is called in the event of trying to access a key in the Hash which does not exist. The block is passed the Hash itself and the key that was requested (the two parameters) and should return the value that should be returned for the requested key.</p>
<p>You will notice that the leet lambda does 2 things. It returns a new Hash with leet itself as the block for handling defaults. This is the behaviour which allows autonew to work for Hashes of arbitrary depth. It also assigns this new Hash to hsh[key] so that next time you request the same key you will get the existing Hash rather than a new one being created.</p>
<p>So:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">a = <span style="color:#CC00FF; font-weight:bold;">Hash</span>.<span style="color:#9900CC;">autonew</span>
a<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'b'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'c'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'d'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'e'</span><span style="color:#006600; font-weight:bold;">&#93;</span>=<span style="color:#996600;">'f'</span> <span style="color:#008000; font-style:italic;">#=&gt; &quot;f&quot;</span>
a.<span style="color:#9900CC;">inspect</span> <span style="color:#008000; font-style:italic;">#=&gt; {&quot;b&quot;=&gt;{&quot;c&quot;=&gt;{&quot;d&quot;=&gt;{&quot;e&quot;=&gt;&quot;f&quot;}}}}</span></pre></div></div>

</ul>
<h2>Class instance variables</h2>
<p>This answer of this one is easy, but the idea of <strong>Class instance variables</strong> is what matters.</p>
<p><strong>The question title is</strong>: <a href="http://stackoverflow.com/questions/1548661/total-newbie-instance-variables-in-ruby">Total newbie: Instance variables in ruby?</a></p>
<p><strong>The question text is</strong>:<br />
Pardon the total newbiew question but why is <code>@game_score</code> always nil?</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Bowling
  <span style="color:#0066ff; font-weight:bold;">@game_score</span> = <span style="color:#006666;">0</span>
    <span style="color:#9966CC; font-weight:bold;">def</span> hit<span style="color:#006600; font-weight:bold;">&#40;</span>pins<span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#0066ff; font-weight:bold;">@game_score</span> = <span style="color:#0066ff; font-weight:bold;">@game_score</span> <span style="color:#006600; font-weight:bold;">+</span> pins
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> score
        <span style="color:#0066ff; font-weight:bold;">@game_score</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p><strong>The answer is</strong>:<br />
Because you don&#8217;t have</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> initialize
  <span style="color:#0066ff; font-weight:bold;">@game_score</span> = <span style="color:#006666;">0</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>The assignment in the class definition is not doing what you think it is doing, and when <code>hit</code> gets invoked it can&#8217;t add to <code>nil</code>.<br />
If you now ask what happened to <code>@game_score</code>?, well, always remember Class is an object and Object is a class.<br />
It&#8217;s way cool the way Ruby classes have this Zen-like &#8220;real&#8221; existence. Ruby doesn&#8217;t precisely have named classes, rather, class names are references to objects of class <code>Class</code>. <strong>By assigning to <code>@game_score</code> outside of an instance method you created a class instance variable, an attribute of the class object <code>Bowling</code>, which is an instance of class <code>Class</code></strong>. These objects are not, in general, very useful. (See Chapter 1, The Ruby Way, Hal Fulton.)</p>
<p>You can reach the @game_score class instance variable by doing this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> Bowling
  attr_accessor <span style="color:#ff3333; font-weight:bold;">:game_score</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
Bowling.<span style="color:#9900CC;">game_score</span> <span style="color:#008000; font-style:italic;">#=&gt; 0</span>
Bowling.<span style="color:#9900CC;">game_score</span> = <span style="color:#006666;">6</span> <span style="color:#008000; font-style:italic;">#=&gt; 6</span></pre></div></div>

<p><strong>(A question for you: What is the difference between class variables and class instance variables?)</strong></p>
<h2>Creating data structures in Ruby</h2>
<p>I have committed a mistake myself in answering this question <img src='http://www.khelll.com/blog/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> , however remember: If you don&#8217;t try, you don&#8217;t learn.</p>
<p><strong>The question title is</strong>: <a href="http://stackoverflow.com/questions/1571349/can-the-array-be-reinvented-in-ruby">Can the array be reinvented in Ruby?</a></p>
<p><strong>The question text is</strong>:<br />
This is just a hypothetical question, if you wouldn&#8217;t have the <code>Array</code> and the <code>Hash</code> class, would there be any way of implementing an Array class in pure Ruby? How?</p>
<p><strong>The answer is</strong>:<br />
Yes we can:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> MyArray
  <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#CC00FF; font-weight:bold;">Enumerable</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize
    <span style="color:#0066ff; font-weight:bold;">@size</span> = <span style="color:#006666;">0</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;</span><span style="color:#006600; font-weight:bold;">&#40;</span>val<span style="color:#006600; font-weight:bold;">&#41;</span>
    instance_variable_set<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;@a#{@size}&quot;</span>.<span style="color:#9900CC;">to_sym</span>, val<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@size</span> <span style="color:#006600; font-weight:bold;">+</span>= <span style="color:#006666;">1</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#40;</span>n<span style="color:#006600; font-weight:bold;">&#41;</span>
    instance_variable_get<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;@a#{n}&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> length
    <span style="color:#0066ff; font-weight:bold;">@size</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> each
    0.<span style="color:#9900CC;">upto</span><span style="color:#006600; font-weight:bold;">&#40;</span>@size <span style="color:#006600; font-weight:bold;">-</span> <span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>n<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#9966CC; font-weight:bold;">yield</span> <span style="color:#0000FF; font-weight:bold;">self</span><span style="color:#006600; font-weight:bold;">&#91;</span>n<span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
a = MyArray.<span style="color:#9900CC;">new</span>
a <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#006666;">1</span>
a <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#006666;">2</span>
<span style="color:#CC0066; font-weight:bold;">p</span> a.<span style="color:#9900CC;">to_a</span>     <span style="color:#008000; font-style:italic;">#=&gt; [1,2]</span></pre></div></div>

<p>This works by creating instance variables <code>@a0</code>, <code>@a1</code>, etc. on the object to represent array indices 0, 1, etc. It has constant time length and index operations. The rest of the operations (remove, etc.) are a bit more effort to implement, but it&#8217;s absolutely possible.</p>
<p>Note that the constant time property for the index operation depends on the underlying Ruby runtime using an appropriate data structure for instance variables.</p>
<h2>Hash#reject</h2>
<p><strong>The question title is</strong>: <a href="http://stackoverflow.com/questions/1531047/update-attributes-unless-blank">Update attributes unless blank?</a></p>
<p><strong>The question text is</strong>:<br />
I have an existing Project record, and I&#8217;m importing a CSV file to update the associated Project attributes. However, often the CSV will contain blank fields and I don&#8217;t want to overright exisiting attributes if the related CSV field is blank.</p>
<p>Something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">project.<span style="color:#9900CC;">update_attributes</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:name</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> row.<span style="color:#9900CC;">field</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'project_name'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">unless</span> row.<span style="color:#9900CC;">field</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'project_name'</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">blank</span>?,                                                                         
                          <span style="color:#ff3333; font-weight:bold;">:owner</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> row.<span style="color:#9900CC;">field</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'project_owner'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">unless</span> row.<span style="color:#9900CC;">field</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'project_owner'</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">blank</span>?,
                          <span style="color:#ff3333; font-weight:bold;">:due_date</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> row.<span style="color:#9900CC;">field</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'project_due_date'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">unless</span> row.<span style="color:#9900CC;">field</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'project_due_date'</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">blank</span>?<span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p><strong>The answer is:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">project.<span style="color:#9900CC;">update_attributes</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#123;</span>:name <span style="color:#006600; font-weight:bold;">=&gt;</span> row.<span style="color:#9900CC;">field</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'project_name'</span><span style="color:#006600; font-weight:bold;">&#41;</span>,                                                                         
                          <span style="color:#ff3333; font-weight:bold;">:owner</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> row.<span style="color:#9900CC;">field</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'project_owner'</span><span style="color:#006600; font-weight:bold;">&#41;</span>,
                          <span style="color:#ff3333; font-weight:bold;">:due_date</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> row.<span style="color:#9900CC;">field</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'project_due_date'</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#125;</span>.<span style="color:#9900CC;">reject</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>k,v<span style="color:#006600; font-weight:bold;">|</span> v.<span style="color:#9900CC;">blank</span>?<span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<h2>Ruby &#8216;private&#8217; behavior</h2>
<p>The key point here to remember is that <code>private</code>, <code>protected</code> and <code>pulbic</code></a> are just other methods defined in Ruby <a href="http://www.ruby-doc.org/core/classes/Module.html">Module</a> class.</p>
<p><strong>The question title is</strong>: <a href="http://stackoverflow.com/questions/1565269/are-there-good-reasons-for-private-to-work-the-way-it-does-in-ruby">Are there good reasons for ‘private’ to work the way it does in Ruby?</a></p>
<p><strong>The question text is</strong>:<br />
It took me a while to understand how private methods work in Ruby, and it really strikes me as being very awkward. Does anyone know if there are good reasons for private methods to be handled the way they are? Is it just historic reasons? Or implementation reasons? Or are there good solid logical reasons (ie. semantic)?</p>
<p>For example:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Person
  private
  attr_reader <span style="color:#ff3333; font-weight:bold;">:weight</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> Spy <span style="color:#006600; font-weight:bold;">&lt;</span> Person
 private
  attr_accessor <span style="color:#ff3333; font-weight:bold;">:code</span>
 public
  <span style="color:#9966CC; font-weight:bold;">def</span> test
    code          <span style="color:#008000; font-style:italic;">#(1) OK: you can call a private method in self</span>
    Spy.<span style="color:#9900CC;">new</span>.<span style="color:#9900CC;">code</span>  <span style="color:#008000; font-style:italic;">#(2) ERROR: cannot call a private method on any other object</span>
    <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">code</span>     <span style="color:#008000; font-style:italic;">#(3) ERROR!!! cannot call a private method explicitly on 'self'</span>
    code=<span style="color:#996600;">&quot;xyz&quot;</span>    <span style="color:#008000; font-style:italic;">#(4) Ok, it runs, but it actually creates a local variable!!!</span>
    <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">code</span>=<span style="color:#996600;">&quot;z&quot;</span> <span style="color:#008000; font-style:italic;">#(5) OK! This is the only case where explicit 'self' is ok</span>
    weight        <span style="color:#008000; font-style:italic;">#(6) OK! You can call a private method defined in a base class</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<ul>
<li>Ruby&#8217;s behaviour on lines (1), (2) and (5) seems reasonable.</li>
<li>The fact that (6) is ok is a bit strange, especially coming from Java and C++. Any good reason for this?</li>
<li>I really do not understand why (3) fails ! An explanation, anyone?</li>
<li>The problem on line (4) looks like an ambiguity in the grammar, which has nothing to do with &#8216;private&#8217;.</li>
<p>Any ideas?
</ul>
<p><strong>The  answer is:</strong></p>
<p>You might find it helpful to read ruby&#8217;s <a href="http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut%5Fclasses.html">definition of public, private and protected</a> <strong>(Skip to Access Control)<br />
</strong><br />
Ruby&#8217;s private is analogous to Java&#8217;s protected. There is no Ruby equivalent of Java&#8217;s private. But there are ways to fake it.</p>
<p>Private is defined as methods/variables that can <strong>only</strong> be called implicitly. This is why statements 2 and 3 fail. In other words, private limits methods/variables to the context of a class or subclass in which they are defined. Inheritance passes private methods to the subclasses and can therefore be accessed with an implicit self. (Explaining why statement 6 works.)</p>
<p>I think you&#8217;re looking for something closer to protected. Which behaves similarly to Java accessors that are not given a visibility (eg: public, private, protected) By changing the private in Spy to protected all 6 of your statements work. Protected methods can be called by any instance of the defining class or their subclasses. Either explicitly or implicitly called on self are valid statements for protected methods so long as the caller is either the class of the object responding to the call, or inherits from it.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Person
  private
  attr_reader <span style="color:#ff3333; font-weight:bold;">:weight</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> Spy <span style="color:#006600; font-weight:bold;">&lt;</span> Person
 protected
  attr_accessor <span style="color:#ff3333; font-weight:bold;">:code</span>
 public
  <span style="color:#9966CC; font-weight:bold;">def</span> test
    code          <span style="color:#008000; font-style:italic;">#(1) OK: you can call a private method in self</span>
    Spy.<span style="color:#9900CC;">new</span>.<span style="color:#9900CC;">code</span>  <span style="color:#008000; font-style:italic;">#(2) OK: Calling protected method on another instance from same class family or a descendant.</span>
    <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">code</span>     <span style="color:#008000; font-style:italic;">#(3) OK: Calling protected method on with explicit self is allowed with protected</span>
    code=<span style="color:#996600;">&quot;xyz&quot;</span>    <span style="color:#008000; font-style:italic;">#(4) Ok, it runs, but it actually creates a local variable!!!</span>
    <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">code</span>=<span style="color:#996600;">&quot;z&quot;</span> <span style="color:#008000; font-style:italic;">#(5) OK! This is the only case where explicit 'self' is ok</span>
    weight        <span style="color:#008000; font-style:italic;">#(6) OK! You can call a private method defined in a base class</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
s = Spy.<span style="color:#9900CC;">new</span>
s.<span style="color:#9900CC;">test</span> <span style="color:#008000; font-style:italic;"># succeeds</span>
s.<span style="color:#9900CC;">code</span> <span style="color:#008000; font-style:italic;">#(7) Error: Calling protected method outside of the class or its descendants.</span></pre></div></div>

<p>As for statement 4. You are correct in assuming this is to avoid ambiguity. It&#8217;s more a safeguard to the potential harm of ruby&#8217;s dynamic nature. It ensures that you cannot override accessors by opening up the class again later. A situation that can arise, for example by eval&#8217;ing tainted code.</p>
<p>I can only speculate on he design decisions that led to these behaviours. For most of it I feel it comes down to the dynamic nature of the language.</p>
<p>P.S. If you really want to give things the java definition of private. Only available to the class in which it&#8217;s defined, not even subclasses. You could add an self.inherited method to your classes to remove references to the methods you want to limit access to.</p>
<p>Making the weight attribute inaccessible from subclasses:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Person
  private
  attr_reader <span style="color:#ff3333; font-weight:bold;">:weight</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize
    <span style="color:#0066ff; font-weight:bold;">@weight</span> = <span style="color:#006666;">5</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">inherited</span><span style="color:#006600; font-weight:bold;">&#40;</span>subclass<span style="color:#006600; font-weight:bold;">&#41;</span>
    subclass.<span style="color:#9900CC;">send</span> <span style="color:#ff3333; font-weight:bold;">:undef_method</span>, <span style="color:#ff3333; font-weight:bold;">:weight</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> Spy <span style="color:#006600; font-weight:bold;">&lt;</span> Person
 private
  attr_accessor <span style="color:#ff3333; font-weight:bold;">:code</span>
 public
  <span style="color:#9966CC; font-weight:bold;">def</span> test
     weight       
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
Person.<span style="color:#9900CC;">new</span>.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:weight</span><span style="color:#006600; font-weight:bold;">&#41;</span>  <span style="color:#008000; font-style:italic;"># =&gt; 5</span>
Spy.<span style="color:#9900CC;">new</span>.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:weight</span><span style="color:#006600; font-weight:bold;">&#41;</span>  <span style="color:#008000; font-style:italic;">#=&gt; Unhelpful undefined method error</span></pre></div></div>

<p>It may make more sense to replace the undef_method call to something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">inherited</span><span style="color:#006600; font-weight:bold;">&#40;</span>subclass<span style="color:#006600; font-weight:bold;">&#41;</span>
    subclass.<span style="color:#9900CC;">class_eval</span> <span style="color:#006600; font-weight:bold;">%</span><span style="color:#006600; font-weight:bold;">&#123;</span>
      <span style="color:#9966CC; font-weight:bold;">def</span> weight 
        <span style="color:#CC0066; font-weight:bold;">raise</span> <span style="color:#996600;">&quot;Private method called from subclass. Access Denied&quot;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
     <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Which provides a much more helpful error and the same functionality.</p>
<p>The send is necessary to get around calling private methods for other classes. Only used to prove that things are actually working.</p>
<p>Which in hindsight, makes private and protected useless. If you&#8217;re really serious about protecting your methods you will have to override send to block them. You could also make send private.</p>
<p>It ends up being one of those &#8220;Damned if you do, damned if you don&#8217;t&#8221; situations. There are legitimate uses of calling send from outside an object. And overriding send requires some tricky code to account for the variable number of arguments that can be sent to any method.</p>
<p>That&#8217;s an exercise for another time.</p>
<p><strong>Suggestions/comments regarding the content of this series are welcome! Have your say!</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://www.khelll.com/blog/ruby/stackoverflow-cool-ruby-questions-2/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>StackOverflow cool Ruby questions</title>
		<link>http://www.khelll.com/blog/ruby/stackoverflow-cool-ruby-question/</link>
		<comments>http://www.khelll.com/blog/ruby/stackoverflow-cool-ruby-question/#comments</comments>
		<pubDate>Wed, 30 Sep 2009 05:25:08 +0000</pubDate>
		<dc:creator>khelll</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Stackoverflow]]></category>

		<guid isPermaLink="false">http://www.khelll.com/blog/?p=573</guid>
		<description><![CDATA[I have participated heavily during the last 2 weeks in answering Ruby tagged questions on stackoverflow. That took place mainly after Ryan Bates tweet wishing to see more Ruby developers on stackoverflow. Since then I have enjoyed reading various kinds of questions that range in level from basic to professional ones and which cover various [...]]]></description>
			<content:encoded><![CDATA[<p>I have participated heavily during the last 2 weeks in answering <a href="http://stackoverflow.com/questions/tagged/ruby">Ruby tagged</a> questions on <a href="http://stackoverflow.com/">stackoverflow</a>. That took place mainly after Ryan Bates <a href="http://twitter.com/rbates/status/3270391247">tweet</a> wishing to see more Ruby developers on stackoverflow. Since then I have enjoyed reading various kinds of questions that range in level from basic to professional ones and which cover various stuff like normal questions, tricks, meta programming, best practices and language specific features.</p>
<p>So, I&#8217;m announcing in this post an <a href="http://www.khelll.com/blog/category/stackoverflow/">endless series</a> called &#8220;Stackoverflow cool Ruby questions&#8221; which will target cool Ruby questions on stackoverflow. I strongly recommend that you visit Stackoverflow on daily basis and try to participate if you have the time, but if you don&#8217;t, then I hope that you will enjoy this educational series.</p>
<p>There are currently more than 4,640 tagged Ruby questions, I&#8217;ll try to mix old + recent questions. I won&#8217;t cover basic questions here unless the question gets high votes. The covered questions will be related to new, tricky and controversial topics.</p>
<p>Please don&#8217;t hesitate to give any notes regarding the idea and the way the questions are picked and presented.</p>
<p>Let&#8217;s start this series, with 3 questions for this post:</p>
<h2>&#8216;Instance_eval&#8217; instead of &#8216;yield(self)&#8217;</h2>
<p><strong>The question title is</strong>: <a href="http://stackoverflow.com/questions/1485186/instance-eval-within-block">Instance Eval Within Block</a></p>
<p><strong>The question text is</strong>:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Builder
    <span style="color:#9966CC; font-weight:bold;">def</span> initialize
        <span style="color:#0066ff; font-weight:bold;">@lines</span> = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> lines
        block_given? ? <span style="color:#9966CC; font-weight:bold;">yield</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span><span style="color:#006600; font-weight:bold;">&#41;</span> : <span style="color:#0066ff; font-weight:bold;">@lines</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> add_line<span style="color:#006600; font-weight:bold;">&#40;</span> text <span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#0066ff; font-weight:bold;">@lines</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> text
    <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Now, how do I change this</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">my_builder = Builder.<span style="color:#9900CC;">new</span>
my_builder.<span style="color:#9900CC;">lines</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>b<span style="color:#006600; font-weight:bold;">|</span>
    b.<span style="color:#9900CC;">add_line</span> <span style="color:#996600;">&quot;foo&quot;</span>
    b.<span style="color:#9900CC;">add_line</span> <span style="color:#996600;">&quot;bar&quot;</span>
<span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#CC0066; font-weight:bold;">p</span> my_builder.<span style="color:#9900CC;">lines</span> <span style="color:#008000; font-style:italic;"># =&gt; [&quot;foo&quot;, &quot;bar&quot;]</span></pre></div></div>

<p>Into this?</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">my_builder = Builder.<span style="color:#9900CC;">new</span>
my_builder.<span style="color:#9900CC;">lines</span> <span style="color:#006600; font-weight:bold;">&#123;</span>
    add_line <span style="color:#996600;">&quot;foo&quot;</span>
    add_line <span style="color:#996600;">&quot;bar&quot;</span>
<span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#CC0066; font-weight:bold;">p</span> my_builder.<span style="color:#9900CC;">lines</span> <span style="color:#008000; font-style:italic;"># =&gt; [&quot;foo&quot;, &quot;bar&quot;]</span></pre></div></div>

<p><strong>The answer is</strong>:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Builder
    <span style="color:#9966CC; font-weight:bold;">def</span> initialize
        <span style="color:#0066ff; font-weight:bold;">@lines</span> = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> lines<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&amp;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span>
        block_given? ? instance_eval<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&amp;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span> : <span style="color:#0066ff; font-weight:bold;">@lines</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> add_line<span style="color:#006600; font-weight:bold;">&#40;</span> text <span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#0066ff; font-weight:bold;">@lines</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> text
    <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
my_builder = Builder.<span style="color:#9900CC;">new</span>
my_builder.<span style="color:#9900CC;">lines</span> <span style="color:#006600; font-weight:bold;">&#123;</span>
    add_line <span style="color:#996600;">&quot;foo&quot;</span>
    add_line <span style="color:#996600;">&quot;bar&quot;</span>
<span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#CC0066; font-weight:bold;">p</span> my_builder.<span style="color:#9900CC;">lines</span> <span style="color:#008000; font-style:italic;"># =&gt; [&quot;foo&quot;, &quot;bar&quot;]</span></pre></div></div>

<p>The trick here is to use instance_eval which evaluates the passed block in the context of the current object called on.</p>
<h2>Determining whether a variable is any one of a list of values</h2>
<p><strong>The question title is</strong>: <a href="http://stackoverflow.com/questions/1487075/ruby-shortest-most-idiomatic-way-of-determining-whether-a-variable-is-any-one-of">Shortest/most idiomatic way of determining whether a variable is any one of a list of values</a></p>
<p><strong>The question text is:</strong></p>
<p>This seems a ridiculously simple question to be asking, but what&#8217;s the shortest/most idiomatic way of rewriting this in Ruby?</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">if</span> variable == <span style="color:#ff3333; font-weight:bold;">:a</span> <span style="color:#9966CC; font-weight:bold;">or</span> variable == <span style="color:#ff3333; font-weight:bold;">:b</span> <span style="color:#9966CC; font-weight:bold;">or</span> variable == <span style="color:#ff3333; font-weight:bold;">:c</span> <span style="color:#9966CC; font-weight:bold;">or</span> variable == <span style="color:#ff3333; font-weight:bold;">:d</span> <span style="color:#008000; font-style:italic;"># etc.</span></pre></div></div>

<p>I&#8217;ve previously seen this solution:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:a</span>, <span style="color:#ff3333; font-weight:bold;">:b</span>, <span style="color:#ff3333; font-weight:bold;">:c</span>, <span style="color:#ff3333; font-weight:bold;">:d</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9966CC; font-weight:bold;">include</span>? variable</pre></div></div>

<p>but this isn&#8217;t always functionally equivalent &#8211; I believe Array#include? actually looks to see if the variable object is contained in the list; it doesn&#8217;t take into account that the object may implement its own equality test with def ==(other).</p>
<p>Take, for example, Rails&#8217; <code>Mime::Type implementation: request.format == :html </code>may return true, but <code>[:html].include?(request.format)</code> will return false, as request.format is an instance of Mime::Type, not a symbol.</p>
<p><strong>The answer</strong>:<br />
Actually, #include? does use ==. The problem arises from the fact that if you do <code>[:a].include? foo</code>, it checks <code>:a == foo</code>, not <code>foo == :a</code>. <strong>That is, it uses the == method defined on the objects in the Array, not the variable. Therefore you can use it so long as you make sure the objects in the array have a proper == method.</strong></p>
<p>In cases where that won&#8217;t work, you can simplify your statement by removing the select statement and passing the block directly to any:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:a</span>, <span style="color:#ff3333; font-weight:bold;">:b</span>, <span style="color:#ff3333; font-weight:bold;">:c</span>, <span style="color:#ff3333; font-weight:bold;">:d</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">any</span>? <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>f<span style="color:#006600; font-weight:bold;">|</span> variable == f<span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<h2>Using &#8216;instance_eval&#8217; instead of &#8217;send&#8217;</h2>
<p><strong>The question title is</strong>: <a href="http://stackoverflow.com/questions/1485068/ruby-how-to-evalulate-multiple-methods-per-send-command">How to evaluate multiple methods per send command?</a></p>
<p><strong>The question text is</strong>:</p>
<p>Let&#8217;s say I have an XML::Element&#8230;I want to do something like:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">my_xml_element.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;parent.next_sibling.next_sibling&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p><strong>The answer is:</strong></p>
<p>In your case it&#8217;s better to use instance_eval</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#996600;">&quot;Test&quot;</span>.<span style="color:#9900CC;">instance_eval</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#CC0066; font-weight:bold;">chop!</span>.<span style="color:#CC0066; font-weight:bold;">chop!</span><span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#008000; font-style:italic;">#=&gt; &quot;Te&quot;</span></pre></div></div>

<p>And for your code:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">my_xml_element.<span style="color:#9900CC;">instance_eval</span><span style="color:#006600; font-weight:bold;">&#123;</span>parent.<span style="color:#9900CC;">next_sibling</span>.<span style="color:#9900CC;">next_sibling</span><span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.khelll.com/blog/ruby/stackoverflow-cool-ruby-question/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>
