-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfile.miscellaneous.html
159 lines (108 loc) · 8.09 KB
/
file.miscellaneous.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<!DOCTYPE html>
<html>
<head>
<meta charset='UTF-8'>
<meta name='viewport' content='width=device-width, initial-scale=1.0, user-scalable=no'>
<meta name='apple-touch-fullscreen' content='yes'>
<meta name='apple-mobile-web-app-capable' content='yes'>
<meta name='apple-mobile-web-app-status-bar-style' content='rgba(228,228,228,1.0)'>
<title>File: Miscellaneous — Ruby-2.6.10</title>
<link rel='stylesheet' type='text/css' href='../css/y_fonts.css' />
<link rel='stylesheet' type='text/css' href='../css/highlight.github.css' />
<link rel='stylesheet' type='text/css' href='../css/y_style.css' />
<link rel='stylesheet' type='text/css' href='../css/y_list.css' />
<link rel='stylesheet' type='text/css' href='../css/y_color.css' />
<script type='text/javascript'>
var pathId = "miscellaneous",
relpath = '';
var t2Info = {
CSEP: '.',
ISEP: '#',
NSEP: '::'
};
</script>
<script type='text/javascript' charset='utf-8' src='../js/highlight.pack.js'></script>
<script type='text/javascript' charset='utf-8' src='../js/y_app.js'></script>
</head>
<body>
<svg id='y_wait' class viewBox='0 0 90 90'></svg>
<div id='settings' class='hidden'></div>
<div id='y_list' class='d h'>
<header id='list_header'></header>
<nav id= 'list_nav' class='y_nav l_nav'>
<ul id='list_items'></ul>
</nav>
</div>
<div id='y_toc' class='f h'>
<header id='toc_header'></header>
<nav id= 'toc_nav' class='y_nav t_nav'>
<ol id='toc_items'></ol>
</nav>
</div>
<div id='y_main' tabindex='-1'>
<header id='y_header'>
<div id='y_menu'>
<a id='home_no_xhr' href='/'>Home</a> »
<a href='.'>Ruby-2.6.10</a> »
<a href='_index.html'>Index</a> »
<span class='title'><a id='t2_doc_top' href='#'>File: Miscellaneous ▲</a></span>
</div>
<a id='list_href' href="file_list.html"></a>
<div id='y_measure_em' class='y_measure'></div>
<div id='y_measure_vh' class='y_measure'></div>
<span id='y_measure_50pre' class='y_measure'><code>123456789_123456789_123456789_123456789_123456789_</code></span>
</header>
<div id='content' class='file'>
<h1 id="label-Miscellaneous+Syntax">Miscellaneous Syntax</h1>
<h2 id="label-Ending+an+Expression">Ending an Expression</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</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 mis-match, you will receive a warning.</p>
<h2 id="label-alias"><code>alias</code></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="code ruby"><code class="ruby"><span class='kw'>alias</span> <span class='id identifier rubyid_new_name'>new_name</span> <span class='id identifier rubyid_old_name'>old_name</span>
<span class='kw'>alias</span> <span class='symbeg'>:</span><span class='id identifier rubyid_new_name'>new_name</span> <span class='symbeg'>:</span><span class='id identifier rubyid_old_name'>old_name</span></code></pre>
<p>For methods, Module#alias_method 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="code ruby"><code class="ruby"><span class='gvar'>$old</span> <span class='op'>=</span> <span class='int'>0</span>
<span class='kw'>alias</span> <span class='gvar'>$new</span> <span class='gvar'>$old</span>
<span class='id identifier rubyid_p'>p</span> <span class='gvar'>$new</span> <span class='comment'># prints 0</span></code></pre>
<p>You may use <code>alias</code> in any scope.</p>
<h2 id="label-undef"><code>undef</code></h2>
<p>The <code>undef</code> keyword prevents the current class from responding to calls to the named methods.</p>
<pre class="code ruby"><code class="ruby"><span class='kw'>undef</span> <span class='id identifier rubyid_my_method'>my_method</span></code></pre>
<p>You may use symbols instead of method names:</p>
<pre class="code ruby"><code class="ruby"><span class='kw'>undef</span> <span class='symbeg'>:</span><span class='id identifier rubyid_my_method'>my_method</span></code></pre>
<p>You may undef multiple methods:</p>
<pre class="code ruby"><code class="ruby"><span class='kw'>undef</span> <span class='id identifier rubyid_method1'>method1</span><span class='comma'>,</span> <span class='id identifier rubyid_method2'>method2</span></code></pre>
<p>You may use <code>undef</code> in any scope. See also Module#undef_method</p>
<h2 id="label-defined-3F"><code>defined?</code></h2>
<p><code>defined?</code> is a keyword that returns a string describing its argument:</p>
<pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_p'>p</span> <span class='kw'>defined?</span>(<span class='const'>UNDEFINED_CONSTANT</span>) <span class='comment'># prints nil
</span><span class='id identifier rubyid_p'>p</span> <span class='kw'>defined?</span>(<span class='const'>RUBY_VERSION</span>) <span class='comment'># prints "constant"
</span><span class='id identifier rubyid_p'>p</span> <span class='kw'>defined?</span>(<span class='int'>1</span> <span class='op'>+</span> <span class='int'>1</span>) <span class='comment'># prints "method"</span></code></pre>
<p>You don’t need to use parenthesis with <code>defined?</code>, but they are recommended due to the <a href="file.precedence.html" title="low precedence">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="code ruby"><code class="ruby"><span class='kw'>defined?</span> <span class='ivar'>@instance_variable</span> <span class='op'>&&</span> <span class='ivar'>@instance_variable</span>.<span class='id identifier rubyid_zero?'>zero?</span></code></pre>
<p>This returns <code>"expression"</code>, which is not what you want if the instance variable is not defined.</p>
<pre class="code ruby"><code class="ruby"><span class='ivar'>@instance_variable</span> <span class='op'>=</span> <span class='int'>1</span>
<span class='kw'>defined?</span>(<span class='ivar'>@instance_variable</span>) <span class='op'>&&</span> <span class='ivar'>@instance_variable</span>.<span class='id identifier rubyid_zero?'>zero?</span></code></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></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="code ruby"><code class="ruby"><span class='kw'>BEGIN</span> {
<span class='id identifier rubyid_count'>count</span> <span class='op'>=</span> <span class='int'>0</span>
}</code></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 class="code ruby"><code class="ruby">ruby -ne 'BEGIN { count = 0 }; END { puts count }; count += gets.to_i'</code></pre>
<div id='footer'></div>
</div> <!-- content -->
</div> <!-- y_main -->
</body>
</html>