-
Notifications
You must be signed in to change notification settings - Fork 0
/
ruby_txt.html
executable file
·434 lines (291 loc) · 17.5 KB
/
ruby_txt.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>ruby - RDoc Documentation</title>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-83416778-1', 'auto');
ga('send', 'pageview');
</script>
<script type="text/javascript">
var rdoc_rel_prefix = "./";
</script>
<script src="./js/jquery.js"></script>
<script src="./js/darkfish.js"></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-1+-+Splat+operator+-28-22-2A-22-29">1 - Splat operator (“*”)</a>
<li><a href="#label-1b+-+Block-2FMethod+parameters">1b - Block/Method parameters</a>
<li><a href="#label-2+-+Double-bang+operator+-28-22-21-21-22-29">2 - Double-bang operator (“!!”)</a>
<li><a href="#label-3+-+Namespace+resolution+operator+-28-22-3A-3A-22-29">3 - Namespace resolution operator (“::”)</a>
<li><a href="#label-4+-+Scope+resolution+operator">4 - Scope resolution operator</a>
<li><a href="#label-5+-+Method+argument+hash+key+syntactic+sugar">5 - Method argument hash key syntactic sugar</a>
<li><a href="#label-6+-+-26Symbol+--3E+Symbol-23to_proc">6 - &Symbol -> Symbol#to_proc</a>
<li><a href="#label-7+-+Send+method">7 - Send method</a>
<li><a href="#label-8+-+Variable+syntax">8 - Variable syntax</a>
<li><a href="#label-9+-+Require+vs+Include">9 - Require vs Include</a>
<li><a href="#label-10+-+Yield">10 - Yield</a>
<li><a href="#label-11+-+RDoc+of+installed+gems">11 - RDoc of installed gems</a>
<li><a href="#label-12+-+Lambda+syntax+in+Ruby+1.9">12 - Lambda syntax in Ruby 1.9</a>
<li><a href="#label-13+-+Appending+and+prepending+colon">13 - Appending and prepending colon</a>
<li><a href="#label-14+-+Open+classes+via+Override+or+monkey-pathing">14 - Open classes via Override or monkey-pathing</a>
<li><a href="#label-15+-+Find+where+method+defined">15 - Find where method defined</a>
<li><a href="#label-16+-+Sinatra+and+Rack">16 - Sinatra and Rack</a>
<li><a href="#label-Rack">Rack</a>
<li><a href="#label-Sinatra">Sinatra</a>
<li><a href="#label-17+-+Debugger">17 - Debugger</a>
<li><a href="#label-18+-+Pattern+matching">18 - Pattern matching</a>
</ul>
</div>
<div id="project-metadata">
<div id="fileindex-section" class="nav-section">
<h3>Pages</h3>
<ul class="link-list">
<li><a href="./big-data_txt.html">big-data</a>
<li><a href="./coffeescript_txt.html">coffeescript</a>
<li><a href="./git-cmds_txt.html">git-cmds</a>
<li><a href="./home_txt.html">home</a>
<li><a href="./java_txt.html">java</a>
<li><a href="./mac-cmds_txt.html">mac-cmds</a>
<li><a href="./metaprogramming_txt.html">metaprogramming</a>
<li><a href="./opensource-projects_txt.html">opensource-projects</a>
<li><a href="./psql_txt.html">psql</a>
<li><a href="./python_txt.html">python</a>
<li><a href="./rails-cmds_txt.html">rails-cmds</a>
<li><a href="./rails_txt.html">rails</a>
<li><a href="./railscasts/railsCasts2013-14_txt.html">railsCasts2013-14</a>
<li><a href="./railscasts/railsCasts2015_txt.html">railsCasts2015</a>
<li><a href="./railscasts/railsCasts2016_txt.html">railsCasts2016</a>
<li><a href="./ros_txt.html">ros</a>
<li><a href="./rspec_txt.html">rspec</a>
<li><a href="./ruby-fann_txt.html">ruby-fann</a>
<li><a href="./ruby_txt.html">ruby</a>
<li><a href="./spree-methods_txt.html">spree-methods</a>
<li><a href="./sql_txt.html">sql</a>
<li><a href="./static-website_txt.html">static-website</a>
<li><a href="./tdd_txt.html">tdd</a>
<li><a href="./ubuntu_txt.html">ubuntu</a>
<li><a href="./vision_txt.html">vision</a>
</ul>
</div>
</div>
</nav>
<main role="main" aria-label="Page ruby.txt">
<h1 id="label-1+-+Splat+operator+-28-22-2A-22-29">1 - Splat operator (“*”)<span><a href="#label-1+-+Splat+operator+-28-22-2A-22-29">¶</a> <a href="#top">↑</a></span></h1>
<p>Entry in arg list “soaks up” list of args</p>
<pre class="ruby"><span class="ruby-identifier">http</span>:<span class="ruby-operator">/</span><span class="ruby-regexp">/www.catb.org/</span><span class="ruby-identifier">jargon</span><span class="ruby-operator">/</span><span class="ruby-identifier">html</span><span class="ruby-operator">/</span><span class="ruby-constant">S</span><span class="ruby-operator">/</span><span class="ruby-identifier">splat</span>.<span class="ruby-identifier">html</span>
</pre>
<p>CAN BE USED TO FIND LIST OF ARGS PASSED TO BLOCK:</p>
<pre class="ruby"><span class="ruby-identifier">unknown_block</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-operator">*</span><span class="ruby-identifier">args</span><span class="ruby-operator">|</span>
<span class="ruby-identifier">args</span>.<span class="ruby-identifier">count</span>
<span class="ruby-identifier">args</span>[<span class="ruby-value">0</span>].<span class="ruby-identifier">class</span>
<span class="ruby-keyword">end</span>
</pre>
<p>[CoffeeScript initially used the “*VAR” then changed to “VAR…” <a
href="https://github.com/jashkenas/coffee-script/issues/45">github.com/jashkenas/coffee-script/issues/45</a>]</p>
<h3 id="label-1b+-+Block-2FMethod+parameters">1b - Block/Method parameters<span><a href="#label-1b+-+Block-2FMethod+parameters">¶</a> <a href="#top">↑</a></span></h3>
<p>Variable in pipes at the start of the block/method</p>
<h1 id="label-2+-+Double-bang+operator+-28-22-21-21-22-29">2 - Double-bang operator (“!!”)<span><a href="#label-2+-+Double-bang+operator+-28-22-21-21-22-29">¶</a> <a href="#top">↑</a></span></h1>
<p>The “!” converts the value into a boolean and then negates it.</p>
<pre>The extra "!" negates the negated boolean eg "!! completed_at" -> "if completed"</pre>
<h1 id="label-3+-+Namespace+resolution+operator+-28-22-3A-3A-22-29">3 - Namespace resolution operator (“::”)<span><a href="#label-3+-+Namespace+resolution+operator+-28-22-3A-3A-22-29">¶</a> <a href="#top">↑</a></span></h1>
<pre class="ruby"><span class="ruby-keyword">module</span> <span class="ruby-constant">SomeModule</span>
<span class="ruby-keyword">module</span> <span class="ruby-constant">InnerModule</span>
<span class="ruby-keyword">class</span> <span class="ruby-constant">MyClass</span>
<span class="ruby-constant">CONSTANT</span> = <span class="ruby-value">4</span>
<span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>
<span class="ruby-constant">SomeModule</span><span class="ruby-operator">::</span><span class="ruby-constant">InnerModule</span><span class="ruby-operator">::</span><span class="ruby-constant">MyClass</span><span class="ruby-operator">::</span><span class="ruby-constant">CONSTANT</span>
</pre>
<h1 id="label-4+-+Scope+resolution+operator">4 - Scope resolution operator<span><a href="#label-4+-+Scope+resolution+operator">¶</a> <a href="#top">↑</a></span></h1>
<pre>. = instance method (or message operator)
:: = class/module method (or scope opertor)
(See http://en.wikipedia.org/wiki/Scope_resolution_operator#Ruby)
[Rails routes use '#' for 'Controller#Action']</pre>
<h1 id="label-5+-+Method+argument+hash+key+syntactic+sugar">5 - Method argument hash key syntactic sugar<span><a href="#label-5+-+Method+argument+hash+key+syntactic+sugar">¶</a> <a href="#top">↑</a></span></h1>
<p>Ruby has implicit hash parameters so enclosing “{}” not required (as in
option 3)</p>
<ul><li>
<p>method(a: 12, b: 13)</p>
<p>same as</p>
</li><li>
<p>method(:a => 12, :b => 13)</p>
<p>same as</p>
</li><li>
<p>method({:a => 12, :b => 13})</p>
</li></ul>
<p>(See <a href="#label-13+-+Appending+and+prepending+colon">13 - Appending
and prepending colon</a>)</p>
<h1 id="label-6+-+-26Symbol+--3E+Symbol-23to_proc">6 - &Symbol -> Symbol#to_proc<span><a href="#label-6+-+-26Symbol+--3E+Symbol-23to_proc">¶</a> <a href="#top">↑</a></span></h1>
<p>Impicit class cast applies 'Symbol#to_proc' to ':foo'</p>
<pre>'bar(&:foo)' converted to 'bar{|x| x.foo}'</pre>
<p>Can be used with 'map' on array of objects</p>
<pre>'~/.rvm/gems/ruby-2.2.2/gems/fnordmetric-1.2.9/lib/fnordmetric.rb':
def self.register(obj)
@@pool.push(obj)
end
def self.start_em
EM.run do
EM.next_tick do
(@@pool || []).map(&:initialized)
end
end
end</pre>
<h1 id="label-7+-+Send+method">7 - Send method<span><a href="#label-7+-+Send+method">¶</a> <a href="#top">↑</a></span></h1>
<p>(See 'tdd.txt' for details about test doubles hooking messages)</p>
<p>Send a method message to an object</p>
<pre>Like passing a function pointer to a middleware in C
'def method_missing' can be used to perform the appropriate action</pre>
<h1 id="label-8+-+Variable+syntax">8 - Variable syntax<span><a href="#label-8+-+Variable+syntax">¶</a> <a href="#top">↑</a></span></h1>
<ul><li>
<p>Local = lowercase letter or underscore name start (within block)</p>
</li><li>
<p>Global = $ (anywhere in program)</p>
</li><li>
<p>Instance = @ (instance of “self”)</p>
</li><li>
<p>Class = @@ (all instances of class)</p>
</li></ul>
<h1 id="label-9+-+Require+vs+Include">9 - Require vs Include<span><a href="#label-9+-+Require+vs+Include">¶</a> <a href="#top">↑</a></span></h1>
<p>Require when wanted methods are in another file ('require_relative'
when in same directory)</p>
<p>Include when extending classes with other modules (ie mix-ins) eg “include
Checkout” in 'spree/core/app/models/spree/order.rb'</p>
<h1 id="label-10+-+Yield">10 - Yield<span><a href="#label-10+-+Yield">¶</a> <a href="#top">↑</a></span></h1>
<p>Yields to the associated block of code (implicitly passed to the method)</p>
<pre>'block_given?' prevents 'yield' giving an error on no block implicitly passed</pre>
<h1 id="label-11+-+RDoc+of+installed+gems">11 - RDoc of installed gems<span><a href="#label-11+-+RDoc+of+installed+gems">¶</a> <a href="#top">↑</a></span></h1>
<p>$ gem server;</p>
<h1 id="label-12+-+Lambda+syntax+in+Ruby+1.9">12 - Lambda syntax in Ruby 1.9<span><a href="#label-12+-+Lambda+syntax+in+Ruby+1.9">¶</a> <a href="#top">↑</a></span></h1>
<p>'lambda' (ie anonymous functions like 'proc') can now be
specified with '->' (or the 'stab' operator or “dash
rocket”). Eg:</p>
<pre>> foo2 = ->(arg) { arg*2 }
> foo2.call "now"
=> nownow
Note the lack of space between -> and (arg).</pre>
<p>“->(x) { x * 2 }” SAME AS “lambda { |x| x * 2 }”</p>
<p>('=>', or “fat comma”, “hash rocket”, binds key-value pairs in a
hash)</p>
<h1 id="label-13+-+Appending+and+prepending+colon">13 - Appending and prepending colon<span><a href="#label-13+-+Appending+and+prepending+colon">¶</a> <a href="#top">↑</a></span></h1>
<p>New syntax for Ruby 1.9</p>
<p>(“Rails 5.0 will target Ruby 2.2+ exclusively” so up until that release
then Ruby 1.9.3+ can be used, <a
href="http://edgeguides.rubyonrails.org/upgrading_ruby_on_rails.html#ruby-versions">edgeguides.rubyonrails.org/upgrading_ruby_on_rails.html#ruby-versions</a>)</p>
<p>Both are forms of a symbol</p>
<p>Prior to Ruby 1.9 hash's were defined with more verbose syntax:</p>
<pre class="ruby"><span class="ruby-identifier">hash</span> = {
:<span class="ruby-identifier">key</span> =<span class="ruby-operator">></span> <span class="ruby-string">"value"</span>,
:<span class="ruby-identifier">another_key</span> =<span class="ruby-operator">></span> <span class="ruby-value">4</span>
}
</pre>
<p>Now can use:</p>
<pre class="ruby"><span class="ruby-identifier">hash</span> = {
<span class="ruby-identifier">key</span><span class="ruby-operator">:</span> <span class="ruby-string">"value"</span>,
<span class="ruby-identifier">another_key</span><span class="ruby-operator">:</span> <span class="ruby-value">4</span>
}
</pre>
<p>(See <a href="#label-5+-+Method+argument+hash+key+syntactic+sugar">5 -
Method argument hash key syntactic sugar</a>)</p>
<h1 id="label-14+-+Open+classes+via+Override+or+monkey-pathing">14 - Open classes via Override or monkey-pathing<span><a href="#label-14+-+Open+classes+via+Override+or+monkey-pathing">¶</a> <a href="#top">↑</a></span></h1>
<p>Ruby classes are open</p>
<p><a
href="http://ruby-journal.com/how-to-open-slash-override-slash-monkey-patch-a-class-in-ruby">ruby-journal.com/how-to-open-slash-override-slash-monkey-patch-a-class-in-ruby</a>/</p>
<p>Can override existing class if already exists (just define it if not)</p>
<pre>http://docs.ruby-lang.org/en/2.0.0/syntax/methods_rdoc.html#label-Overriding</pre>
<p>'class_eval' opens existing class</p>
<pre>If class does not exist then NameError exeception will be raised
Therefore extra check rather than overriding/defining class</pre>
<h1 id="label-15+-+Find+where+method+defined">15 - Find where method defined<span><a href="#label-15+-+Find+where+method+defined">¶</a> <a href="#top">↑</a></span></h1>
<p>'debugger' or '<% debugger %>'</p>
<ul><li>
<p>(byebug) method(:title)</p>
<pre class="ruby"><span class="ruby-comment">#<Method: #<Class:0x0000010363e288>(LayoutHelper)#title></span>
</pre>
</li><li>
<p>(byebug) method(:title).source_location</p>
<pre class="ruby">[<span class="ruby-string">"/Users/doug/src/cloudreach/app/helpers/layout_helper.rb"</span>, <span class="ruby-value">6</span>]
</pre>
</li></ul>
<h1 id="label-16+-+Sinatra+and+Rack">16 - Sinatra and Rack<span><a href="#label-16+-+Sinatra+and+Rack">¶</a> <a href="#top">↑</a></span></h1>
<p>(28/8/16)</p>
<h3 id="label-Rack">Rack<span><a href="#label-Rack">¶</a> <a href="#top">↑</a></span></h3>
<p><a href="http://rack.github.io">rack.github.io</a>/</p>
<p>Rack, a modular Ruby webserver interface.</p>
<pre>Rack provides a minimal, modular and adaptable interface for developing web applications in Ruby.
By wrapping HTTP requests and responses in the simplest way possible, it unifies and distills the API for web servers,
web frameworks, and middleware into a single method call.</pre>
<ul><li>
<p>Thin is rails default rack-based web server (<a
href="http://code.macournoyer.com/thin">code.macournoyer.com/thin</a>/)</p>
</li></ul>
<h3 id="label-Sinatra">Sinatra<span><a href="#label-Sinatra">¶</a> <a href="#top">↑</a></span></h3>
<p><a
href="http://www.sinatrarb.com/intro.html">www.sinatrarb.com/intro.html</a></p>
<p>Sinatra is a small “framework” on top of Rack</p>
<pre>Sinatra is a DSL for quickly creating web applications in Ruby with minimal effort</pre>
<h1 id="label-17+-+Debugger">17 - Debugger<span><a href="#label-17+-+Debugger">¶</a> <a href="#top">↑</a></span></h1>
<p>(28/12/16)</p>
<ul><li>
<p>export GEM_PATH</p>
</li><li>
<p>export HOMEBREW_RUBY_PATH=“$(which ruby)”</p>
</li><li>
<p>require 'byebug'</p>
<p>'byebug' or 'debugger' in code</p>
</li></ul>
<p><a
href="https://www.tutorialspoint.com/ruby/ruby_debugger.htm">www.tutorialspoint.com/ruby/ruby_debugger.htm</a></p>
<ul><li>
<p>ENV</p>
<pre class="ruby"><span class="ruby-constant">ENV</span>[<span class="ruby-string">"HOMEBREW_WHICH_RUBY"</span>]
</pre>
</li></ul>
<h1 id="label-18+-+Pattern+matching">18 - Pattern matching<span><a href="#label-18+-+Pattern+matching">¶</a> <a href="#top">↑</a></span></h1>
<p>(18/2/17)</p>
<ul><li>
<p>See <a
href="file:///Users/doug/Downloads/ruby-2.0.0-p648/doc/doc/re_rdoc.html">RDoc
of downloaded ‘ruby-2.0.0-p648’ src</a></p>
</li></ul>
</main>
<footer id="validator-badges" role="contentinfo">
<p><a href="http://validator.w3.org/check/referer">Validate</a>
<p>Generated by <a href="http://docs.seattlerb.org/rdoc/">RDoc</a> 4.2.2.
<p>Based on <a href="http://deveiate.org/projects/Darkfish-RDoc/">Darkfish</a> by <a href="http://deveiate.org">Michael Granger</a>.
</footer>