Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><a href="http://en.wikipedia.org/wiki/Ruby_(programming_language)#.22Gotchas.22" rel="noreferrer">Wikipedia Ruby gotchas</a></p> <p>From the article:</p> <ul> <li>Names which begin with a capital letter are treated as constants, so local variables should begin with a lowercase letter.</li> <li>The characters <code>$</code> and <code>@</code> do not indicate variable data type as in Perl, but rather function as scope resolution operators.</li> <li>To denote floating point numbers, one must follow with a zero digit (<code>99.0</code>) or an explicit conversion (<code>99.to_f</code>). It is insufficient to append a dot (<code>99.</code>), because numbers are susceptible to method syntax.</li> <li>Boolean evaluation of non-boolean data is strict: <code>0</code>, <code>""</code> and <code>[]</code> are all evaluated to <code>true</code>. In C, the expression <code>0 ? 1 : 0</code> evaluates to <code>0</code> (i.e. false). In Ruby, however, it yields <code>1</code>, as all numbers evaluate to <code>true</code>; only <code>nil</code> and <code>false</code> evaluate to <code>false</code>. A corollary to this rule is that Ruby methods by convention — for example, regular-expression searches — return numbers, strings, lists, or other non-false values on success, but <code>nil</code> on failure (e.g., mismatch). This convention is also used in Smalltalk, where only the special objects <code>true</code> and <code>false</code> can be used in a boolean expression.</li> <li>Versions prior to 1.9 lack a character data type (compare to C, which provides type <code>char</code> for characters). This may cause surprises when slicing strings: <code>"abc"[0]</code> yields <code>97</code> (an integer, representing the ASCII code of the first character in the string); to obtain <code>"a"</code> use <code>"abc"[0,1]</code> (a substring of length 1) or <code>"abc"[0].chr</code>.</li> <li><p>The notation <code>statement until expression</code>, unlike other languages' equivalent statements (e.g. <code>do { statement } while (not(expression));</code> in C/C++/...), actually never runs the statement if the expression is already <code>true</code>. This is because <code>statement until expression</code> is actually syntactic sugar over</p> <pre><code>until expression statement end </code></pre> <p>, the equivalent of which in C/C++ is <code>while (not(expression)) statement;</code> just like <code>statement if expression</code> is an equivalent to</p> <pre><code>if expression statement end </code></pre> <p>However, the notation</p> <pre><code>begin statement end until expression </code></pre> <p>in Ruby will in fact run the statement once even if the expression is already true.</p></li> <li>Because constants are references to objects, changing what a constant refers to generates a warning, but modifying the object itself does not. For example, <code>Greeting &lt;&lt; " world!" if Greeting == "Hello"</code> does not generate an error or warning. This is similar to <code>final</code> variables in Java, but Ruby does also have the functionality to "freeze" an object, unlike Java.</li> </ul> <p>Some features which differ notably from other languages:</p> <ul> <li><p>The usual operators for conditional expressions, <code>and</code> and <code>or</code>, do not follow the normal rules of precedence: <code>and</code> does not bind tighter than <code>or</code>. Ruby also has expression operators <code>||</code> and <code>&amp;&amp;</code> which work as expected.</p></li> <li><p><code>def</code> inside <code>def</code> doesn't do what a Python programmer might expect:</p> <pre><code>def a_method x = 7 def print_x; puts x end print_x end </code></pre> <p>This gives an error about <code>x</code> not being defined. You need to use a <code>Proc</code>.</p></li> </ul> <p>Language features</p> <ul> <li>Omission of parentheses around method arguments may lead to unexpected results if the methods take multiple parameters. The Ruby developers have stated that omission of parentheses on multi-parameter methods may be disallowed in future Ruby versions; the current (November 2007) Ruby interpreter throws a warning which encourages the writer not to omit <code>()</code>, to avoid ambiguous meaning of code. Not using <code>()</code> is still common practice, and can be especially nice to use Ruby as a human readable domain-specific programming language itself, along with the method called <code>method_missing()</code>.</li> </ul>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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