Note that there are some explanatory texts on larger screens.

plurals
  1. POConfusion with `nil` output when using `Symbol#<=>` in Ruby
    text
    copied!<p><a href="http://www.ruby-doc.org/core-2.0/Symbol.html#method-i-3C-3D-3E" rel="nofollow"><strong><em><code>Symbol#&lt;=&gt;</code></em></strong></a> </p> <blockquote> <p>simply says: Compares symbol with other_symbol after calling to_s on each of the symbols. Returns -1, 0, +1 or nil depending on whether symbol is less than, equal to, or greater than other_symbol. <strong><code>nil</code> is returned if the two values are incomparable.</strong></p> </blockquote> <p>I was trying to understand how <code>Symbol#&lt;=&gt;</code> works when returning <code>nil</code>. Doing so I played with the code:</p> <pre><code>&gt;&gt; :x.to_s =&gt; "x" &gt;&gt; 'x'.to_s =&gt; "x" </code></pre> <p>From the above <code>IRB</code> code I thought the return value will be <code>0</code>. But the actual is <code>nil</code>. As doc says that before using <code>&lt;=&gt;</code> operator <code>to_s</code> is applied both the <code>RHO</code> and <code>LHO</code>. But here the below code is not supporting that principle, seems to me.</p> <pre><code>&gt;&gt; :x &lt;=&gt; "x" #=&gt; nil </code></pre> <p>So I tried to see the source code,to answer myself:</p> <pre><code>static VALUE sym_cmp(VALUE sym, VALUE other) { if (!SYMBOL_P(other)) { return Qnil; } return rb_str_cmp_m(rb_sym_to_s(sym), rb_sym_to_s(other)); } </code></pre> <p>Looking at the source code it is clear that if <code>RHO</code> is not the object of class <code>Symbol</code>, <code>nil</code> will be returned. Let's see the something more in IRB:</p> <pre><code>&gt;&gt; "x" &lt;=&gt; :x #=&gt; nil </code></pre> <p>Again <code>nil</code>. The source code saying that <code>rb_str_cmp_m(rb_sym_to_s(sym),rb_sym_to_s(other))</code> will be executed now. So now I went to the see the source code of <a href="https://github.com/ruby/ruby/blob/trunk/string.c" rel="nofollow"><strong><em><code>STRING.C</code></em></strong></a>. So we are basically pasing the <code>rb_str_cmp_m(???,"x")</code>. Now the I found from github:(<code>?</code> means don't know what value)</p> <pre><code>rb_str_cmp_m(VALUE str1, VALUE str2) { int result; if (!RB_TYPE_P(str2, T_STRING)) { VALUE tmp = rb_check_funcall(str2, rb_intern("to_str"), 0, 0); if (RB_TYPE_P(tmp, T_STRING)) { result = rb_str_cmp(str1, tmp); } else { return rb_invcmp(str1, str2); } } else { result = rb_str_cmp(str1, str2); } return INT2FIX(result); } </code></pre> <p>But the above code I couldn't understand.But I beleieve it has the answer how <code>nil</code> is producing when <code>LHO</code> is not the object of class <code>Symbol</code>.</p> <p>Can anyone help me here to understand how the <code>nil</code> is coming when <code>LHO</code> is not <code>sym</code>?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload