2013-01-16 18:36:46 -05:00
|
|
|
= Miscellaneous Syntax
|
|
|
|
|
2013-01-16 20:21:28 -05:00
|
|
|
== 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
|
|
|
|
|
2015-12-17 22:39:49 -05:00
|
|
|
Ruby does not require any indentation. Typically, ruby programs are indented
|
2013-01-16 20:21:28 -05:00
|
|
|
two spaces.
|
|
|
|
|
2019-12-19 19:19:39 -05:00
|
|
|
If you run ruby with warnings enabled and have an indentation mismatch, you
|
2013-01-16 20:21:28 -05:00
|
|
|
will receive a warning.
|
|
|
|
|
2013-01-16 18:36:46 -05:00
|
|
|
== +alias+
|
|
|
|
|
|
|
|
The +alias+ keyword is most frequently used to alias methods. When aliasing a
|
2015-12-17 22:39:49 -05:00
|
|
|
method, you can use either its name or a symbol:
|
2013-01-16 18:36:46 -05:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2013-01-18 19:52:57 -05:00
|
|
|
== +defined?+
|
|
|
|
|
|
|
|
+defined?+ is a keyword that returns a string describing its argument:
|
|
|
|
|
|
|
|
p defined?(UNDEFINED_CONSTANT) # prints nil
|
|
|
|
p defined?(RUBY_VERSION) # prints "constant"
|
|
|
|
p defined?(1 + 1) # prints "method"
|
|
|
|
|
2015-12-17 22:39:49 -05:00
|
|
|
You don't need to use parenthesis with +defined?+, but they are recommended due
|
2013-01-18 19:52:57 -05:00
|
|
|
to the {low precedence}[rdoc-ref:syntax/precedence.rdoc] of +defined?+.
|
|
|
|
|
|
|
|
For example, if you wish to check if an instance variable exists and that the
|
|
|
|
instance variable is zero:
|
|
|
|
|
|
|
|
defined? @instance_variable && @instance_variable.zero?
|
|
|
|
|
2015-12-17 22:39:49 -05:00
|
|
|
This returns <code>"expression"</code>, which is not what you want if the
|
2013-01-18 19:52:57 -05:00
|
|
|
instance variable is not defined.
|
|
|
|
|
|
|
|
@instance_variable = 1
|
|
|
|
defined?(@instance_variable) && @instance_variable.zero?
|
|
|
|
|
|
|
|
Adding parentheses when checking if the instance variable is defined is a
|
|
|
|
better check. This correctly returns +nil+ when the instance variable is not
|
|
|
|
defined and +false+ when the instance variable is not zero.
|
|
|
|
|
|
|
|
Using the specific reflection methods such as instance_variable_defined? for
|
|
|
|
instance variables or const_defined? for constants is less error prone than
|
|
|
|
using +defined?+.
|
|
|
|
|
2020-03-06 16:03:58 -05:00
|
|
|
+defined?+ handles some regexp global variables specially based on whether
|
|
|
|
there is an active regexp match and how many capture groups there are:
|
|
|
|
|
|
|
|
/b/ =~ 'a'
|
|
|
|
defined?($~) # => "global-variable"
|
|
|
|
defined?($&) # => nil
|
|
|
|
defined?($`) # => nil
|
|
|
|
defined?($') # => nil
|
|
|
|
defined?($+) # => nil
|
|
|
|
defined?($1) # => nil
|
|
|
|
defined?($2) # => nil
|
|
|
|
|
|
|
|
/./ =~ 'a'
|
|
|
|
defined?($~) # => "global-variable"
|
|
|
|
defined?($&) # => "global-variable"
|
|
|
|
defined?($`) # => "global-variable"
|
|
|
|
defined?($') # => "global-variable"
|
|
|
|
defined?($+) # => nil
|
|
|
|
defined?($1) # => nil
|
|
|
|
defined?($2) # => nil
|
|
|
|
|
|
|
|
/(.)/ =~ 'a'
|
|
|
|
defined?($~) # => "global-variable"
|
|
|
|
defined?($&) # => "global-variable"
|
|
|
|
defined?($`) # => "global-variable"
|
|
|
|
defined?($') # => "global-variable"
|
|
|
|
defined?($+) # => "global-variable"
|
|
|
|
defined?($1) # => "global-variable"
|
|
|
|
defined?($2) # => nil
|
|
|
|
|
2013-01-16 18:36:46 -05:00
|
|
|
== +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'
|