|
|||
Previous < |
Contents ^
|
Next >
|
KCODE
option must be
set appropriately, as shown on page 137.]
Ruby is a line-oriented language. Ruby expressions and statements are
terminated at the end of a line unless the statement is obviously
incomplete---for example if the last token on a line is an operator or
comma.
A semicolon can be used to separate
multiple expressions on a line. You can also put a backslash at the
end of a line to continue it onto the next. Comments start
with `#' and run to the end of the
physical line. Comments are ignored during compilation.
a = 1 b = 2; c = 3 d = 4 + 5 + # no '\' needed 6 + 7 e = 8 + 9 \ + 10 # '\' needed |
{=begin documentation}
a line starting with =end are
ignored by the compiler and may be used for embedded documentation
(see Appendix A, which begins on page 511).
Ruby reads its program input in a single pass, so you can pipe
programs to the compiler's stdin
.
echo 'print "Hello\n"' | ruby |
__END__
'',
with no leading or trailing whitespace, it
treats that line as the end of the program---any subsequent lines will not be
compiled. However, these lines can be read into the running program
using the global IO
object DATA
, described
on page 217.
BEGIN
blocks) and after the program
has finished executing (the END
blocks).
BEGIN { begin code } END { end code } |
BEGIN
and END
blocks.
BEGIN
blocks are executed in the order they are encountered.
END
blocks are executed in reverse order.
General delimited input
|
(
'',
``[
'', ``{
'', or ``<
'', the literal consists of the
characters up to the matching closing delimiter, taking account of
nested delimiter pairs. For all other delimiters, the literal
comprises the characters up to the next occurrence of the delimiter
character.
%q/this is a string/ %q-string- %q(a (nested) string) |
%q{def fred(a) a.each { |i| puts i } end} |
Fixnum
or Bignum
.
Fixnum
objects
hold integers that fit within the native machine word minus 1 bit.
Whenever a Fixnum
exceeds this range, it is automatically converted
to a Bignum
object, whose range is effectively limited only
by available memory. If an operation with a Bignum
result
has a final value that will fit in a Fixnum
, the result will
be returned as a Fixnum
.
Integers are written using an optional leading sign, an optional base
indicator (0
for octal, 0x
for hex, or 0b
for binary), followed by a string of digits in the appropriate base.
Underscore characters are ignored in the digit string.
You can get the integer value corresponding to an ASCII
character by preceding that character with a question mark. Control
and meta combinations of characters can also be generated using
?\C-x, ?\M-x, and ?\M-\C-x.
The control version of ch
is ch&0x9f
, and the meta
version is ch | 0x80
. You can get the integer value of a backslash
character using the sequence ?\\
.
123456 # Fixnum 123_456 # Fixnum (underscore ignored) -543 # Negative Fixnum 123_456_789_123_345_789 # Bignum 0xaabb # Hexadecimal 0377 # Octal -0b1010 # Binary (negated) 0b001_001 # Binary ?a # character code ?A # upper case ?\C-a # control a = A - 0x40 ?\C-A # case ignored for control chars ?\M-a # meta sets bit 7 ?\M-\C-a # meta and control a |
Float
object, corresponding to the native
architecture's double
data type. You must
follow the decimal point with a digit, as
1.e3
tries to invoke the method e3
in class Fixnum
.
12.34
|
» |
12.34
|
-.1234e2
|
» |
-12.34
|
1234e-2
|
» |
12.34
|
String
. The different
mechanisms vary in terms of how a string is delimited and how much
substitution is done on the literal's content.
Single-quoted string literals ('
stuff
'
and
%q/stuff/)
undergo the least substitution.
Both convert
the sequence
'hello'
|
» |
hello
|
'a backslash \'\\\''
|
» |
a backslash '\'
|
%q/simple string/
|
» |
simple string
|
%q(nesting (really) works)
|
» |
nesting (really) works
|
%q no_blanks_here ;
|
» |
no_blanks_here
|
Substitutions in double-quoted
strings
|
a = 123
|
||
"\123mile"
|
» |
Smile
|
"Say \"Hello\""
|
» |
Say "Hello"
|
%Q!"I said 'nuts'," I said!
|
» |
"I said 'nuts'," I said
|
%Q{Try #{a + 1}, not #{a - 1}}
|
» |
Try 124, not 122
|
%<Try #{a + 1}, not #{a - 1}>
|
» |
Try 124, not 122
|
"Try #{a + 1}, not #{a - 1}"
|
» |
Try 124, not 122
|
a = 123 print <<HERE Double quoted \ here document. Sum = #{a + 1} HERE print <<-'THERE' This is single quoted. The above used #{a + 1} THERE |
Double quoted here document. Sum = 124 This is single quoted. The above used #{a + 1} |
String
object.
'Con' "cat" 'en' "ate"
|
» |
"Concatenate"
|
jcode
library supports a set of operations of
strings written with EUC, SJIS, or UTF-8
encoding.
The underlying string, however, is still accessed as a
series of bytes.] and each byte may contain any of the 256 8-bit
values, including null and newline.
The substitution mechanisms in
Table 18.2 on page 203 allow nonprinting characters to be
inserted conveniently and portably.
Every time a string literal is used in an assignment or as a
parameter, a new String
object is created.
for i in 1..3 print 'hello'.id, " " end |
537767360 537767070 537767040 |
String
starts on page 363.
..
expr and expr
...
expr
construct Range
objects.
The two-dot form is an inclusive range;
the one with three dots is a range that excludes its last element.
See the description of class Range
on page 359 for
details. Also see the description of conditional expressions
on page 222 for other uses of ranges.
Array
are created by placing a comma-separated
series of object references between square brackets. A trailing comma
is ignored.
arr = [ fred, 10, 3.14, "This is a string", barney("pebbles"), ] |
%w
,
which extracts space-separated tokens into successive
elements of the array. A space can be escaped with a backslash.
This is a form of general delimited input,
described on pages 200--201.
arr = %w( fred wilma barney betty great\ gazoo )
|
||
arr
|
» |
["fred", "wilma", "barney", "betty", "great gazoo"]
|
Hash
is created by placing a list of key/value
pairs between braces, with either a comma or the sequence =>
between the key and the value. A trailing comma is ignored.
colors = { "red" => 0xf00, "green" => 0x0f0, "blue" => 0x00f } |
hash
with a hash value, and the hash value for a
given key must not change.
This means that certain classes (such as
Array
and Hash
, as of this writing) can't conveniently be used
as keys, because their hash values can change based on their contents.
If you keep an external reference to an object that is used as a key,
and use that reference to alter the object and change its hash value,
the hash lookup based on that key may not work.
Because strings are the most frequently used keys, and because string
contents are often changed, Ruby treats string keys specially. If you
use a String
object as a hash key, the hash will duplicate the
string internally and will use that copy as its key. Any changes
subsequently made to the original string will not affect the hash.
If you write your own classes and use instances of them as hash keys, you
need to make sure that either (a) the hashes of the key objects
don't change once the objects have been created or (b) you remember
to call the
Hash#rehash
method to reindex the hash whenever a
key hash is changed.
:Object :myVariable |
Regexp
. They can
be created by explicitly calling the
Regexp.new
constructor, or
by using the literal forms, /pattern/ and
%r{
pattern
}
. The %r
construct is
a form of general delimited input (described
on pages 200--201).
/pattern/ /pattern/options %r{pattern} %r{pattern}options Regexp.new( 'pattern' [, options ] ) |
Regexp
object, then the options comprise one or more characters placed
immediately after the terminator. If you're using Regexp.new
, the
options are constants used as the second parameter of the constructor.
i
|
Case Insensitive. The pattern match will ignore
the case of letters in the pattern and string. Matches are also
case-insensitive if the global variable $= is set. |
o
|
Substitute Once. Any #{...} substitutions
in
a particular regular expression literal will be performed just once,
the first time it is evaluated. Otherwise, the substitutions
will be performed every time the literal generates a Regexp object. |
m
|
Multiline Mode. Normally, ``.'' matches any
character except a newline. With the /m option, ``.'' matches
any character. |
x
|
Extended Mode. Complex regular expressions can be difficult to read. The `x' option allows you to insert spaces, newlines, and comments in the pattern to make it more readable. |
^
$
\A
\z
\Z
\b
, \B
[
characters
]
|, (, ), [, ^, $, *,
and ?
,
which have special meanings elsewhere in patterns, lose their
special significance between brackets. The sequences
\
nnn, \x
nn, \c
x, \C-
x, \M-
x, and \M-\C-
x
have the meanings shown in Table 18.2 on page 203. The
sequences \d
, \D
, \s
, \S
, \w
, and \W
are abbreviations for groups of characters, as
shown in Table 5.1 on page 59. The sequence c1-c2
represents all the characters between c1 and c2, inclusive.
Literal ]
or -
characters must appear immediately after
the opening bracket. An uparrow (^) immediately following the
opening bracket negates the sense of the match---the pattern matches
any character that isn't in the character class.
\d
, \s
, \w
.
(period)
/m
option, it matches newline, too).
*
+
{m,n}
?
*
, +
, and {m,n}
modifiers are greedy by
default. Append a question mark to make them minimal.
|
re2
|
has a low
precedence.
(...)
/abc+/
matches a string containing an ``a,'' a ``b,''
and one or more ``c''s. /(abc)+/
matches one or more sequences
of ``abc''. Parentheses are also used to collect the results of
pattern matching. For each opening parenthesis, Ruby stores the
result of the partial match between it and the corresponding closing
parenthesis as successive groups. Within the same pattern,
\1
refers to the match of the first group, \2
the
second group, and so on. Outside the pattern, the special variables
$1
, $2
, and so on, serve the same purpose.
#{...}
/o
option, it is performed just the first
time.
\0, \1, \2, ... \9, \&, \`, \', \+
(?
and )
. The parentheses
that bracket these extensions are groups, but they do not generate
backreferences: they do not set the values of \1
and $1
etc.
(?# comment)
(?:re)
$1
or whatever. In the
example that follows, both patterns match a date with either colons
or spaces between the month, day, and year. The first form stores
the separator character in $2
and $4
, while the second
pattern doesn't store the separator in an external variable.
date = "12/25/01"
|
||
date =~ %r{(\d+)(/|:)(\d+)(/|:)(\d+)}
|
||
[$1,$2,$3,$4,$5]
|
» |
["12", "/", "25", "/", "01"]
|
date =~ %r{(\d+)(?:/|:)(\d+)(?:/|:)(\d+)}
|
||
[$1,$2,$3]
|
» |
["12", "25", "01"]
|
(?=re)
$&
. In this example, the scan
method matches words
followed by a comma, but the commas are not included in the result.
str = "red, white, and blue"
|
||
str.scan(/[a-z]+(?=,)/)
|
» |
["red", "white"]
|
(?!re)
/hot(?!dog)(\w+)/
matches any word that contains the
letters ``hot'' that aren't followed by ``dog'', returning the end
of the word in $1
.
(?>re)
/a.*b.*a/
takes exponential time when matched
against a string containing an ``a'' followed by a number of ``b''s,
but with no trailing ``a.'' However, this can be avoided by using a
nested regular expression /a(?>.*b).*a/
. In this form, the
nested expression consumes all the the input string up to the last
possible ``b'' character. When the check for a trailing ``a'' then
fails, there is no need to backtrack, and the pattern match fails promptly.
require "benchmark" include Benchmark str = "a" + ("b" * 5000) bm(8) do |test| test.report("Normal:") { str =~ /a.*b.*a/ } test.report("Nested:") { str =~ /a(?>.*b).*a/ } end |
user system total real Normal: 0.420000 0.000000 0.420000 ( 0.414843) Nested: 0.000000 0.000000 0.000000 ( 0.001205) |
(?imx)
(?-imx)
(?imx:re)
(?-imx:re)
Reserved words
|
fred anObject _x three_two_one |
@
'') followed by an
upper- or lowercase letter, optionally followed by name
characters.
@name @_ @Size |
@@
'')
followed by an upper- or lowercase letter, optionally followed by name
characters.
@@name @@_ @@Size |
module Math PI = 3.1415926 end class BigBlob |
$
'') followed by name characters. In addition,
there is a set of two-character variable names in which the second
character is a punctuation character. These predefined variables are
listed starting on page 213. Finally, a global variable name
can be formed using ``$-
'' followed by any single character.
$params $PROGRAM $! $_ $-a $-. |
def a print "Function 'a' called\n" 99 end for i in 1..2 if i == 2 print "a=", a, "\n" else a = 1 print "a=", a, "\n" end end |
a=1 Function 'a' called a=99 |
a = 1 if false; a |
MY_CONST = 1 MY_CONST = 2 # generates a warning |
prog.rb:2: warning: already initialized constant MY_CONST |
MY_CONST = "Tim"
|
||
MY_CONST[0] = "J" # alter string referenced by constant
|
||
MY_CONST
|
» |
"Jim"
|
::
'' prefixed by an
expression that returns the appropriate class or module object.
Constants defined outside any class or module may be accessed
unadorned or by using the scope operator ``::
'' with no prefix. Constants may not
be defined in methods.
OUTER_CONST = 99
|
||
class Const
|
||
def getConst
|
||
CONST
|
||
end
|
||
CONST = OUTER_CONST + 1
|
||
end
|
||
Const.new.getConst
|
» |
100
|
Const::CONST
|
» |
100
|
::OUTER_CONST
|
» |
99
|
nil
.
Class variables are available throughout a class or module body. Class
variables must be initialized before use. A class variable is shared
among all instances of a class and is available within the class
itself.
class Song @@count = 0 def initialize @@count += 1 end def Song.getCount @@count end end |
Object
, and behave like global variables. Class variables defined
within singleton methods belong to the receiver if the receiver is a
class or a module; otherwise, they belong to the class of the receiver.
class Holder @@var = 99 def Holder.var=(val) @@var = val end end a = Holder.new def a.var @@var end |
Previous < |
Contents ^
|
Next >
|