File: C:/Ruby27-x64/share/doc/ruby/html/syntax/miscellaneous_rdoc.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>miscellaneous - RDoc Documentation</title>
<script type="text/javascript">
var rdoc_rel_prefix = "../";
var index_rel_prefix = "../";
</script>
<script src="../js/navigation.js" defer></script>
<script src="../js/search.js" defer></script>
<script src="../js/search_index.js" defer></script>
<script src="../js/searcher.js" defer></script>
<script src="../js/darkfish.js" defer></script>
<link href="../css/fonts.css" rel="stylesheet">
<link href="../css/rdoc.css" rel="stylesheet">
<body id="top" role="document" class="file">
<nav role="navigation">
<div id="project-navigation">
<div id="home-section" role="region" title="Quick navigation" class="nav-section">
<h2>
<a href="../index.html" rel="home">Home</a>
</h2>
<div id="table-of-contents-navigation">
<a href="../table_of_contents.html#pages">Pages</a>
<a href="../table_of_contents.html#classes">Classes</a>
<a href="../table_of_contents.html#methods">Methods</a>
</div>
</div>
<div id="search-section" role="search" class="project-section initially-hidden">
<form action="#" method="get" accept-charset="utf-8">
<div id="search-field-wrapper">
<input id="search-field" role="combobox" aria-label="Search"
aria-autocomplete="list" aria-controls="search-results"
type="text" name="search" placeholder="Search" spellcheck="false"
title="Type to search, Up and Down to navigate, Enter to load">
</div>
<ul id="search-results" aria-label="Search Results"
aria-busy="false" aria-expanded="false"
aria-atomic="false" class="initially-hidden"></ul>
</form>
</div>
</div>
<div class="nav-section">
<h3>Table of Contents</h3>
<ul class="link-list" role="directory">
<li><a href="#label-Miscellaneous+Syntax">Miscellaneous Syntax</a>
<li><a href="#label-Ending+an+Expression">Ending an Expression</a>
<li><a href="#label-Indentation">Indentation</a>
<li><a href="#label-alias"><code>alias</code></a>
<li><a href="#label-undef"><code>undef</code></a>
<li><a href="#label-defined-3F"><code>defined?</code></a>
<li><a href="#label-BEGIN+and+END"><code>BEGIN</code> and <code>END</code></a>
</ul>
</div>
<div id="project-metadata">
<div id="fileindex-section" class="nav-section">
<h3>Pages</h3>
<ul class="link-list">
<li><a href="../CONTRIBUTING_md.html">CONTRIBUTING</a>
<li><a href="../COPYING.html">COPYING</a>
<li><a href="../COPYING_ja.html">COPYING.ja</a>
<li><a href="../LEGAL.html">LEGAL</a>
<li><a href="../NEWS.html">NEWS</a>
<li><a href="../NEWS-1_8_7.html">NEWS-1.8.7</a>
<li><a href="../NEWS-1_9_1.html">NEWS-1.9.1</a>
<li><a href="../NEWS-1_9_2.html">NEWS-1.9.2</a>
<li><a href="../NEWS-1_9_3.html">NEWS-1.9.3</a>
<li><a href="../NEWS-2_0_0.html">NEWS-2.0.0</a>
<li><a href="../NEWS-2_1_0.html">NEWS-2.1.0</a>
<li><a href="../NEWS-2_2_0.html">NEWS-2.2.0</a>
<li><a href="../NEWS-2_3_0.html">NEWS-2.3.0</a>
<li><a href="../NEWS-2_4_0.html">NEWS-2.4.0</a>
<li><a href="../NEWS-2_5_0.html">NEWS-2.5.0</a>
<li><a href="../NEWS-2_6_0.html">NEWS-2.6.0</a>
<li><a href="../README_ja_md.html">README.ja</a>
<li><a href="../README_md.html">README</a>
<li><a href="../bug_triaging_rdoc.html">bug_triaging</a>
<li><a href="../contributing_rdoc.html">contributing</a>
<li><a href="../contributors_rdoc.html">contributors</a>
<li><a href="../dtrace_probes_rdoc.html">dtrace_probes</a>
<li><a href="../extension_ja_rdoc.html">extension.ja</a>
<li><a href="../extension_rdoc.html">extension</a>
<li><a href="../globals_rdoc.html">globals</a>
<li><a href="../keywords_rdoc.html">keywords</a>
<li><a href="../lib/racc/pre-setup.html">pre-setup</a>
<li><a href="../lib/racc/rdoc/grammar_en_rdoc.html">grammar.en</a>
<li><a href="../maintainers_rdoc.html">maintainers</a>
<li><a href="../marshal_rdoc.html">marshal</a>
<li><a href="../regexp_rdoc.html">regexp</a>
<li><a href="../security_rdoc.html">security</a>
<li><a href="../signals_rdoc.html">signals</a>
<li><a href="../standard_library_rdoc.html">standard_library</a>
<li><a href="../syntax_rdoc.html">syntax</a>
<li><a href="../syntax/assignment_rdoc.html">assignment</a>
<li><a href="../syntax/calling_methods_rdoc.html">calling_methods</a>
<li><a href="../syntax/comments_rdoc.html">comments</a>
<li><a href="../syntax/control_expressions_rdoc.html">control_expressions</a>
<li><a href="../syntax/exceptions_rdoc.html">exceptions</a>
<li><a href="../syntax/literals_rdoc.html">literals</a>
<li><a href="../syntax/methods_rdoc.html">methods</a>
<li><a href="../syntax/miscellaneous_rdoc.html">miscellaneous</a>
<li><a href="../syntax/modules_and_classes_rdoc.html">modules_and_classes</a>
<li><a href="../syntax/precedence_rdoc.html">precedence</a>
<li><a href="../syntax/refinements_rdoc.html">refinements</a>
<li><a href="../win32/README_win32.html">README.win32</a>
</ul>
</div>
</div>
</nav>
<main role="main" aria-label="Page syntax/miscellaneous.rdoc">
<h1 id="label-Miscellaneous+Syntax">Miscellaneous Syntax<span><a href="#label-Miscellaneous+Syntax">¶</a> <a href="#top">↑</a></span></h1>
<h2 id="label-Ending+an+Expression">Ending an Expression<span><a href="#label-Ending+an+Expression">¶</a> <a href="#top">↑</a></span></h2>
<p>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.</p>
<p>You can end an expression with a <code>;</code> (semicolon). Semicolons are most frequently used with <code>ruby -e</code>.</p>
<h2 id="label-Indentation">Indentation<span><a href="#label-Indentation">¶</a> <a href="#top">↑</a></span></h2>
<p>Ruby does not require any indentation. Typically, ruby programs are indented two spaces.</p>
<p>If you run ruby with warnings enabled and have an indentation mismatch, you will receive a warning.</p>
<h2 id="label-alias"><code>alias</code><span><a href="#label-alias">¶</a> <a href="#top">↑</a></span></h2>
<p>The <code>alias</code> keyword is most frequently used to alias methods. When aliasing a method, you can use either its name or a symbol:</p>
<pre class="ruby"><span class="ruby-keyword">alias</span> <span class="ruby-identifier">new_name</span> <span class="ruby-identifier">old_name</span>
<span class="ruby-keyword">alias</span> <span class="ruby-value">:new_name</span> <span class="ruby-value">:old_name</span>
</pre>
<p>For methods, <a href="../Module.html#method-i-alias_method"><code>Module#alias_method</code></a> can often be used instead of <code>alias</code>.</p>
<p>You can also use <code>alias</code> to alias global variables:</p>
<pre class="ruby"><span class="ruby-identifier">$old</span> = <span class="ruby-value">0</span>
<span class="ruby-keyword">alias</span> <span class="ruby-identifier">$new</span> <span class="ruby-identifier">$old</span>
<span class="ruby-identifier">p</span> <span class="ruby-identifier">$new</span> <span class="ruby-comment"># prints 0</span>
</pre>
<p>You may use <code>alias</code> in any scope.</p>
<h2 id="label-undef"><code>undef</code><span><a href="#label-undef">¶</a> <a href="#top">↑</a></span></h2>
<p>The <code>undef</code> keyword prevents the current class from responding to calls to the named methods.</p>
<pre class="ruby"><span class="ruby-keyword">undef</span> <span class="ruby-identifier">my_method</span>
</pre>
<p>You may use symbols instead of method names:</p>
<pre class="ruby"><span class="ruby-keyword">undef</span> <span class="ruby-value">:my_method</span>
</pre>
<p>You may undef multiple methods:</p>
<pre class="ruby"><span class="ruby-keyword">undef</span> <span class="ruby-identifier">method1</span>, <span class="ruby-identifier">method2</span>
</pre>
<p>You may use <code>undef</code> in any scope. See also <a href="../Module.html#method-i-undef_method"><code>Module#undef_method</code></a></p>
<h2 id="label-defined-3F"><code>defined?</code><span><a href="#label-defined-3F">¶</a> <a href="#top">↑</a></span></h2>
<p><code>defined?</code> is a keyword that returns a string describing its argument:</p>
<pre class="ruby"><span class="ruby-identifier">p</span> <span class="ruby-keyword">defined?</span>(<span class="ruby-constant">UNDEFINED_CONSTANT</span>) <span class="ruby-comment"># prints nil</span>
<span class="ruby-identifier">p</span> <span class="ruby-keyword">defined?</span>(<span class="ruby-constant">RUBY_VERSION</span>) <span class="ruby-comment"># prints "constant"</span>
<span class="ruby-identifier">p</span> <span class="ruby-keyword">defined?</span>(<span class="ruby-value">1</span> <span class="ruby-operator">+</span> <span class="ruby-value">1</span>) <span class="ruby-comment"># prints "method"</span>
</pre>
<p>You don't need to use parenthesis with <code>defined?</code>, but they are recommended due to the <a href="precedence_rdoc.html">low precedence</a> of <code>defined?</code>.</p>
<p>For example, if you wish to check if an instance variable exists and that the instance variable is zero:</p>
<pre class="ruby"><span class="ruby-keyword">defined?</span> <span class="ruby-ivar">@instance_variable</span> <span class="ruby-operator">&&</span> <span class="ruby-ivar">@instance_variable</span>.<span class="ruby-identifier">zero?</span>
</pre>
<p>This returns <code>"expression"</code>, which is not what you want if the instance variable is not defined.</p>
<pre class="ruby"><span class="ruby-ivar">@instance_variable</span> = <span class="ruby-value">1</span>
<span class="ruby-keyword">defined?</span>(<span class="ruby-ivar">@instance_variable</span>) <span class="ruby-operator">&&</span> <span class="ruby-ivar">@instance_variable</span>.<span class="ruby-identifier">zero?</span>
</pre>
<p>Adding parentheses when checking if the instance variable is defined is a better check. This correctly returns <code>nil</code> when the instance variable is not defined and <code>false</code> when the instance variable is not zero.</p>
<p>Using the specific reflection methods such as instance_variable_defined? for instance variables or const_defined? for constants is less error prone than using <code>defined?</code>.</p>
<h2 id="label-BEGIN+and+END"><code>BEGIN</code> and <code>END</code><span><a href="#label-BEGIN+and+END">¶</a> <a href="#top">↑</a></span></h2>
<p><code>BEGIN</code> 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 <code>END</code> defines a block that is run after any other code.</p>
<p><code>BEGIN</code> must appear at top-level and <code>END</code> will issue a warning when you use it inside a method.</p>
<p>Here is an example:</p>
<pre class="ruby"><span class="ruby-keyword">BEGIN</span> {
<span class="ruby-identifier">count</span> = <span class="ruby-value">0</span>
}
</pre>
<p>You must use <code>{</code> and <code>}</code> you may not use <code>do</code> and <code>end</code>.</p>
<p>Here is an example one-liner that adds numbers from standard input or any files in the argument list:</p>
<pre>ruby -ne 'BEGIN { count = 0 }; END { puts count }; count += gets.to_i'</pre>
</main>
<footer id="validator-badges" role="contentinfo">
<p><a href="https://validator.w3.org/check/referer">Validate</a>
<p>Generated by <a href="https://ruby.github.io/rdoc/">RDoc</a> 6.2.1.1.
<p>Based on <a href="http://deveiate.org/projects/Darkfish-RDoc/">Darkfish</a> by <a href="http://deveiate.org">Michael Granger</a>.
</footer>