-
Notifications
You must be signed in to change notification settings - Fork 0
/
atom.xml
836 lines (674 loc) · 54.2 KB
/
atom.xml
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title><![CDATA[Scrappy Academy]]></title>
<link href="http://ScrappyAcademy.github.com/atom.xml" rel="self"/>
<link href="http://ScrappyAcademy.github.com/"/>
<updated>2013-02-16T16:44:56-05:00</updated>
<id>http://ScrappyAcademy.github.com/</id>
<author>
<name><![CDATA[Scrappy Academy]]></name>
</author>
<generator uri="http://octopress.org/">Octopress</generator>
<entry>
<title type="html"><![CDATA[About Ruby Blocks]]></title>
<link href="http://ScrappyAcademy.github.com/blog/2013/02/16/about-ruby-blocks/"/>
<updated>2013-02-16T16:10:00-05:00</updated>
<id>http://ScrappyAcademy.github.com/blog/2013/02/16/about-ruby-blocks</id>
<content type="html"><![CDATA[<p>After learning a bit about Ruby and the basic way that data is stored and
manipulated I came across the concept of the Block.</p>
<p>I was enthralled…and bewildered.</p>
<p>The concept seemed simple enough. Blocks are pieces of code, or instructions,
that are passed to a function.</p>
<p>Amazing! Passing messages to other messages! This sounds highly advanced. I
confess my first thought <em>“Why would I ever want to pass a message to another
message”?</em></p>
<p>This blog entry will begin with the syntax used to pass a Block to a function
and finish with a discussion on the usefulness of being able to pass code to a
function.</p>
<h2>Syntax</h2>
<p>So first a bit about Blocks in general. A Block is a bit of code that follows a
function call and is contained within a set of braces <code>{}</code> or <code>do</code> / <code>end</code>
delimiters. There is a common convention that braces should be used where the
Block is only one line and <code>do</code> / <code>end</code> delimiters are used where the code
extends over multiple lines. Examples below…</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">some_function</span><span class="p">(</span><span class="n">arguments</span><span class="p">)</span> <span class="p">{</span> <span class="nb">puts</span> <span class="s2">"Short Code Example"</span> <span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">some_function</span><span class="p">(</span><span class="n">arguments</span><span class="p">)</span> <span class="k">do</span>
</span><span class='line'> <span class="nb">puts</span> <span class="s2">"This is a style"</span>
</span><span class='line'> <span class="nb">puts</span> <span class="s2">"commonly used for code in a block"</span>
</span><span class='line'> <span class="nb">puts</span> <span class="s2">"fitting over multiple lines"</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>
<p>_Jim Weirich discusses another convention specific to the presence of a return
value in his <a href="http://onestepback.org/index.cgi/Tech/Ruby/BraceVsDoEnd.rdoc">blog
entry</a>.</p>
<p>Regardless of how the Block is defined, it is always listed after a message
starting on the same line as the message call.</p>
<p><strong>General Syntax</strong></p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">object</span><span class="o">.</span><span class="n">message</span><span class="p">(</span><span class="n">arguments</span><span class="p">)</span> <span class="p">{</span><span class="n">block</span><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="n">object</span><span class="o">.</span><span class="n">message</span><span class="p">(</span><span class="n">arguments</span><span class="p">)</span> <span class="k">do</span>
</span><span class='line'> <span class="c1"># block</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>
<p><strong>Example 1:</strong></p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="mi">3</span><span class="o">.</span><span class="n">each</span> <span class="p">{</span> <span class="nb">puts</span> <span class="s2">"I can't stop printing!"</span> <span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>
<p><strong>Console:</strong></p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='text'><span class='line'>I can't stop printing!
</span><span class='line'>I can't stop printing!
</span><span class='line'>I can't stop printing!
</span></code></pre></td></tr></table></div></figure>
<p><strong>Example 2:</strong></p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">array</span> <span class="o">=</span> <span class="sx">%w[ geography art science ]</span>
</span><span class='line'><span class="n">array</span><span class="o">.</span><span class="n">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">subject</span><span class="o">|</span>
</span><span class='line'> <span class="nb">puts</span> <span class="s2">"I love </span><span class="si">#{</span><span class="n">subject</span><span class="si">}</span><span class="s2">"</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>
<p><strong>Console:</strong></p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='text'><span class='line'>I love art
</span><span class='line'>I love science
</span><span class='line'>I love math
</span></code></pre></td></tr></table></div></figure>
<p>The second example demonstrates how a Block can accept an argument passed to it
from the function. In this case it is the subject or value of the array.</p>
<h2>Usage</h2>
<p><em>So a block is a piece of logic that is passed with a message. But other than
utilizing simple pre-built iterators how can I unleash a Block within my own
code? How can I write a function that accepts a Block?</em></p>
<p>Blocks are implicitly accepted by all functions. Even if the function does not
use the block, it is valid syntax to pass it.</p>
<p>However, often, you do want to use the block. To write the function to
accommodate the presence of the block use <code>yield</code>. To provide a default
behavior if no block is provided it can be switched on with <code>if block_given?</code></p>
<p>When the function reaches <code>yield</code> the block is invoked. If arguments are to be
passed to the block they are listed after <code>yield</code>. ( <code>yield arguments</code>)</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">def</span> <span class="nf">type_of_day</span>
</span><span class='line'> <span class="k">if</span> <span class="nb">block_given?</span>
</span><span class='line'> <span class="nb">puts</span> <span class="k">yield</span>
</span><span class='line'> <span class="k">else</span>
</span><span class='line'> <span class="nb">puts</span> <span class="s2">"I havn't used a block all day, this code sucks"</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="n">type_of_day</span> <span class="p">{</span> <span class="s2">"I'm using a Block just like a real Ruby programmer!"</span> <span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>
<p>Blocks are particulary usefull when you find yourself writing lots of functions
containing the same code. If you have five functions with code that differs
only by one line…consider using a block. This way, you can condense all of
the functions into one well written function that contains a <code>yield</code> statement
with the differing code.</p>
<p>Another similar example would be writing a function that contains a bit of
logic that might change in the future (think <code>each</code>). If you build a function
that iterates through an array and performs a bit of logic on each index you
could write a function that accepts a block containing the logic to perform.</p>
<p>The following example shows code that might exist for a program designed to
automate yard maintenance. The <code>Yard</code> function has a public interface called
<code>maintain_garden</code> that contains several functions needed for garden upkeep.</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">Yard</span>
</span><span class='line'> <span class="k">def</span> <span class="nf">maintain_garden</span>
</span><span class='line'> <span class="n">water_garden</span>
</span><span class='line'> <span class="n">weed_garden</span>
</span><span class='line'> <span class="k">yield</span> <span class="k">if</span> <span class="nb">block_given?</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'>
</span><span class='line'> <span class="c1"># other functions omitted for brevity</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>
<p>Utilizing a block the <code>maintain_garden</code> function is dynamically able to provide
a way for others to hook in additional behavior. This way, if we decide to add
functions like <code>fertilize_garden</code> and <code>harvest_garden</code> we can pass them into
the function without having to modify the default behavior.</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">first_yard</span> <span class="o">=</span> <span class="no">Yard</span><span class="o">.</span><span class="n">new</span>
</span><span class='line'>
</span><span class='line'><span class="c1">### During the winter</span>
</span><span class='line'><span class="n">first_yard</span><span class="o">.</span><span class="n">maintain_garden</span>
</span><span class='line'>
</span><span class='line'><span class="c1">### During the spring</span>
</span><span class='line'><span class="n">first_yard</span><span class="o">.</span><span class="n">maintain_garden</span> <span class="p">{</span> <span class="n">fertilize_garden</span> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="c1">### During the fall</span>
</span><span class='line'><span class="n">first_yard</span><span class="o">.</span><span class="n">maintain_garden</span> <span class="k">do</span>
</span><span class='line'> <span class="n">harvest_garden</span>
</span><span class='line'> <span class="n">till_garden</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>
<h2>Conclusion</h2>
<p>Block’s can be used to make your functions more versatile allowing you to
reduce duplication of code (making your code more DRY). They can also be
utilized where logic may change in the future. Instead of having to go back and
manipulate the code within a function a block can be used to pass new logic to
the existing function.</p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Palindrome Warm-Up]]></title>
<link href="http://ScrappyAcademy.github.com/blog/2013/02/04/palindrome-warm-up/"/>
<updated>2013-02-04T09:36:00-05:00</updated>
<id>http://ScrappyAcademy.github.com/blog/2013/02/04/palindrome-warm-up</id>
<content type="html"><![CDATA[<p>This week we continued our efforts at Project Euler tackling # 4, <a href="http://projecteuler.net/problem=4">Largest
Palindrome Product</a></p>
<blockquote><p>A palindromic number reads the same both ways. The largest palindrome made
from the product of two 2-digit numbers is 9009 = 91 99.</p>
<p>Find the largest palindrome made from the product of two 3-digit numbers.</p></blockquote>
<p>This week instead of diving into code head-on we discussed what the exact
problem was, <em>We wanted to find the largest palindrome for a product of numbers
from 100-999.</em></p>
<p>We then wrote up our thoughts into a format for the code we thought we should
be writing to solve the problem:</p>
<p><code>products(100..999).select(&:is_palindrome?).max</code></p>
<p>Doing this allowed us to visualize a possible solution through code. It also
made obvious a few small problems we needed to solve before solving the main
question.</p>
<h2>Working with our code to find a solution:</h2>
<p><strong>1. Produce an array of products</strong></p>
<p>We looked through <a href="http://ruby-doc.org/core-1.9.3/Array.html">Ruby Doc</a> to find
the perfect method to call on an existing array to create a new array of
products.</p>
<p><a href="http://ruby-doc.org/core-1.9.3/Array.html#method-i-repeated_combination"><code>repeated_combination</code></a>
was exactly what we were looking for. This creates a new array of all the
combinations of elements in your existing array, without duplicates.</p>
<p>We then used the <code>map</code> method to generate the products of these new number
pairs.</p>
<p>Our Resulting Code:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">def</span> <span class="nf">products</span><span class="p">(</span><span class="n">range</span><span class="p">)</span>
</span><span class='line'> <span class="c1"># Range can be an array or numbers or a Range. Either way calling `to_a`</span>
</span><span class='line'> <span class="c1"># ensures we have an array to work with</span>
</span><span class='line'> <span class="n">range</span><span class="o">.</span><span class="n">to_a</span><span class="o">.</span><span class="n">repeated_combination</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span><span class="o">.</span><span class="n">map</span><span class="p">{</span><span class="o">|</span><span class="n">nums</span><span class="o">|</span> <span class="n">nums</span><span class="o">.</span><span class="n">reduce</span><span class="p">(</span><span class="ss">:*</span><span class="p">)}</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>
<p><strong>2. Evaluate products to see which products are palindromes</strong></p>
<p>This involved much discussion. At first we discussed manipulating the values
as an array.</p>
<p>What if we called <code>reverse</code> on the first three and last three indexes of the
array values and if they were equal we had our palindromes?</p>
<p>This could be a solution, but would result in a lot of labor intensive work by
the function to manipulate every value several times.</p>
<p>Will, a Scrappy member, brought up changing the array to a string. This way we
could just call <code>reverse</code> on all the strings. Then it is simply a matter of
checking if the string is the same forwards and backwards.</p>
<p>This seemed like the best idea. So we decided to delegate the <code>is_palindrome?</code>
logic to <code>String</code>:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">Numeric</span>
</span><span class='line'> <span class="k">def</span> <span class="nf">is_palindrome?</span>
</span><span class='line'> <span class="nb">to_s</span><span class="o">.</span><span class="n">is_palindrome?</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>
<p><strong>3. Define <code>is_palindrome</code></strong></p>
<p>This problem was pretty easily solved.</p>
<p>As we had already discussed figuring out what values were palindromes, we just
needed our strings before we could implement the function.</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">String</span>
</span><span class='line'> <span class="k">def</span> <span class="nf">is_palindrome?</span>
</span><span class='line'> <span class="nb">self</span> <span class="o">==</span> <span class="n">reverse</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>
<p><strong>4. Test our functions</strong></p>
<p>We had solved all our mini problems so now it was time to tackle the main
question:</p>
<blockquote><p>Find the largest palindrome made from the product of two 3-digit
numbers from 100-999.</p></blockquote>
<p>We tested our functions in IRB: <code>products(100...1000, 2).select{|n|
n.is_palindrome?}.max</code> sure enough returned the correct answer</p>
<p><strong>5. Correct our function to work with any number of arguments</strong></p>
<p>What excitement! We had collectively solved our weekly Project Euler problem,
but could we do more?</p>
<p>Yes! Our functions only returned the largest palindrome for a products of 2
numbers. What if we wanted to explore the largest palindrome for the product
of 3 numbers? No Problem!</p>
<p>To update our function we created a variable <code>n_elements</code> for the number of
arguments to be passed into the <code>repeated_combination</code> method in place of “2”.</p>
<p>Defining a variable for the number of arguments allows you to customize the
numbers you’re passing in to create a product. Our new <code>products</code> function
looked like this:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">def</span> <span class="nf">products</span><span class="p">(</span><span class="n">range</span><span class="p">,</span> <span class="n">n_elements</span> <span class="o">=</span> <span class="mi">2</span><span class="p">)</span>
</span><span class='line'> <span class="n">range</span><span class="o">.</span><span class="n">to_a</span><span class="o">.</span><span class="n">repeated_combination</span><span class="p">(</span><span class="n">n_elements</span><span class="p">)</span><span class="o">.</span><span class="n">map</span><span class="p">{</span><span class="o">|</span><span class="n">nums</span><span class="o">|</span> <span class="n">nums</span><span class="o">.</span><span class="n">reduce</span><span class="p">(</span><span class="ss">:*</span><span class="p">)}</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>
<p><strong>6. Wrapping Up</strong></p>
<p>So we had a solution. One thing that should be stated, was that we did monkey
patch Ruby core. There is a lot of discussion in the community about how this is
bad. In this case, we were willing to take the trade off with this simple patch
as it followed these rules:</p>
<ul>
<li>Is local only to our project</li>
<li>Is generic and can easily be re-used</li>
<li>Another implementation would probably not differ at all</li>
</ul>
<p>As always, Ruby is a language with lots of power. Use it responsibly.</p>
<p>Final solution:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">def</span> <span class="nf">products</span><span class="p">(</span><span class="n">range</span><span class="p">,</span> <span class="n">n_elements</span> <span class="o">=</span> <span class="mi">2</span><span class="p">)</span>
</span><span class='line'> <span class="c1"># Range can be an array or numbers or a Range. Either way calling `to_a`</span>
</span><span class='line'> <span class="c1"># ensures we have an array to work with</span>
</span><span class='line'> <span class="n">range</span><span class="o">.</span><span class="n">to_a</span><span class="o">.</span><span class="n">repeated_combination</span><span class="p">(</span><span class="n">n_elements</span><span class="p">)</span><span class="o">.</span><span class="n">map</span><span class="p">{</span><span class="o">|</span><span class="n">nums</span><span class="o">|</span> <span class="n">nums</span><span class="o">.</span><span class="n">reduce</span><span class="p">(</span><span class="ss">:*</span><span class="p">)}</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="k">class</span> <span class="nc">String</span>
</span><span class='line'> <span class="k">def</span> <span class="nf">is_palindrome?</span>
</span><span class='line'> <span class="nb">self</span> <span class="o">==</span> <span class="n">reverse</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="k">class</span> <span class="nc">Numeric</span>
</span><span class='line'> <span class="k">def</span> <span class="nf">is_palindrome?</span>
</span><span class='line'> <span class="nb">to_s</span><span class="o">.</span><span class="n">is_palindrome?</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="n">products</span><span class="p">(</span><span class="mi">100</span><span class="o">.</span><span class="n">.</span><span class="mi">999</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span><span class="o">.</span><span class="n">select</span><span class="p">(</span><span class="o">&</span><span class="ss">:is_palindrome?</span><span class="p">)</span><span class="o">.</span><span class="n">max</span>
</span></code></pre></td></tr></table></div></figure>
<h2>What else happened at Scrappy?</h2>
<p>After the warm-up we started exploring <a href="http://coffeescript.org/">Coffee
Script</a>. The rest of our meetings this month will be
focusing on the Campfire chat client ‘Hubot.’ Hubot uses Coffee Script to
interact with users and have some fun.</p>
<p><strong>What is Coffee Script?</strong></p>
<p>Coffee Script is a Ruby-like language that compiles to Javascript.</p>
<p><strong>Why would you use Coffee Script instead of good ole Javascript?</strong></p>
<p>There are several idioms specific to Javascript that can be overlooked after a
long day of coding in Rails. Coffee Script can make it easier to switch from
back end to front end work without overlooking some of those specific idioms.</p>
<p>Also, CoffeeScript is available by default in Rails. So it’s just one of the
other technologies that are available for developer to learn.</p>
<p>We started exploring Hubot in campfire and even made our first update. We
cloned the Hubot from Scrappy Academy’s main repo directly onto our machine
from <a href="https://github.com/ScrappyAcademy/hubot">GitHub</a></p>
<p>Next we made a branch on the Scrappy Academy Hubot repo to match the issue we
wanted to solve, in this case we created the branch “Fix Cheer Me Up”.</p>
<p>Our goal with “Fix Cheer Me Up” was to tell our Hubot to listen for the phrase
“cheer me up” and respond instead of having to ask our Hubot directly to “cheer
me up”.</p>
<p>After cloning the repo onto our machine we opened up the <code>script</code>
file and then opened <code>cheer.coffee</code>. We located “cheer me up” and
updated <code>robot.respond /cheer me up...</code> to <code>robot.hear /cheer me up...</code></p>
<p>Now we needed to update the main repo with the changes we made on our local
machine.</p>
<p>We went to GitHub and created a Pull Request to merge the updates from our “Fix
Cheer Me Up” branch to the Hubot “Master” branch.</p>
<p>We checked to make sure all systems were a go and then merged our updates onto
Master, success!</p>
<p>Now our hubot takes note anytime “cheer me up” is mentioned in converstaion on
our Campfire site instead of having to ask our hubot directly to cheer us up!</p>
<p>This week our goal is to tackle some of the issues with Hubot in the Scrappy
Academy repo. What issues are you tackling this week on Hubot?</p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Fibonacci Warm-Up]]></title>
<link href="http://ScrappyAcademy.github.com/blog/2013/01/28/fibonacci-warm-up/"/>
<updated>2013-01-28T19:24:00-05:00</updated>
<id>http://ScrappyAcademy.github.com/blog/2013/01/28/fibonacci-warm-up</id>
<content type="html"><![CDATA[<p>Something we started doing just this week, and plan on continuing moving
forward, is tackling “warm-up” problems. This is reminiscent of high school math
class. These are relatively simple programming problems that are solved via
<em>pairing</em>.</p>
<p>This week, we solved via <em>group-pairing</em> is where the entire group participates
in solving the problem. One person is designated as the <em>coder</em>. S/he displays
her/his laptop on the conference room TV (via an Apple TV). The group then
discusses the problem, and codes up a solution together.</p>
<p>Often this leads into side discussions about various programming techniques,
tips, tricks, etc. It’s a good time for all. Everyone at different experience
levels walks away with something new.</p>
<p>Last week we started tackling <a href="http://projecteuler.net/">Project Euler</a>
problems. Up first was <a href="http://projecteuler.net/problem=2"><em>Even Fibonacci numbers</em></a>.
This appeared to be a relatively straight forward problem. The first solution
that was attempted looked a bit like:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">a</span> <span class="o">=</span> <span class="mi">1</span>
</span><span class='line'><span class="n">b</span> <span class="o">=</span> <span class="mi">1</span>
</span><span class='line'><span class="n">sum</span> <span class="o">=</span> <span class="mi">0</span>
</span><span class='line'><span class="k">while</span> <span class="n">b</span> <span class="o"><</span> <span class="mi">4_000_000</span>
</span><span class='line'> <span class="n">a</span> <span class="o">=</span> <span class="n">b</span>
</span><span class='line'> <span class="n">b</span> <span class="o">=</span> <span class="n">a</span> <span class="o">+</span> <span class="n">b</span>
</span><span class='line'> <span class="k">if</span> <span class="n">b</span><span class="o">.</span><span class="n">even?</span>
</span><span class='line'> <span class="n">sum</span> <span class="o">=</span> <span class="n">sum</span> <span class="o">+</span> <span class="n">b</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'><span class="nb">puts</span> <span class="n">sum</span>
</span></code></pre></td></tr></table></div></figure>
<p>However, this produced an incorrect result. Upon further investigation, the bug
was in lines 5 and 6. Due to the order of the statements the value of <code>a</code> gets
overwritten, before it can be added to <code>b</code>. The evaluation looks a bit like
this:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">a</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">b</span> <span class="o">=</span> <span class="mi">1</span> <span class="c1"># Before loop</span>
</span><span class='line'><span class="n">a</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">b</span> <span class="o">=</span> <span class="mi">2</span> <span class="c1"># 1st pass</span>
</span><span class='line'><span class="n">a</span> <span class="o">=</span> <span class="mi">2</span><span class="p">;</span> <span class="n">b</span> <span class="o">=</span> <span class="mi">4</span> <span class="c1"># 2nd pass</span>
</span><span class='line'><span class="n">a</span> <span class="o">=</span> <span class="mi">4</span><span class="p">;</span> <span class="n">b</span> <span class="o">=</span> <span class="mi">8</span> <span class="c1"># 3rd pass</span>
</span></code></pre></td></tr></table></div></figure>
<p>So to fix this, the group replaced those two lines of code with:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">tmp</span> <span class="o">=</span> <span class="n">b</span>
</span><span class='line'><span class="n">b</span> <span class="o">=</span> <span class="n">a</span> <span class="o">+</span> <span class="n">b</span>
</span><span class='line'><span class="n">a</span> <span class="o">=</span> <span class="n">tmp</span>
</span></code></pre></td></tr></table></div></figure>
<p>Now when things get evaluated, it looks like:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">a</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">b</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">tmp</span> <span class="o">=</span> <span class="kp">nil</span> <span class="c1"># Before loop</span>
</span><span class='line'><span class="n">a</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">b</span> <span class="o">=</span> <span class="mi">2</span><span class="p">;</span> <span class="n">tmp</span> <span class="o">=</span> <span class="mi">1</span> <span class="c1"># 1st pass</span>
</span><span class='line'><span class="n">a</span> <span class="o">=</span> <span class="mi">2</span><span class="p">;</span> <span class="n">b</span> <span class="o">=</span> <span class="mi">3</span><span class="p">;</span> <span class="n">tmp</span> <span class="o">=</span> <span class="mi">2</span> <span class="c1"># 2nd pass</span>
</span><span class='line'><span class="n">a</span> <span class="o">=</span> <span class="mi">3</span><span class="p">;</span> <span class="n">b</span> <span class="o">=</span> <span class="mi">5</span><span class="p">;</span> <span class="n">tmp</span> <span class="o">=</span> <span class="mi">3</span> <span class="c1"># 3rd pass</span>
</span></code></pre></td></tr></table></div></figure>
<p>This led into a side discussion about some Ruby idioms. Namely, multiple
assignment. We were able to replace these three lines of code with one:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="o">=</span> <span class="n">b</span><span class="p">,</span> <span class="n">a</span><span class="o">+</span><span class="n">b</span>
</span></code></pre></td></tr></table></div></figure>
<p>This will do exactly as you expect. It helps to understand what Ruby is doing
behind the scenes. In essence, it’s evaluating and storing the right-hand-side
(<em>rhs</em> as it is commonly seen in error messages). Then it uses the <code>splat</code> to
dereference the values and assign them in turn. Think of it as:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">tmp</span> <span class="o">=</span> <span class="o">[</span><span class="n">b</span><span class="p">,</span> <span class="n">a</span><span class="o">+</span><span class="n">b</span><span class="o">]</span>
</span><span class='line'><span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="o">=</span> <span class="o">*</span><span class="n">tmp</span>
</span></code></pre></td></tr></table></div></figure>
<p>Additionally, we talked about the <code>if</code> and <code>unless</code> one-liners. These are great
ways to shorten up the code. Our final solution, took the form:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">a</span> <span class="o">=</span> <span class="n">b</span> <span class="o">=</span> <span class="mi">1</span>
</span><span class='line'><span class="n">sum</span> <span class="o">=</span> <span class="mi">0</span>
</span><span class='line'><span class="k">while</span> <span class="n">b</span> <span class="o"><</span> <span class="mi">4_000_000</span>
</span><span class='line'> <span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="o">=</span> <span class="n">b</span><span class="p">,</span> <span class="n">a</span><span class="o">+</span><span class="n">b</span>
</span><span class='line'> <span class="n">sum</span> <span class="o">+=</span> <span class="n">b</span> <span class="k">if</span> <span class="n">b</span><span class="o">.</span><span class="n">even?</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'><span class="nb">puts</span> <span class="n">sum</span>
</span></code></pre></td></tr></table></div></figure>
<p>While this is a good solution. It wasn’t very re-useable. So to make it more
re-useable we wrapped it in a method:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">def</span> <span class="nf">even_fibonnaci_sum</span><span class="p">(</span><span class="n">upper_limit</span><span class="p">)</span>
</span><span class='line'> <span class="n">a</span> <span class="o">=</span> <span class="n">b</span> <span class="o">=</span> <span class="mi">1</span>
</span><span class='line'> <span class="n">sum</span> <span class="o">=</span> <span class="mi">0</span>
</span><span class='line'> <span class="k">while</span> <span class="n">b</span> <span class="o"><</span> <span class="n">upper_limit</span>
</span><span class='line'> <span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="o">=</span> <span class="n">b</span><span class="p">,</span> <span class="n">a</span><span class="o">+</span><span class="n">b</span>
</span><span class='line'> <span class="n">sum</span> <span class="o">+=</span> <span class="n">b</span> <span class="k">if</span> <span class="n">b</span><span class="o">.</span><span class="n">even?</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'> <span class="n">sum</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>
<p><span class='pullquote-right' data-pullquote='So at this point we took
a step back, and asked: “What is the code that we wish we could write?”'>
That’s great. Now we can re-use it anywhere, and we are no longer locked into a
<code>4_000_000</code> cap. However, the
<a href="http://en.wikipedia.org/wiki/Fibonacci_number">Fibonnaci</a> sequence is very
common. And this method doesn’t let us re-use that. So at this point we took
a step back, and asked: “What is the code that we wish we could write?” The
answer looked something like:
</span></p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="no">Fibonnaci</span><span class="o">.</span><span class="n">upto</span><span class="p">(</span><span class="mi">4_000_000</span><span class="p">)</span><span class="o">.</span><span class="n">select</span><span class="p">(</span><span class="o">&</span><span class="ss">:even?</span><span class="p">)</span><span class="o">.</span><span class="n">sum</span>
</span></code></pre></td></tr></table></div></figure>
<p>A nice little one-liner. And it’s very clear what we are doing. In English,
this would read: <em>“For Fibonnaci numbers up to 4,000,000. Take the even numbers
and sum them.”</em></p>
<p>Sadly, there is no Fibonnaci number generator, but we can
<a href="http://www.dev.gd/20130114-building-sequences-with-enumerator.html">build sequences</a>.
In fact the Ruby docs show an example of how to create one
<a href="http://ruby-doc.org/core-1.9.3/Enumerator.html#method-c-new">Fibonacci Generator</a>.</p>
<p>So we coded one up:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">def</span> <span class="nf">Fibonnaci</span>
</span><span class='line'> <span class="no">Enumerator</span><span class="o">.</span><span class="n">new</span> <span class="k">do</span> <span class="o">|</span><span class="n">y</span><span class="o">|</span>
</span><span class='line'> <span class="n">a</span> <span class="o">=</span> <span class="n">b</span> <span class="o">=</span> <span class="mi">1</span>
</span><span class='line'> <span class="kp">loop</span> <span class="k">do</span>
</span><span class='line'> <span class="n">y</span> <span class="o"><<</span> <span class="n">a</span>
</span><span class='line'> <span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="o">=</span> <span class="n">b</span><span class="p">,</span> <span class="n">a</span> <span class="o">+</span> <span class="n">b</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>
<p>Great! We have an <a href="http://ruby-doc.org/core-1.9.3/Enumerator.html"><code>Enumerator</code></a>,
but there’s no <code>upto</code>. So we created one:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">module</span> <span class="nn">Sequentially</span>
</span><span class='line'> <span class="k">def</span> <span class="nf">upto</span><span class="p">(</span><span class="n">limit</span><span class="p">,</span> <span class="o">&</span><span class="n">block</span><span class="p">)</span>
</span><span class='line'> <span class="n">enum</span> <span class="o">=</span> <span class="no">Enumerator</span><span class="o">.</span><span class="n">new</span> <span class="k">do</span> <span class="o">|</span><span class="n">y</span><span class="o">|</span>
</span><span class='line'> <span class="n">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">num</span><span class="o">|</span>
</span><span class='line'> <span class="k">break</span> <span class="k">unless</span> <span class="n">num</span> <span class="o"><</span> <span class="n">limit</span>
</span><span class='line'> <span class="n">y</span> <span class="o"><<</span> <span class="n">num</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'>
</span><span class='line'> <span class="n">block</span> <span class="p">?</span> <span class="n">enum</span><span class="o">.</span><span class="n">each</span><span class="p">(</span><span class="o">&</span><span class="n">block</span><span class="p">)</span> <span class="p">:</span> <span class="n">enum</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>
<p>And updated the Fibonnaci generator accordingly:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">def</span> <span class="nf">Fibonnaci</span>
</span><span class='line'> <span class="no">Enumerator</span><span class="o">.</span><span class="n">new</span><span class="p">{</span> <span class="o">|</span><span class="n">y</span><span class="o">|</span>
</span><span class='line'> <span class="c1"># see above for meat</span>
</span><span class='line'> <span class="p">}</span><span class="o">.</span><span class="n">extend</span> <span class="no">Sequentially</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>
<p>Sweet! <code>select</code> will work just fine. However, <code>sum</code> doesn’t exist (at least not
outside Rails). So, let’s patch that in.</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">module</span> <span class="nn">Enumerable</span>
</span><span class='line'> <span class="k">def</span> <span class="nf">sum</span>
</span><span class='line'> <span class="n">reduce</span><span class="p">(</span><span class="ss">:+</span><span class="p">)</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>
<p>So this final solution is <strong><em>a lot longer</em></strong>. However, it is composed of very
re-usable parts that we can continue to use for more
<a href="http://projecteuler.net/">Project Euler</a> puzzles. Put that all together and
you have the elegant solution:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">module</span> <span class="nn">Enumerable</span>
</span><span class='line'> <span class="k">def</span> <span class="nf">sum</span>
</span><span class='line'> <span class="n">reduce</span><span class="p">(</span><span class="ss">:+</span><span class="p">)</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="k">module</span> <span class="nn">Sequentially</span>
</span><span class='line'> <span class="k">def</span> <span class="nf">upto</span><span class="p">(</span><span class="n">limit</span><span class="p">,</span> <span class="o">&</span><span class="n">block</span><span class="p">)</span>
</span><span class='line'> <span class="n">enum</span> <span class="o">=</span> <span class="no">Enumerator</span><span class="o">.</span><span class="n">new</span> <span class="k">do</span> <span class="o">|</span><span class="n">y</span><span class="o">|</span>
</span><span class='line'> <span class="n">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">num</span><span class="o">|</span>
</span><span class='line'> <span class="k">break</span> <span class="k">unless</span> <span class="n">num</span> <span class="o"><</span> <span class="n">limit</span>
</span><span class='line'> <span class="n">y</span> <span class="o"><<</span> <span class="n">num</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'>
</span><span class='line'> <span class="n">block</span> <span class="p">?</span> <span class="n">enum</span><span class="o">.</span><span class="n">each</span><span class="p">(</span><span class="o">&</span><span class="n">block</span><span class="p">)</span> <span class="p">:</span> <span class="n">enum</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'><span class="k">def</span> <span class="nf">Fibonnaci</span>
</span><span class='line'> <span class="no">Enumerator</span><span class="o">.</span><span class="n">new</span><span class="p">{</span> <span class="o">|</span><span class="n">y</span><span class="o">|</span>
</span><span class='line'> <span class="n">a</span> <span class="o">=</span> <span class="n">b</span> <span class="o">=</span> <span class="mi">1</span>
</span><span class='line'> <span class="kp">loop</span> <span class="k">do</span>
</span><span class='line'> <span class="n">y</span> <span class="o"><<</span> <span class="n">a</span>
</span><span class='line'> <span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="o">=</span> <span class="n">b</span><span class="p">,</span> <span class="n">a</span> <span class="o">+</span> <span class="n">b</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'> <span class="p">}</span><span class="o">.</span><span class="n">extend</span> <span class="no">Sequentially</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="no">Fibonnaci</span><span class="o">.</span><span class="n">upto</span><span class="p">(</span><span class="mi">4_000_000</span><span class="p">)</span><span class="o">.</span><span class="n">select</span><span class="p">(</span><span class="o">&</span><span class="ss">:even?</span><span class="p">)</span><span class="o">.</span><span class="n">sum</span>
</span></code></pre></td></tr></table></div></figure>
]]></content>
</entry>
</feed>