-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
1995 lines (1429 loc) · 150 KB
/
index.html
File metadata and controls
1995 lines (1429 loc) · 150 KB
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
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<!--[if IEMobile 7 ]><html class="no-js iem7"><![endif]-->
<!--[if lt IE 9]><html class="no-js lte-ie8"><![endif]-->
<!--[if (gt IE 8)|(gt IEMobile 7)|!(IEMobile)|!(IE)]><!--><html class="no-js" lang="en"><!--<![endif]-->
<head>
<meta charset="utf-8">
<title>Thoughts & Technical Writings.</title>
<meta name="author" content="Matt Campbell">
<meta name="description" content="Original post: http://bluematt.bitcoin.ninja/2017/02/28/bitcoin-trustlessness/ While there is arguably some trust in miners required to ensure the …">
<!-- http://t.co/dKP3o1e -->
<meta name="HandheldFriendly" content="True">
<meta name="MobileOptimized" content="320">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="canonical" href="http://mattcampbell.nyc/">
<link href="/favicon.png" rel="icon">
<link href="/stylesheets/screen.css" media="screen, projection" rel="stylesheet" type="text/css">
<link href="/atom.xml" rel="alternate" title="Thoughts & Technical Writings." type="application/atom+xml">
<script src="/javascripts/modernizr-2.0.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>!window.jQuery && document.write(unescape('%3Cscript src="/javascripts/libs/jquery.min.js"%3E%3C/script%3E'))</script>
<script src="/javascripts/octopress.js" type="text/javascript"></script>
<!--Fonts from Google"s Web font directory at http://google.com/webfonts -->
<link href="http://fonts.googleapis.com/css?family=PT+Serif:regular,italic,bold,bolditalic" rel="stylesheet" type="text/css">
<link href="http://fonts.googleapis.com/css?family=PT+Sans:regular,italic,bold,bolditalic" rel="stylesheet" type="text/css">
</head>
<body >
<header role="banner"><div class="jumbotron">
<div class="container">
Bitcoin maximalist.
<h3 class="tagline">Thoughts & Technical Writings.</h3>
</div>
</div>
</header>
<nav role="navigation"><ul class="subscription" data-subscription="rss">
<li><a href="/atom.xml" rel="subscribe-rss" title="subscribe via RSS">RSS</a></li>
</ul>
<form action="http://google.com/search" method="get">
<fieldset role="search">
<input type="hidden" name="sitesearch" value="mattcampbell.nyc">
<input class="search" type="text" name="q" results="0" placeholder="Search"/>
</fieldset>
</form>
<ul class="nav">
<li class="active"><a href="/index.html">Home</a></li>
<li ><a href="/blog/archives/index.html">Archives</a></li>
</ul>
</nav>
<div id="main">
<div id="content">
<div class="blog-index">
<article>
<header>
<h1 class="entry-title"><a href="/2017/03/04/responses-to-bitcoins-diversity-of-use-cases-and-security-models/">Responses to Bitcoin’s Diversity of Use-Cases and Security Models</a></h1>
<p class="meta">
<time class='entry-date' datetime='2017-03-04T15:01:41-05:00'><span class='date'><span class='date-month'>Mar</span> <span class='date-day'>4</span><span class='date-suffix'>th</span>, <span class='date-year'>2017</span></span> <span class='time'>3:01 pm</span></time>
| <a href="/2017/03/04/responses-to-bitcoins-diversity-of-use-cases-and-security-models/#disqus_thread"
data-disqus-identifier="http://mattcampbell.nyc/2017/03/04/responses-to-bitcoins-diversity-of-use-cases-and-security-models/">Comments</a>
</p>
</header>
<div class="entry-content"><p>Original post: <a href="http://bluematt.bitcoin.ninja/2017/02/28/bitcoin-trustlessness/">http://bluematt.bitcoin.ninja/2017/02/28/bitcoin-trustlessness/</a></p>
<blockquote><p>While there is arguably some trust in miners required to ensure the entirety of the blockchain isn’t reorganized</p></blockquote>
<p>Why are you singling out miners? The 51% attack vector is neither exclusive to nor more likely to be pursued by miners. Anyone can incur the PoW cost to mine an alternative version of history (alternative blockchain) and propagate those blocks to nodes. In fact, I would argue that miners are less likely than non-miners to pursue a 51% attack since they are in an extremely competitive business.</p>
<blockquote><p>Of course to ensure you aren’t trusting miners and pools to secure their operations perfectly</p></blockquote>
<p>What does this mean? If your point is that one should wait for <code>n</code> confirmations before assuming your transaction is valid, that is incorrect. Miners have no role in validating transactions (full nodes do that); they just choose which already-validated transactions should be put in the next block. If they try to put invalid transactions in a block, then that block will be rejected and their mining reward foregone.</p>
<blockquote><p>Clearly a payment system which requires a week or more for payment to clear would not be able to compete with much faster alternatives.</p></blockquote>
<p>This is not an apples to apples comparison. Many people seem to want to compare Bitcoin to Visa, but that is a trap. After SegWit is activated, Bitcoin will <em>enable</em> a trustless, instantaneous and secure peer-to-peer payment network like Lightning, but Bitcoin itself will never be that payment network by design. Acheiving decentralized consensus via PoW and fully-validating nodes requires a massive amount of redundant messaging to propagate throughout the network, which is not a design that lends itself to high throughput for the network at large.</p>
<blockquote><p>Many investors who care strongly about Bitcoin’s scarcity properties are happy to trust centralized third-parties in the form of Bitcoin exchanges and “Bitcoin banks”.</p></blockquote>
<p>That trust is misplaced. They should not keep coins at an exchange (see: all the exchange hacks) and should certainly not get anywhere near something called a “Bitcoin bank” since in Bitcoin users <em>are</em> their own bank. Bitcoin is all about financial disintermediation.</p>
<blockquote><p>Many Bitcoin users who want fast payments for medium- to small-value transactions are happy to trust miners, in sufficient measure.</p></blockquote>
<p>In what way are they trusting miners? Again, miners do not validate transactions, they just decide which transactions to put in the next block.</p>
<blockquote><p>Such trust relationships, as long as users aren’t forced into them (either by explicit requirement or sufficiently strong financial incentive), can provide significantly better user experience through faster, cheaper, and more user-friendly transactions.</p></blockquote>
<p>Aren’t you saying that users <em>choose</em> such a trusted relationship? One would think they do this because of a strong financial incentive, e.g. 0 confirmations at one time was considering a value-add feature for payment processors to attract users, but the payment processor would have to trust the user to not double spend. Can you give an example of what you’re describing in this paragraph?</p>
<blockquote><p> (eg trusting miners or developers to keep the 21 million Bitcoin limit)</p></blockquote>
<p>Neither of these make sense to me. Developers do not own the monopology on total number of Bitcoins in circulation, clearly. What does ‘trusting miners’ mean?</p>
<blockquote><p>Bitcoin must only change by consensus of its ever-growing userbase.</p></blockquote>
<p>Bitcoin <em>is</em> consensus, and that consensus is determined by the software being run primarily by fully-validating nodes, and additionaly to some extent by miners & wallets/clients which relay new transactions to the network. Bitcoin does not care about whether or not people agree or not.</p>
<blockquote><p>Putting all of this together we see a picture of where Bitcoin must evolve if it wants to retain its trustless properties while providing a usable system for its many, vastly divergent, use-cases.</p></blockquote>
<p>That step in the evolutino is already here and it’s called SegWit. So, run a SegWit full node and help us move towards that vision.</p>
<blockquote><p>or even a trusted “Bitcoin bank”.</p></blockquote>
<p>This is not one of the divergent use cases you mention that are relevant or specific to Bitcoin. If you want to trust a 3rd party, you don’t need (and shouldn’t use) Bitcoin.</p>
<blockquote><p>Users which do not even want to trust miners should be free to do so, placing their transactions on the blockchain and waiting weeks to ensure even future hashpower attacks will not reverse them</p></blockquote>
<p>What? I’m lost. By “placing their transactions on the blockchain” are you talking about running one’s own mining equipment? And, again, you aren’t trusting miners. Miners expend energy to perform proof-of-work; an attacker trying to double spend needs to <em>redo</em> all of the work beteween (a) the block that includes the double spend transaction(s), and (b) the most recent block in the chain.</p>
<blockquote><p>the community of Bitcoin users must continue to enforce that changes happen only through consensus among the ever-broadening group</p></blockquote>
<p>What community? Whom are you addressing? Bitcoin is about <em>software running on machines</em>, not people’s opinions about what is good or bad for Bitcoin.</p>
<blockquote><p>Critically, this means that all changes which do not harm the utility of Bitcoin for any of its many use-cases, while helping others, should be made, wherever possible.</p></blockquote>
<p>This is the exact thing people are disagreeing about currently vis-a-vis SegWit. And since SegWit is an opt-in backwards compatible soft fork that preserves the trustless, decentralized model, it should be activated.</p>
</div>
</article>
<article>
<header>
<h1 class="entry-title"><a href="/2017/02/17/bitcoin-the-freedom-fighter/">Bitcoin the Freedom Fighter</a></h1>
<p class="meta">
<time class='entry-date' datetime='2017-02-17T21:36:41-05:00'><span class='date'><span class='date-month'>Feb</span> <span class='date-day'>17</span><span class='date-suffix'>th</span>, <span class='date-year'>2017</span></span> <span class='time'>9:36 pm</span></time>
| <a href="/2017/02/17/bitcoin-the-freedom-fighter/#disqus_thread"
data-disqus-identifier="http://mattcampbell.nyc/2017/02/17/bitcoin-the-freedom-fighter/">Comments</a>
</p>
</header>
<div class="entry-content"><p>Bitcoin represents a mental stepping stone, a philosophical spring board from which I’m better able to envision a future not controlled by the State and its numerous appendages (the FBI, CIA) and countless subsidiaries. By breaking the link between money and sovereignty (there are neither Presidents nor sovereigns printed on bitcoins), Bitcoin paves the way for a tidal wave of cultural, social, and technological innovation and progress. Since Bitcoin offers an Internet of Money with no inherent overlap with existing (i.e. fiat) monetary and financial systems, over time as adoption rises, people will be motivated to keep an ever-increasing proportion of their value in Bitcoins <strong>because it is better money.</strong> This last point is not debatable; it is not an opinion, it is not fake news, it is fact. Fiat money is not yours (aside from cash; but what portion of your value do you hold in cash?) - it is merely an IOU, a ledger balance. When you log into your online banking (assuming you live in one of the advanced economies where we have access to such financial services), the money that you see and claim to “have in your account” has in reality been lent out or applied to some probably more risky strategy like trading or financing of some exotic, complicated deal loaded with tantalizing fees for the bankers involved. If - or rather, when, given the history of financial crises, which seem to be part of the human economic condition - there is a run on the bank and the FDIC, you will be forced to get in line with all the rest (and probably take a massive haircut, i.e you will lose most or all of the money held in bank accounts).</p>
<p>This is simply not the case with Bitcoin. You <strong><em>are</em></strong> your bank. Bitcoin is strictly a bearer asset, where possession of the private key that allows your coins to be spent is ten-tenths of the law, so to speak.</p>
<blockquote><p>But, bitcoins are not very useful today. I have to wait at least 20 minutes for my transactions to be confirmed; transaction fees are too high, I pay nothing to use my credit cards. Why would <em>anyone ever use Bitcoin</em>?</p></blockquote>
<p>Ask the people in Africa, South America, East Asia, and so on, where each morning you wake up and hope that your grab bag of problems created by Big Brother doesn’t have a surprise waiting for you. That list includes things like profound corruption & graft, the social strangehold wraught by nepotism at all levels of socieyt, hyperinflation of your currency that destroys your purchasing power on a daily basis, oppressive political regimes which rob & pillage the people, and so on.</p>
<p>If, 10 years ago before Bitcoin was invented, someone has asked you the following question, how would you have responded?</p>
<blockquote><p>How long would it take to design an Internet of Money which is not controlled by any central arbiter, no government, no corporation; a freely accessible network that cuts out the banks and turns the global financial-monetary system on its head and renders it utterly useless?</p></blockquote>
<p>I suspect you’d either laugh, stare, or walk away, thinking something like, “Dreamers, who needs ‘em.” We all would have. Bitcoin has existed for 8 short years and it’s already significantly better than SWIFT, ACH, and most bank transfer services around the world.</p>
<p>In 10 years, maybe 5, all the “problems” that exist today with Bitcoin will be solved. Lightning Network is coming, SegWit will be adopted, and the impact of Bitcoin’s open, permissionsless protocol allowing for <a href="https://www.youtube.com/watch?v=n2yD1PSWBU0">exponential innovation</a> will render all the world’s fiat currencies laughably, hopelessly obsolete with no shot of ever catching up. From there, the virtuous cycle of utility begetting adoption <em>ad infinitum</em> will kick in, and then it’s simply game over for the incumbents.</p>
<p>So, what happens to things like taxes when, in the future, everyone is storing their value in Bitcoin, where it <a href="http://www.libertylifetrail.com/2016/04/04/the-top-use-cases-for-bitcoin/">is easy to hide it from the prying eyes of the State and even your spouse</a>? It will trigger a forced revolution, a painful, messy, chaotic breaking with the past - a cultural & social forest fire which will wipe out the old guard and the incumbents, anyone standing in the way of progress, and will give the future over to the nerds and to the Individual. Power structures like governments will no longer be able to domineer over their “subjects” by dictating and controlling what they can (buy things at Wal-Mart, please!) and cannot (that politician is evil, and since you donated to her you must be, too!) do with your money. The State and any centralized entity will only be able to grow as large as the exact number of Individuals which explicitly “vote” to support that structure, where voting is represented as financial support - buying a product from a company, paying for a service, donations, or even paying taxes if you’d like.</p>
<p>The future is bright for the Individual, but the transition will be difficult. And we really haven’t even begun yet. Remember: <a href="http://www.kurzweilai.net/the-law-of-accelerating-returns">all exponential relationships appear to be linear at the beginning</a>. I hope I live long enough to see the revolution in full swing.</p>
</div>
</article>
<article>
<header>
<h1 class="entry-title"><a href="/2016/11/25/bitcoin-and-taxes/">Bitcoin and Taxes (Your Favorite Topic!)</a></h1>
<p class="meta">
<time class='entry-date' datetime='2016-11-25T12:18:58-05:00'><span class='date'><span class='date-month'>Nov</span> <span class='date-day'>25</span><span class='date-suffix'>th</span>, <span class='date-year'>2016</span></span> <span class='time'>12:18 pm</span></time>
| <a href="/2016/11/25/bitcoin-and-taxes/#disqus_thread"
data-disqus-identifier="http://mattcampbell.nyc/2016/11/25/bitcoin-and-taxes/">Comments</a>
</p>
</header>
<div class="entry-content"><blockquote><p>“Each $5,000 of annual tax payments made over a 40-year period reduces your net worth by $2.2 million assuming a 10% annual return on your investments,” reports James Dale Davidson in <a href="https://www.amazon.com/The-Sovereign-Individual-Transition-Information/dp/0684832720"><em>The Sovereign Individual: Mastering the Transition to the Information Age</em></a></p></blockquote>
<p>Just let that sink in for a bit please. Mull over the figures in your delicate psyche. Imagine the impact of $2.2mm on your future.</p>
<p>… now, who’s ready to talk about taxes?!</p>
<h3>Is Bitcoin useful for tax evasion?</h3>
<p><small>(<strong>NOTE</strong>: This is not tax advice, nor am I suggesting you seek to evade paying taxes. This post is for entertainment purposes only.)</small></p>
<p>There are two types of taxation regimes the world over: You either live in a country with <strong>competent</strong> or <strong>incompetent</strong> tax authorities.</p>
<p>If incompetent, there are a dozen other ways to avoid paying taxes, probably all of them easier than Bitcoin.</p>
<p>The United States is certainly one of the more competent tax collectors in the world via its ‘revenue’ (talk about a euphemism) arm, the IRS.</p>
<p>So our question is really: is it possible to use bitcoin for tax evasion within the US?</p>
<h3>What can the IRS <em>do</em>, exactly?</h3>
<p>As of today, the IRS simply <a href="https://bitcoinmagazine.com/articles/tax-day-is-coming-a-primer-on-bitcoin-and-taxes-1459786613">hopes that you report any capital gains on profits obtained on net BTC purchases</a>. However, it’s clear the IRS is trying to move from ‘hope’ to ‘coerce’ as its policy verb of choice: they’ve <a href="http://www.coindesk.com/irs-seeking-data-coinbases-bitcoin-customers/">just asked for a broad and sweeping disclosure from Coinbase</a> regarding its customers’ historical transaction data. Should this data be handed over to the Feds, you can bet they will start building a ledger for each customer - purchases made (volume and price) net of sales (again, volume and price) and send you a capital gains bill for any PNL realized on those transactions.</p>
<p>However, this doesn’t address situations in which you’ve used Coinbase (or another exchange like <a href="https://gemini.com/">Gemini</a>) solely to purchase your coins, and immediately withdrawn them to your personal, private wallet.</p>
<p>What will the IRS’ policy be regarding these murky situations? Remember, I’m saying ‘murky’ here because <strong>they do not know the fate of your withdrawn bitcoins</strong>. That means that you could have:</p>
<ul>
<li>lost your coins -> you’d claim a capital <strong>loss</strong> in this case, since the capital you outlaid upfront is now worth exactly $0</li>
<li>held onto your coins -> you’d report nothing as you only incur capital gains upon <em>sale</em> of your investment</li>
<li>sold your coins -> you’re supposed to report this honestly and pay capital gains</li>
</ul>
<p>As you can see, the main issue here for the IRS is their <strong>total and utter lack of transparency</strong>. Bitcoin does not need the IRS’ nor the Federal government’s blessing to exist - it just does. It is a completely separate, autonomous payment network that is under the control of no single individual or entity. There is no one rubber-stamping transactions like the Federal Reserve does in this country.</p>
<p>Some open questions, however, remain regarding the extent to which the IRS could pursue an individual (i.e. audit them) if they suspect misreporting of capital gains/losses regarding cryptocurrency holdings. Will they conduct rigorous <a href="http://www.coindesk.com/what-block-chain-analysis-tells-bitcoin/">blockchain analysis</a> - which is extremely computationally expensive (by design, in a way… more on this in another post perhaps) - to try and piece together a story and send you a tax bill? This strategy is feasible in theory, but would represent a huge consumption of their resources in practice.</p>
<p>The fact is that the tools at the disposal of the IRS are limited at best.</p>
<p>Perhaps, though, this is for the better.</p>
<h2>A better tax regime</h2>
<p>In bitcoin, you pay taxes on every transaction - it’s called the transaction fee. You can <em>voluntarily</em> pay a higher fee and you will increase the likelihood that your (probably time-sensitive) transaction will be part of the network’s next block, i.e. your transaction will be settled in a much more timely manner. This is an example of a <a href="https://en.wikipedia.org/wiki/Voluntary_taxation">voluntary tax system</a> (credit to <a href="https://twitter.com/TravisPatron">@TravisPatron</a> for <a href="http://www.coindesk.com/why-bitcoin-creates-a-voluntary-tax-system/">exposing me to this concept</a>).</p>
<blockquote><p>[The Bitcoin] transactions queue represents a voluntary, pay-for-performance taxation structure where the performance derived from the system is dependent upon how much taxation [a user is willing to] pay.</p></blockquote>
<p>Bitcoin puts the power back in the hands of its users. If you value the service, the right to use the Bitcoin network to move value between two nodes in the network, then you pay tax for that performance.</p>
<p>Imagine a system where you pay tax for roads only when you use the roads to drive. Or you only pay taxes to schools in the form of some tuition cost when your children are attending those schools.</p>
<p>I think that a tax regime that aligns a person’s tax liabilities with his or her <em>behaviors</em> (where a behavior is an expression of their preference) is a compelling vision for the future. Perhaps you will agree.</p>
</div>
</article>
<article>
<header>
<h1 class="entry-title"><a href="/2016/10/26/world-wide-web-coin-a-thought-experiment/">WorldWideWebCoin: A Thought Experiment</a></h1>
<p class="meta">
<time class='entry-date' datetime='2016-10-26T14:18:58-04:00'><span class='date'><span class='date-month'>Oct</span> <span class='date-day'>26</span><span class='date-suffix'>th</span>, <span class='date-year'>2016</span></span> <span class='time'>2:18 pm</span></time>
| <a href="/2016/10/26/world-wide-web-coin-a-thought-experiment/#disqus_thread"
data-disqus-identifier="http://mattcampbell.nyc/2016/10/26/world-wide-web-coin-a-thought-experiment/">Comments</a>
</p>
</header>
<div class="entry-content"><p><sub>
<em>WorldWideWebCoin - WWWCoin vs. Bitcoin - More usefulness leads to more adoption leads to more usefulness</em>
</sub></p>
<p>Bitcoin is <a href="https://www.coinbase.com/what-is-bitcoin?locale=en">many things</a> - open source peer-to-peer (p2p) digital money; an <a href="http://fortune.com/2016/10/18/winklevoss-state-street-bitcoin-etf-administrator/">alternative asset class for investors</a>; and possibly the global reserve currency of the future, when <a href="https://startupboy.com/2014/04/01/the-fifth-protocol/">machines are participating in marketplaces autonomously without human intervention</a>.</p>
<p>The value of one Bitcoin (1 BTC) in essence is a reflection of its <strong>usefulness</strong> across the many functions in which it’s being used.</p>
<p>Bitcoin starts to look more like a safe haven asset that’s decorrelated with, say, the S&P500? Its value should increase.</p>
<p>Bitcoin can be used more and more places to buy goods and services? Its value should increase.</p>
<p>More companies offer employees compensation partially of wholly in Bitcoin? Its value should increase.</p>
<h2>WWWCoin Hypothetical Price History</h2>
<p>Let’s say there had been a <strong>WWWCoin</strong> back in 1994 shortly after Tim Burners-Lee <a href="https://en.wikipedia.org/wiki/World_Wide_Web">invented the World Wide Web</a> in the late 1980s.</p>
<p>Now picture a chart of that coin’s valuation from its inception in c. 1989; through the <a href="https://en.wikipedia.org/wiki/Dot-com_bubble">dot-com bubble’s bursting</a> beginning in 2000; and finally bring it through to the present. What would that chart look like? Here’s my take…</p>
<div style="text-align:center">
<img src="https://www.dropbox.com/s/k30yfttch1o54yl/Screenshot%202016-10-26%2016.24.53.png?dl=1"/>
</div>
<h2>Bitcoin Realized Price History</h2>
<p>Just for fun, let’s see how our hypothetical WWWCoin chart compares to the realized BTC price chart to date.</p>
<div style="text-align:center">
<img src="https://www.dropbox.com/s/d5ukaisoi2f194n/Screenshot%202016-10-26%2015.29.20.png?dl=1"/>
</div>
<p>Some similarities of note:</p>
<ul>
<li>Early precipitous rise leads to a bubble, i.e. overdone price increase relative to the <strong>actual + potential usefulness of both the internet and Bitcoin</strong></li>
<li>Steady, grinding recovery following the first major crash in price as <strong>the technology continues to make strides in terms of usefulness and applicability</strong></li>
<li><strong>No clear upper bound on price as usefulness enters into a virtuous feedback loop</strong> - more adoption means more usefulness means more adoption, ad nauseam.</li>
</ul>
<p>The last point has only been realized to date in the case of the Internet; my prediction is Bitcoin will follow a similar pattern. Have you heard of <a href="https://en.wikipedia.org/wiki/Micropayment">micropayments</a>? The technology that will finally <a href="https://medium.com/on-blendle/in-a-world-of-ad-blockers-we-need-micropayments-9ddb2f6793cf#.cn7bn57v8">kill YouTube ads</a>? Well, they’re almost here, courtesy of Bitcoin and the <a href="https://lightning.network/">Lightning Network</a>.</p>
<p>I think we’re standing on the precipice of an inflection point in Bitcoin’s history - at which the feedback between Bitcoin’s expanding utility and increasing price becomes obvious to just about everyone. Do you want to participate or not?</p>
</div>
</article>
<article>
<header>
<h1 class="entry-title"><a href="/2016/02/17/private-methods-can-save-some-tdd-headache/">Private Methods Can Save Some TDD Headache</a></h1>
<p class="meta">
<time class='entry-date' datetime='2016-02-17T16:18:58-05:00'><span class='date'><span class='date-month'>Feb</span> <span class='date-day'>17</span><span class='date-suffix'>th</span>, <span class='date-year'>2016</span></span> <span class='time'>4:18 pm</span></time>
| <a href="/2016/02/17/private-methods-can-save-some-tdd-headache/#disqus_thread"
data-disqus-identifier="http://mattcampbell.nyc/2016/02/17/private-methods-can-save-some-tdd-headache/">Comments</a>
</p>
</header>
<div class="entry-content"><p>Let’s face it: TDD is hard. I would argue that there’s value in that difficulty - TDD forces you to address domain modeling decisions early on; to declare your API before implementing it. I have spent more time than I care to admit staring at some <code>*_spec.rb</code> file, unsure about how to translate the loosely-defined conception of behavior in my brain into test examples.</p>
<p>However, there’s good <a href="news:">news:</a> relying on private methods can some (some) of this pain.</p>
<h2>A Rails Example</h2>
<p>Let’s say your co-worker has asked you to write a webhooks microservice. This standalone API will be responsible for:</p>
<ol>
<li>Creating new callback subscriptions for users that want to subscribe</li>
<li>Sending the callback payloads to said user’s callback(s) URL(s)</li>
</ol>
<blockquote><p>‘A crucial part of any callbacks API is delivering, over the web via HTTP POST, the actual callback payload,’ you reason aloud.</p></blockquote>
<p>To that end, you conjure up a vision of some <code>DeliverCallbackJob</code> class that needs only a <code>User</code> instance and the callback payload (probably a long string of JSON, like a serialized web response). Following a common pattern, you decide the interface for this class will be limited to one 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>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="no">DeliverCallbackJob</span><span class="c1">#perform!</span>
</span></code></pre></td></tr></table></div></figure>
<p>In adherence to TDD, we may start with an example (RSpec DSL here) that looks 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>
<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>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="no">RSpec</span><span class="o">.</span><span class="n">describe</span> <span class="no">DeliverCallbackJob</span> <span class="k">do</span>
</span><span class='line'> <span class="c1"># NOTE: some/much setup code omitted...</span>
</span><span class='line'>
</span><span class='line'> <span class="n">let</span><span class="p">(</span><span class="ss">:deliverer</span><span class="p">)</span> <span class="p">{</span> <span class="no">DeliverCallbackJob</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="n">user</span><span class="p">,</span> <span class="n">payload</span><span class="p">)</span> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'> <span class="n">describe</span> <span class="s2">"#perform!"</span> <span class="k">do</span>
</span><span class='line'> <span class="n">subject</span> <span class="p">{</span> <span class="n">deliverer</span><span class="o">.</span><span class="n">perform!</span> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'> <span class="n">it</span> <span class="s2">"sends a callback to all of the user's associated Callback#callback_url endpoints"</span> <span class="k">do</span>
</span><span class='line'> <span class="n">expect</span><span class="p">(</span><span class="no">Net</span><span class="o">::</span><span class="no">HTTP</span><span class="p">)</span><span class="o">.</span><span class="n">to</span> <span class="n">receive</span><span class="p">(</span><span class="ss">:post</span><span class="p">)</span><span class="o">.</span><span class="n">exactly</span><span class="p">(</span><span class="n">user</span><span class="o">.</span><span class="n">callbacks</span><span class="o">.</span><span class="n">size</span><span class="p">)</span><span class="o">.</span><span class="n">times</span>
</span><span class='line'> <span class="n">subject</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>Now, <code>User#callbacks</code> is a standard <code>ActiveRecord::Associations::ClassMethods#has_many</code> association. So we may be tempted to now drop into our class definition and start implementing:</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="c1">#NOTE: This is pseudo code! Read between the lines somewhat please :)</span>
</span><span class='line'>
</span><span class='line'><span class="k">class</span> <span class="nc">DeliverCallbackJob</span>
</span><span class='line'> <span class="k">def</span> <span class="nf">perform!</span>
</span><span class='line'> <span class="n">user</span><span class="o">.</span><span class="n">callbacks</span><span class="o">.</span><span class="n">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">callback</span><span class="o">|</span>
</span><span class='line'> <span class="no">Net</span><span class="o">::</span><span class="no">HTTP</span><span class="o">.</span><span class="n">post</span><span class="p">(</span><span class="n">callback</span><span class="o">.</span><span class="n">callback_url</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 class="k">end</span>
</span></code></pre></td></tr></table></div></figure>
<h2>The Problem</h2>
<p>This works, yes - but we’ve unwittingly coupled logic unrelated to delivering a callback to this class. Specifically (and this can be hard to see in Rails a lot of the times), the usage of <code>user.callbacks</code> is the point of coupling. <code>DeliverCallbackJob</code> does not and should not care how we get a (possibly empty) list of callbacks from our <code>user</code> record.</p>
<h2>Re-writing our Test</h2>
<p>A better test example - one that would have exposed this tendency to couple to the ActiveRecord API - might read 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>
<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>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">describe</span> <span class="s2">"#perform!"</span> <span class="k">do</span>
</span><span class='line'> <span class="n">subject</span> <span class="p">{</span> <span class="n">deliverer</span><span class="o">.</span><span class="n">perform!</span> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'> <span class="n">let</span><span class="p">(</span><span class="ss">:size</span><span class="p">)</span> <span class="p">{</span> <span class="mi">4</span> <span class="p">}</span>
</span><span class='line'> <span class="n">let</span><span class="p">(</span><span class="ss">:callback</span><span class="p">)</span> <span class="p">{</span> <span class="n">double</span><span class="p">(</span><span class="ss">callback_url</span><span class="p">:</span> <span class="s2">"foo.bar.com/callback"</span><span class="p">)</span> <span class="p">}</span>
</span><span class='line'> <span class="n">let</span><span class="p">(</span><span class="ss">:callbacks</span><span class="p">)</span> <span class="p">{</span> <span class="nb">Array</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="n">size</span><span class="p">,</span> <span class="n">callback</span><span class="p">)</span> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'> <span class="n">before</span> <span class="p">{</span> <span class="n">expect</span><span class="p">(</span><span class="n">deliverer</span><span class="p">)</span><span class="o">.</span><span class="n">to</span> <span class="n">receive</span><span class="p">(</span><span class="ss">:callbacks</span><span class="p">)</span><span class="o">.</span><span class="n">and_return</span><span class="p">(</span><span class="n">callbacks</span><span class="p">)</span> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'> <span class="n">it</span> <span class="s2">"sends a callback to all of the user's associated Callback#callback_url endpoints"</span> <span class="k">do</span>
</span><span class='line'> <span class="n">expect</span><span class="p">(</span><span class="no">Net</span><span class="o">::</span><span class="no">HTTP</span><span class="p">)</span><span class="o">.</span><span class="n">to</span> <span class="n">receive</span><span class="p">(</span><span class="ss">:post</span><span class="p">)</span><span class="o">.</span><span class="n">with</span><span class="p">(</span><span class="s2">"foo.bar.com/callback"</span><span class="p">)</span><span class="o">.</span><span class="n">exactly</span><span class="p">(</span><span class="n">size</span><span class="p">)</span><span class="o">.</span><span class="n">times</span>
</span><span class='line'> <span class="n">subject</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>The key difference here is the assertion we’ve added to the <code>before</code> hook:</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">before</span> <span class="p">{</span> <span class="n">expect</span><span class="p">(</span><span class="n">deliverer</span><span class="p">)</span><span class="o">.</span><span class="n">to</span> <span class="n">receive</span><span class="p">(</span><span class="ss">:callbacks</span><span class="p">)</span><span class="o">.</span><span class="n">and_return</span><span class="p">(</span><span class="n">callbacks</span><span class="p">)</span> <span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>
<p>We’ve implicitly augmented the interface of <code>DeliverCallbackJob</code> by a method <code>#callbacks</code>.</p>
<p>This is a really good thing! Now, we have a place to put the logic of “gather all the <code>Callback</code> records against which I need to deliver the callback payload at hand”. And as such, we have a new point at which we can introduce stubs or mocks in our tests.</p>
<p>In order to get this spec passing, we might refactor our class like so:</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>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">DeliverCallbackJob</span>
</span><span class='line'> <span class="k">def</span> <span class="nf">perform!</span>
</span><span class='line'> <span class="n">callbacks</span><span class="o">.</span><span class="n">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">callback</span><span class="o">|</span>
</span><span class='line'> <span class="no">Net</span><span class="o">::</span><span class="no">HTTP</span><span class="o">.</span><span class="n">post</span><span class="p">(</span><span class="n">callback</span><span class="o">.</span><span class="n">callback_url</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="kp">private</span>
</span><span class='line'>
</span><span class='line'> <span class="k">def</span> <span class="nf">callbacks</span>
</span><span class='line'> <span class="c1"># This is now an implementation detail, as opposed to a closely coupled</span>
</span><span class='line'> <span class="c1"># and hidden dependency stowed away within the `perform!` method.</span>
</span><span class='line'>
</span><span class='line'> <span class="c1"># For now, we're using ActiveRecord so implementation may be unchaged</span>
</span><span class='line'> <span class="c1"># relative to our first version of this class... but if we drop Rails/AR</span>
</span><span class='line'> <span class="c1"># in the future, we have a clearly documented spot to change this logic.</span>
</span><span class='line'>
</span><span class='line'> <span class="n">user</span><span class="o">.</span><span class="n">callbacks</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>
<h2>Recap</h2>
<p>So what did we learn today? When you’re struggling to get through your TDD, it tends to indicate hidden coupling and dependencies. When you feel this happening, take a closer look at your implementation and see what can be moved into private methods. Then, in your test examples, make those dependencies explicit so that you can mock, stub, or whatever suits you.</p>
</div>
</article>
<article>
<header>
<h1 class="entry-title"><a href="/2015/12/05/extending-an-instance-to-dynamically-include/">Extend an Instance to Dynamically Include</a></h1>
<p class="meta">
<time class='entry-date' datetime='2015-12-05T13:52:30-05:00'><span class='date'><span class='date-month'>Dec</span> <span class='date-day'>5</span><span class='date-suffix'>th</span>, <span class='date-year'>2015</span></span> <span class='time'>1:52 pm</span></time>
| <a href="/2015/12/05/extending-an-instance-to-dynamically-include/#disqus_thread"
data-disqus-identifier="http://mattcampbell.nyc/2015/12/05/extending-an-instance-to-dynamically-include/">Comments</a>
</p>
</header>
<div class="entry-content"><p>Here’s a question for you lovely developers (and non-developers, too!) out there: how can we <strong>dynamically</strong> include behavior from a specific module at runtime? Put differently, is it possible to include one specific module’s behavior <strong>after</strong> a <code>class</code> definition has already been read in? The typical Ruby include-behavior-from-a-module pattern looks something 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>
<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">Employee</span>
</span><span class='line'> <span class="kp">attr_accessor</span> <span class="ss">:can_be_promoted_to_manager</span>
</span><span class='line'>
</span><span class='line'> <span class="kp">include</span> <span class="no">Trainable</span>
</span><span class='line'>
</span><span class='line'> <span class="o">.</span><span class="n">.</span><span class="o">.</span>
</span><span class='line'>
</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>… where <code>Trainable</code> might extend the behavior of <code>Employee</code> instances something 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>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">module</span> <span class="nn">Trainable</span>
</span><span class='line'> <span class="k">def</span> <span class="nf">train!</span>
</span><span class='line'> <span class="c1">## some training-related code here...</span>
</span><span class='line'> <span class="nb">self</span><span class="o">.</span><span class="n">can_be_promoted_to_manager</span> <span class="o">=</span> <span class="kp">true</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>This is a contrived example, clearly, but the pattern should look familiar: in Ruby, we love to <a href="https://en.wikipedia.org/wiki/Encapsulation_(computer_programming)">encapsulate behavior</a> into modules and <code>include</code> them - the name for this practice is <a href="https://en.wikipedia.org/wiki/Separation_of_concerns">separation of concerns</a>. However, since we write the <code>include</code> directive within a class-level <a href="https://newcircle.com/bookshelf/ruby_tutorial/scope">scope</a>, we cannot for example use an instance of <code>Employee</code> itself to determine which module’s behavior we’d like to include. Let me explain with a quick example…</p>
<h3>Different Modules for Different Training</h3>
<p>Assume that instead of one <code>Trainable</code> module, we really have multiple modules that encapsulate distinct forms of employee training like follows:</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="k">module</span> <span class="nn">ManagerTrainable</span> <span class="p">;</span> <span class="k">end</span>
</span><span class='line'><span class="k">module</span> <span class="nn">TechnicianTrainable</span> <span class="p">;</span> <span class="k">end</span>
</span><span class='line'><span class="k">module</span> <span class="nn">ExecutiveTrainable</span> <span class="p">;</span> <span class="k">end</span>
</span><span class='line'><span class="k">module</span> <span class="nn">SalesPersonTrainable</span> <span class="p">;</span> <span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>
<p>(NB: As you can see, I’ve omitted any actual methods from these modules above, but use your imagination! e.g. <code>ExecutiveTrainable#soul_suck!</code>, or maybe <code>SalesPersonTrainable#learn_to_peddle!</code>)</p>
<p>These modules may have also been written to reflect the various real-world types of <code>Employee</code> that are modeled in our Ruby application:</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">manager</span> <span class="o">=</span> <span class="no">Employee</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="ss">type</span><span class="p">:</span> <span class="ss">:manager</span><span class="p">)</span>
</span><span class='line'><span class="n">technician</span> <span class="o">=</span> <span class="no">Employee</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="ss">type</span><span class="p">:</span> <span class="ss">:employee</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>
<p>Each of these <code>employee</code>s also need to be able to go through some form of “training”. So, in order to ensure that all of <code>Employee</code> types can be <code>train!</code>‘ed in the appropriate manner, we can simply <code>include</code> all modules at the class level:</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>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">Employee</span>
</span><span class='line'> <span class="kp">include</span> <span class="no">ManagerTrainable</span>
</span><span class='line'> <span class="kp">include</span> <span class="no">TechnicianTrainable</span>
</span><span class='line'> <span class="kp">include</span> <span class="no">ExecutiveTrainable</span>
</span><span class='line'> <span class="kp">include</span> <span class="no">SalesPersonTrainable</span>
</span><span class='line'>
</span><span class='line'> <span class="o">.</span><span class="n">.</span><span class="o">.</span>
</span><span class='line'>
</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>Unforutnately, this has a nubmer of problems. First, it doesn’t communicate the <a href="https://signalvnoise.com/posts/3531-intention-revealing-methods">developer’s intent</a> very clearly. Someone coming along and reading this code in some months’ time might ask, “What’s going on - why all these variations of <code>Trainable</code>?” Second, we risk <a href="http://ruby-journal.com/how-to-open-slash-override-slash-monkey-patch-a-class-in-ruby/">overwriting methods</a> if the modules share the same/similar interfaces (for example, if each module implements <code>#train!</code> independently, you would have to change the method names, or apply logic to the ordering of our 4 <code>include</code> directives, or… well, you’d have a real problem on your hands to solve at that point).</p>
<h2><code>extend</code> at instance-level scope to simulate <code>include</code> instead.</h2>
<p>Ruby thankfully offers a very elegant, dynamic solution to this problem. Since the logic of which <code>Trainable</code> module should be included is a function of the instance’s <code>employee.type</code>, we can use a lifecycle hook in combination with <code>extend</code> like so:</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>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">Employee</span>
</span><span class='line'> <span class="kp">attr_accessor</span> <span class="ss">:type</span>
</span><span class='line'>
</span><span class='line'> <span class="k">def</span> <span class="nf">initialize</span><span class="p">(</span><span class="n">opts</span> <span class="o">=</span> <span class="p">{})</span>
</span><span class='line'> <span class="c1"># first set the object's attributes as usual</span>
</span><span class='line'> <span class="k">super</span>
</span><span class='line'> <span class="c1"># extend the appropriate module's behavior to the instance's Eigenclass (more on this shortly)</span>
</span><span class='line'> <span class="kp">extend</span><span class="p">(</span><span class="no">Kernel</span><span class="o">.</span><span class="n">const_get</span><span class="p">(</span><span class="s2">"</span><span class="si">#{</span><span class="nb">self</span><span class="o">.</span><span class="n">type</span><span class="si">}</span><span class="s2">Trainable"</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>In <a href="">Rails</a>, you could refactor this by using the provided <a href="">object lifecycle hooks</a>, e.g. <a href=""><code>after_initialize</code></a> like so:</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">class</span> <span class="nc">Employee</span>
</span><span class='line'> <span class="n">after_initialize</span> <span class="ss">:extend_trainable_behavior</span>
</span><span class='line'>
</span><span class='line'> <span class="o">.</span><span class="n">.</span><span class="o">.</span>
</span><span class='line'>
</span><span class='line'> <span class="kp">private</span>
</span><span class='line'>
</span><span class='line'> <span class="k">def</span> <span class="nf">extend_trainable_behavior</span>
</span><span class='line'> <span class="c1"># So, for an `employee` whose type is 'manager', we are effectively including `ManagerTrainable`'s behavior only!</span>
</span><span class='line'> <span class="kp">extend</span><span class="p">(</span><span class="no">Kernel</span><span class="o">.</span><span class="n">const_get</span><span class="p">(</span><span class="s2">"</span><span class="si">#{</span><span class="nb">self</span><span class="o">.</span><span class="n">type</span><span class="si">}</span><span class="s2">Trainable"</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>
<h2>Code Example: add <code>Human#moniker</code> behavior with both class-level <code>include</code> and instance-level <code>extend</code></h2>
<p>In the following code example (where all humans are named <code>"Matt!"</code>, ha), you can see how either <code>include</code> or <code>extend</code> can be used (at different scopes, of course) to tell all <code>Human</code>s how to report their <code>name</code>. Note that console/IO output is indicated by commented out lines with a single <code>#</code> - you’d get the same output by copy-pasting
this code into a live REPL.</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>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
<span class='line-number'>42</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="c1">## Here's our nameable concern. We use both of Ruby's provided hooks - included and extended - to report</span>
</span><span class='line'><span class="c1">## when either event takes place at runtime. </span>
</span><span class='line'>
</span><span class='line'><span class="k">module</span> <span class="nn">Nameable</span>
</span><span class='line'> <span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">included</span><span class="p">(</span><span class="n">base</span><span class="p">)</span>
</span><span class='line'> <span class="nb">puts</span> <span class="s2">"</span><span class="si">#{</span><span class="nb">self</span><span class="si">}</span><span class="s2"> included in </span><span class="si">#{</span><span class="n">base</span><span class="si">}</span><span class="s2">."</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'>
</span><span class='line'> <span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">extended</span><span class="p">(</span><span class="n">base</span><span class="p">)</span>
</span><span class='line'> <span class="nb">puts</span> <span class="s2">"</span><span class="si">#{</span><span class="nb">self</span><span class="si">}</span><span class="s2"> extended in </span><span class="si">#{</span><span class="n">base</span><span class="si">}</span><span class="s2">."</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'>
</span><span class='line'> <span class="k">def</span> <span class="nf">moniker</span>
</span><span class='line'> <span class="s2">"Matt!"</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">HumanWithNormalInclude</span>
</span><span class='line'> <span class="kp">include</span> <span class="no">Nameable</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'><span class="c1"># Nameable included in HumanWithNormalInclude</span>
</span><span class='line'>
</span><span class='line'><span class="k">class</span> <span class="nc">HumanWithInstanceExtend</span>
</span><span class='line'> <span class="k">def</span> <span class="nf">initialize</span>
</span><span class='line'> <span class="k">super</span>
</span><span class='line'> <span class="n">extend_behavior</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'>
</span><span class='line'> <span class="k">def</span> <span class="nf">extend_behavior</span>
</span><span class='line'> <span class="kp">extend</span> <span class="no">Nameable</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">included_human</span> <span class="o">=</span> <span class="no">HumanWithNormalInclude</span><span class="o">.</span><span class="n">new</span>
</span><span class='line'><span class="n">included_human</span><span class="o">.</span><span class="n">moniker</span>
</span><span class='line'><span class="c1"># "Matt!"</span>
</span><span class='line'>
</span><span class='line'><span class="n">extended_human</span> <span class="o">=</span> <span class="no">HumanWithInstanceExtend</span><span class="o">.</span><span class="n">new</span>
</span><span class='line'><span class="c1"># Nameable extended in #<HumanWithInstanceExtend:0x007fa939a0da40>.</span>
</span><span class='line'><span class="c1"># <HumanWithInstanceExtend:0x007fa939a0da40></span>
</span><span class='line'><span class="n">extended_human</span><span class="o">.</span><span class="n">moniker</span>
</span><span class='line'><span class="c1"># "Matt!"</span>
</span></code></pre></td></tr></table></div></figure>
<h2>Black magic?! How does it work?!!</h2>
<p>To understand why <code>extend</code>ing at an instance scope works the way it does, it will help to understand the concept of <a href="http://madebydna.com/all/code/2011/06/24/eigenclasses-demystified.html">“eigenclasses”</a> in Ruby. First, let’s define eigenclass and then write a sort of helper method that tells us any object’s eigenclass on demand.</p>
<blockquote><p><strong>Eigenclass</strong>: a dynamically created anonymous class that Ruby inserts into the method lookup path <em>any time at least one singleton method is added to an object</em>. (I added the last bit in italics… I think it’s right, ha.)</p></blockquote>
<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="k">class</span> <span class="nc">Object</span>
</span><span class='line'> <span class="k">def</span> <span class="nf">eigenclass</span>
</span><span class='line'> <span class="k">class</span> <span class="o"><<</span> <span class="nb">self</span> <span class="c1"># this opens us to the scope of the receiver's eigenclass</span>
</span><span class='line'> <span class="nb">self</span> <span class="c1"># return the eigenclass itself</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>To tie the eigenclass concept back into our previous code example, let’s try to answer the question: <strong>Where does <code>extended_human.moniker</code> actually “live”?</strong></p>
<p>If we query both of our instances’ classes, it’s not immediately clear where our <code>extended_human</code> instance’s <code>#moniker</code> method lives…</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>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="o">[</span><span class="mi">26</span><span class="o">]</span> <span class="n">pry</span><span class="p">(</span><span class="n">main</span><span class="p">)</span><span class="o">></span> <span class="n">included_human</span><span class="o">.</span><span class="n">class</span><span class="o">.</span><span class="n">ancestors</span>
</span><span class='line'><span class="o">[</span>
</span><span class='line'> <span class="o">[</span><span class="mi">0</span><span class="o">]</span> <span class="no">HumanWithNormalInclude</span> <span class="o"><</span> <span class="no">Object</span><span class="p">,</span>
</span><span class='line'> <span class="o">[</span><span class="mi">1</span><span class="o">]</span> <span class="no">Nameable</span><span class="p">,</span>
</span><span class='line'> <span class="o">[</span><span class="mi">2</span><span class="o">]</span> <span class="no">Object</span> <span class="o"><</span> <span class="no">BasicObject</span><span class="p">,</span>
</span><span class='line'> <span class="o">[</span><span class="mi">3</span><span class="o">]</span> <span class="no">PP</span><span class="o">::</span><span class="no">ObjectMixin</span><span class="p">,</span>
</span><span class='line'> <span class="o">[</span><span class="mi">4</span><span class="o">]</span> <span class="no">Kernel</span><span class="p">,</span>
</span><span class='line'> <span class="o">[</span><span class="mi">5</span><span class="o">]</span> <span class="no">BasicObject</span>
</span><span class='line'><span class="o">]</span>
</span><span class='line'><span class="o">[</span><span class="mi">27</span><span class="o">]</span> <span class="n">pry</span><span class="p">(</span><span class="n">main</span><span class="p">)</span><span class="o">></span> <span class="n">extended_human</span><span class="o">.</span><span class="n">class</span><span class="o">.</span><span class="n">ancestors</span>
</span><span class='line'><span class="o">[</span>
</span><span class='line'> <span class="o">[</span><span class="mi">0</span><span class="o">]</span> <span class="no">HumanWithInstanceExtend</span> <span class="o"><</span> <span class="no">Object</span><span class="p">,</span>
</span><span class='line'> <span class="o">[</span><span class="mi">1</span><span class="o">]</span> <span class="no">Object</span> <span class="o"><</span> <span class="no">BasicObject</span><span class="p">,</span>
</span><span class='line'> <span class="o">[</span><span class="mi">2</span><span class="o">]</span> <span class="no">PP</span><span class="o">::</span><span class="no">ObjectMixin</span><span class="p">,</span>
</span><span class='line'> <span class="o">[</span><span class="mi">3</span><span class="o">]</span> <span class="no">Kernel</span><span class="p">,</span>
</span><span class='line'> <span class="o">[</span><span class="mi">4</span><span class="o">]</span> <span class="no">BasicObject</span>
</span><span class='line'><span class="o">]</span>
</span></code></pre></td></tr></table></div></figure>
<p>I typically query an object’s class’s ancestors when I want to see its inheritance hierarchy (where ‘inheritance’ is a combination of mixins and classical inheritance, i.e. <code>class Foo < Bar</code>) as part of finding where a particular method might have come from. This exercise reminds us that in Ruby, the <code>Kernel</code> module (responsible for <code>eval</code>, <code>gets</code>, <code>puts</code>, etc.) is quite high up in the object hierarchy, and as such its methods/behaviors are made available practically everywhere in a Ruby program.</p>
<blockquote><p><strong>Notably absent from <code>extended_human.class.ancestors</code>, however, is any reference to our <code>Nameable</code> mixin.</strong></p></blockquote>
<p>So - where is <code>extended_human.moniker</code> coming from then? Let’s instead look through the instances’ eigenclasses’ ancestors:</p>
<h2>Ruby’s method lookup path flows through eigenclasses first!</h2>
<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>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="o">[</span><span class="mi">29</span><span class="o">]</span> <span class="n">pry</span><span class="p">(</span><span class="n">main</span><span class="p">)</span><span class="o">></span> <span class="n">included_human</span><span class="o">.</span><span class="n">eigenclass</span><span class="o">.</span><span class="n">ancestors</span>
</span><span class='line'><span class="o">[</span>
</span><span class='line'> <span class="o">[</span><span class="mi">0</span><span class="o">]</span> <span class="no">HumanWithNormalInclude</span> <span class="o"><</span> <span class="no">Object</span><span class="p">,</span>
</span><span class='line'> <span class="o">[</span><span class="mi">1</span><span class="o">]</span> <span class="no">Nameable</span><span class="p">,</span>
</span><span class='line'> <span class="o">[</span><span class="mi">2</span><span class="o">]</span> <span class="no">Object</span> <span class="o"><</span> <span class="no">BasicObject</span><span class="p">,</span>
</span><span class='line'> <span class="o">[</span><span class="mi">3</span><span class="o">]</span> <span class="no">PP</span><span class="o">::</span><span class="no">ObjectMixin</span><span class="p">,</span>
</span><span class='line'> <span class="o">[</span><span class="mi">4</span><span class="o">]</span> <span class="no">Kernel</span><span class="p">,</span>
</span><span class='line'> <span class="o">[</span><span class="mi">5</span><span class="o">]</span> <span class="no">BasicObject</span>
</span><span class='line'><span class="o">]</span>
</span><span class='line'><span class="o">[</span><span class="mi">30</span><span class="o">]</span> <span class="n">pry</span><span class="p">(</span><span class="n">main</span><span class="p">)</span><span class="o">></span> <span class="n">extended_human</span><span class="o">.</span><span class="n">eigenclass</span><span class="o">.</span><span class="n">ancestors</span>
</span><span class='line'><span class="o">[</span>
</span><span class='line'> <span class="o">[</span><span class="mi">0</span><span class="o">]</span> <span class="no">Nameable</span><span class="p">,</span> <span class="c1"># Here it is!</span>
</span><span class='line'> <span class="o">[</span><span class="mi">1</span><span class="o">]</span> <span class="no">HumanWithInstanceExtend</span> <span class="o"><</span> <span class="no">Object</span><span class="p">,</span>
</span><span class='line'> <span class="o">[</span><span class="mi">2</span><span class="o">]</span> <span class="no">Object</span> <span class="o"><</span> <span class="no">BasicObject</span><span class="p">,</span>
</span><span class='line'> <span class="o">[</span><span class="mi">3</span><span class="o">]</span> <span class="no">PP</span><span class="o">::</span><span class="no">ObjectMixin</span><span class="p">,</span>
</span><span class='line'> <span class="o">[</span><span class="mi">4</span><span class="o">]</span> <span class="no">Kernel</span><span class="p">,</span>
</span><span class='line'> <span class="o">[</span><span class="mi">5</span><span class="o">]</span> <span class="no">BasicObject</span>
</span><span class='line'><span class="o">]</span>
</span></code></pre></td></tr></table></div></figure>
<p>Aha - found it! Note that for our <code>extended_human</code>, <code>Nameable</code> is the very first ancestor in its <a href="https://blog.jcoglan.com/2013/05/08/how-ruby-method-dispatch-works/">method-lookup hierarchy</a>. This is because we <code>extend</code>‘ed <code>Nameable</code> directly into <code>extended_human</code>’s eigenclass, as opposed to <code>include</code>‘ing it in its containing class definition. And once again, to display this point differently:</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="o">[</span><span class="mi">31</span><span class="o">]</span> <span class="n">pry</span><span class="p">(</span><span class="n">main</span><span class="p">)</span><span class="o">></span> <span class="n">extended_human</span><span class="o">.</span><span class="n">eigenclass</span><span class="o">.</span><span class="n">ancestors</span> <span class="o">-</span> <span class="n">extended_human</span><span class="o">.</span><span class="n">class</span><span class="o">.</span><span class="n">ancestors</span>
</span><span class='line'><span class="o">[</span>
</span><span class='line'> <span class="o">[</span><span class="mi">0</span><span class="o">]</span> <span class="no">Nameable</span>
</span><span class='line'><span class="o">]</span>
</span></code></pre></td></tr></table></div></figure>
<p>By making use of an anonymous class under the hood at runtime, Ruby gives us the ability to <strong>dynamically mixin behaviors at all levels of our programs - namely in our example, at both the class and instance level!</strong> Wo-man, I <3 Ruby :)</p>
</div>
</article>
<article>
<header>
<h1 class="entry-title"><a href="/2015/09/27/how-fixed-gear-bicycling-taught-me-about-dependency-injection/">How Fixed Gear Bicycling Taught Me About Dependency Injection</a></h1>
<p class="meta">
<time class='entry-date' datetime='2015-09-27T12:17:14-04:00'><span class='date'><span class='date-month'>Sep</span> <span class='date-day'>27</span><span class='date-suffix'>th</span>, <span class='date-year'>2015</span></span> <span class='time'>12:17 pm</span></time>
| <a href="/2015/09/27/how-fixed-gear-bicycling-taught-me-about-dependency-injection/#disqus_thread"
data-disqus-identifier="http://mattcampbell.nyc/2015/09/27/how-fixed-gear-bicycling-taught-me-about-dependency-injection/">Comments</a>
</p>
</header>
<div class="entry-content"><p>I became inspired to write this blog post after watching a great <a href="https://www.twitter.com/sandimetz">@sandimetz</a> <a href="https://www.youtube.com/watch?v=OMPfEXIlTVE">talk which she gave at RailsConf 2015</a>. Her subject was primarily the <a href="https://en.wikipedia.org/wiki/Null_Object_pattern">Null Object Pattern</a>, but she extends the fundamental principle - that objects, not conditionals nor excruciatingly tailored classes, should be used to encapsulate your system’s behaviors - to the practical aspects of injecting dependencies required by your domain’s fundamental objects. Let’s take a look at what I mean by using a real-world example: my recently acquired (and already banged up - I’ve had two flat tires already!) <a href="https://en.wikipedia.org/wiki/Fixed-gear_bicycle">fixed gear (a.k.a “fixie”) bike</a>. I want you to start asking yourself: if you were given the task of modeling in Ruby a fixed-gear bicycle alongside a “normal” or freewheel bicycle, what tools would you reach for - inheritance, composition, or otherwise?</p>
<h2>Modeling our <code>Bicycle</code></h2>