mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
66013dc6d0
indentation. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@38856 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
77 lines
1.8 KiB
Text
77 lines
1.8 KiB
Text
= Miscellaneous Syntax
|
|
|
|
== Ending an Expression
|
|
|
|
Ruby uses a newline as the end of an expression. When ending a line with an
|
|
operator, open parentheses, comma, etc. the expression will continue.
|
|
|
|
You can end an expression with a <code>;</code> (semicolon). Semicolons are
|
|
most frequently used with <code>ruby -e</code>.
|
|
|
|
== Indentation
|
|
|
|
Ruby does not require any indentation. Typically ruby programs are indented
|
|
two spaces.
|
|
|
|
If you run ruby with warnings enabled and have an indentation mis-match you
|
|
will receive a warning.
|
|
|
|
== +alias+
|
|
|
|
The +alias+ keyword is most frequently used to alias methods. When aliasing a
|
|
method you can use either its name or a symbol:
|
|
|
|
alias new_name old_name
|
|
alias :new_name :old_name
|
|
|
|
For methods, Module#alias_method can often be used instead of +alias+.
|
|
|
|
You can also use +alias+ to alias global variables:
|
|
|
|
$old = 0
|
|
|
|
alias $new $old
|
|
|
|
p $new # prints 0
|
|
|
|
You may use +alias+ in any scope.
|
|
|
|
== +undef+
|
|
|
|
The +undef+ keyword prevents the current class from responding to calls to the
|
|
named methods.
|
|
|
|
undef my_method
|
|
|
|
You may use symbols instead of method names:
|
|
|
|
undef :my_method
|
|
|
|
You may undef multiple methods:
|
|
|
|
undef method1, method2
|
|
|
|
You may use +undef+ in any scope. See also Module#undef_method
|
|
|
|
== +BEGIN+ and +END+
|
|
|
|
+BEGIN+ defines a block that is run before any other code in the current file.
|
|
It is typically used in one-liners with <code>ruby -e</code>. Similarly +END+
|
|
defines a block that is run after any other code.
|
|
|
|
+BEGIN+ must appear at top-level and +END+ will issue a warning when you use it
|
|
inside a method.
|
|
|
|
Here is an example:
|
|
|
|
BEGIN {
|
|
count = 0
|
|
}
|
|
|
|
You must use <code>{</code> and <code>}</code> you may not use +do+ and +end+.
|
|
|
|
Here is an example one-liner that adds numbers from standard input or any files
|
|
in the argument list:
|
|
|
|
ruby -ne 'BEGIN { count = 0 }; END { puts count }; count += gets.to_i'
|
|
|