1= Miscellaneous Syntax
2
3== Ending an Expression
4
5Ruby uses a newline as the end of an expression.  When ending a line with an
6operator, open parentheses, comma, etc. the expression will continue.
7
8You can end an expression with a <code>;</code> (semicolon).  Semicolons are
9most frequently used with <code>ruby -e</code>.
10
11== Indentation
12
13Ruby does not require any indentation.  Typically, ruby programs are indented
14two spaces.
15
16If you run ruby with warnings enabled and have an indentation mis-match, you
17will receive a warning.
18
19== +alias+
20
21The +alias+ keyword is most frequently used to alias methods.  When aliasing a
22method, you can use either its name or a symbol:
23
24  alias new_name old_name
25  alias :new_name :old_name
26
27For methods, Module#alias_method can often be used instead of +alias+.
28
29You can also use +alias+ to alias global variables:
30
31  $old = 0
32
33  alias $new $old
34
35  p $new # prints 0
36
37You may use +alias+ in any scope.
38
39== +undef+
40
41The +undef+ keyword prevents the current class from responding to calls to the
42named methods.
43
44  undef my_method
45
46You may use symbols instead of method names:
47
48  undef :my_method
49
50You may undef multiple methods:
51
52  undef method1, method2
53
54You may use +undef+ in any scope.  See also Module#undef_method
55
56== +defined?+
57
58+defined?+ is a keyword that returns a string describing its argument:
59
60  p defined?(UNDEFINED_CONSTANT) # prints nil
61  p defined?(RUBY_VERSION)       # prints "constant"
62  p defined?(1 + 1)              # prints "method"
63
64You don't need to use parenthesis with +defined?+, but they are recommended due
65to the {low precedence}[rdoc-ref:syntax/precedence.rdoc] of +defined?+.
66
67For example, if you wish to check if an instance variable exists and that the
68instance variable is zero:
69
70  defined? @instance_variable && @instance_variable.zero?
71
72This returns <code>"expression"</code>, which is not what you want if the
73instance variable is not defined.
74
75  @instance_variable = 1
76  defined?(@instance_variable) && @instance_variable.zero?
77
78Adding parentheses when checking if the instance variable is defined is a
79better check.  This correctly returns +nil+ when the instance variable is not
80defined and +false+ when the instance variable is not zero.
81
82Using the specific reflection methods such as instance_variable_defined? for
83instance variables or const_defined? for constants is less error prone than
84using +defined?+.
85
86== +BEGIN+ and +END+
87
88+BEGIN+ defines a block that is run before any other code in the current file.
89It is typically used in one-liners with <code>ruby -e</code>.  Similarly +END+
90defines a block that is run after any other code.
91
92+BEGIN+ must appear at top-level and +END+ will issue a warning when you use it
93inside a method.
94
95Here is an example:
96
97  BEGIN {
98    count = 0
99  }
100
101You must use <code>{</code> and <code>}</code> you may not use +do+ and +end+.
102
103Here is an example one-liner that adds numbers from standard input or any files
104in the argument list:
105
106  ruby -ne 'BEGIN { count = 0 }; END { puts count }; count += gets.to_i'
107