Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Symbol names and method names are not linked in any way. A symbol is most closely related to a string. The difference between a string and a symbol is that a symbol is a unique object for the entire Ruby interpreter whereas strings are always unique objects. To illustrate this, open up IRB:</p> <pre><code>irb 1.9.3-p362 :001 &gt; "hello".object_id =&gt; 70162149682220 1.9.3-p362 :002 &gt; "hello".object_id =&gt; 70162149699280 1.9.3-p362 :003 &gt; :hello.object_id =&gt; 460328 1.9.3-p362 :004 &gt; :hello.object_id =&gt; 460328 1.9.3-p362 :005 &gt; "hello".to_sym.object_id =&gt; 460328 </code></pre> <p>As you can see, two identical strings containing "hello" have different object ids, even though they contain the same contents. Symbols on the other hand, always have the same object id as long as the contents of the symbol are the same. You can see that converting the string "hello" to a symbol gives the same object id as the symbol <code>:hello</code>. Symbols are much more memory efficient to use, and equality comparison is much faster. The trade off is that a symbol is essentially a memory leak, because you can never remove one from memory once you define it. </p> <p>As Sergio said, you can invoke methods using the <code>send</code> method, but <code>send</code> uses Ruby's robust reflection to execute methods by name at runtime, it has nothing inherently to do with symbols other than the method names are identified by a symbol.</p> <p>EDIT: If you are curious about how this works, check out <a href="http://ruby-metaprogramming.rubylearning.com/html/ruby_metaprogramming_2.html" rel="nofollow">Ruby Metaprogramming</a> to see the things you are able to do. To understand exactly how this works, however, you would need to look at the C source code of the Ruby language. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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