Deviations From Behavior Elsewhere
Some features which differ notably from languages such as C or Perl:
- The language syntax is sensitive to the capitalization of identifiers, in most cases treating capitalized variables as constants.
- The sigils
$
and@
do not indicate variable data type as in Perl, but rather function as scope resolution operators. - To denote a floating point without a decimal component, one must follow with a zero digit (
99.0
) or an explicit conversion (99.to_f
). It is insufficient to append a dot (99.
) since numbers are susceptible to method syntax. - Boolean evaluation of non-boolean data is strict:
0
,""
andare all evaluated to true. In C, the expression
0 ? 1 : 0
evaluates to 0 (i.e. false). In Ruby, however, it yields 1, as all numbers evaluate to true; onlynil
andfalse
evaluate to false. 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, butnil
on failure. This convention is also used in Smalltalk, where only the special objectstrue
andfalse
can be used in a boolean expression. - Versions prior to 1.9 use plain integers to represent single characters, much like C. This may cause surprises when slicing strings:
"abc"
yields 97 (the ASCII code of the first character in the string); to obtain"a"
use"abc"
(a substring of length 1) or"abc".chr
. - The notation
statement until expression
, like Perl but unlike other languages' equivalent statements (e.g.do { statement } while (!(expression));
in C/C++/...), actually never runs the statement if the expression is already true. This is becausestatement until expression
is actually syntactic sugar overuntil expression; statement; end
, the equivalent of which in C/C++ iswhile (!(expression)) { statement; }
, just asstatement if expression
is equivalent toif (expression) { statement; }
. However, the notationbegin statement end until expression
in Ruby will in fact run the statement once even if the expression is already true, acting similar to the "do-while" of other languages. (Matz has expressed a desire to remove the special behavior ofbegin statement end until expression
, but it still exists as of Ruby 1.9.) - Because constants are references to objects, changing what a constant refers to generates a warning, but modifying the object itself does not. For example,
Greeting << " world!" if Greeting == "Hello"
does not generate an error or warning. This is similar to final variables in Java or a const pointer to a non-const object in C++, but Ruby provides the functionality to "freeze" an object, unlike Java.
Some features which differ notably from other languages:
- The usual operators for conditional expressions,
and
andor
, do not follow the normal rules of precedence:and
does not bind tighter thanor
. Ruby also has expression operators||
and&&
which work as expected.
A list of so-called gotchas may be found in Hal Fulton's book The Ruby Way, 2nd ed (ISBN 0-672-32884-4), Section 1.5. A similar list in the 1st edition pertained to an older version of Ruby (version 1.6), some problems of which have been fixed in the meantime. retry
, for example, now works with while
, until
, and for
, as well as iterators.
Read more about this topic: Ruby (programming Language)
Famous quotes containing the word behavior:
“As long as male behavior is taken to be the norm, there can be no serious questioning of male traits and behavior. A norm is by definition a standard for judging; it is not itself subject to judgment.”
—Myriam Miedzian, U.S. author. Boys Will Be Boys, ch. 1 (1991)