-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoignant.yml
9794 lines (6007 loc) · 385 KB
/
poignant.yml
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
--- !whytheluckystiff.net,2003/^book
title: Why's (Poignant) Guide to Ruby
author: why the lucky stiff
terms: >
bq(#terms). The text of this book and the encompassing web matter are released under the wholy sympathetic
"Attribution-ShareAlike":http://creativecommons.org/licenses/by-sa/2.0/
license, part of the brilliant family of licenses produced
by the "Creative Commons":http://creativecommons.org/.
deed: |
Attribution-ShareAlike 2.0
You are free:
* to copy, distribute, display, and perform the work
* to make derivative works
* to make commercial use of the work
Under the following conditions:
*Attribution*. You must give the original author credit.
*Share Alike*. If you alter, transform, or build upon this work, you may distribute the
resulting work only under a license identical to this one.
* For any reuse or distribution, you must make clear to others the license terms of this work.
* Any of these conditions can be waived if you get permission from the author.
Your fair use and other rights are in no way affected by the above.
This is a human-readable summary of the Legal Code (the full license).
image: !^img poignant.guide.png
teaser:
- Read This Paragraph: >
At my local Barnes and Noble, there is a huge wall of Java books just waiting
to tip over and crush me one day. And one day it will. At the rate things are going,
one day that bookcase will be tall enough to crush us all. It might even loop the world
several times, crushing previous editions of the same Java books over and over again.
- And This Paragraph Too: >
This is just a small Ruby book. It won't crush you. It's light as a
feather (because I haven't finished it yet-- hehe). And there's a reason this book
will stay light: because Ruby is simple to learn.
- But Don't Read This One!: >
_Why's (Poignant) Guide to Ruby_ is released under the
"Attribution-ShareAlike":http://creativecommons.org/licenses/by-sa/2.0/
License. So, yes, please distribute it and print it and read it
leisurely in your housecoat. In fact, there will be a contest at the
end of the book for Best Housecoat. It's a coveted award and you should
feel honored to even read about it! (Especially if you are reading about
it in your soon-to-be-prize-winning housecoat.)
- Now Back to Your Regularly Scheduled Paragraph: >
I'll try not to feel utterly rejected if this book doesn't capture
your fancy. I might experience a few long days of emptiness, accompanied
with profuse weeping, but other than that I'll be fine. It's at least
comforting to know that the following fine texts cover my topic and will
doubtless fill you with Ruby lust:
* "Learning to Program":http://pine.fm/LearnToProgram/
** A very basic, ground-level tutorial for the beginner to Ruby. By Chris Pine.
* "Programming Ruby":http://www.whytheluckystiff.net/ruby/pickaxe/
** The original tome and complete reference for Ruby. By Dave Thomas and Andy Hunt
(also known as _The Pickaxe_)
* "A Little Ruby, A Lot of Objects":http://www.visibleworkings.com/little-ruby/
** Lovely little in-progress work to teach Ruby purely with simple code examples.
By Brian Marick.
* "Ruby User's Guide":http://www.ruby-doc.org/docs/UsersGuide/rg/
** A guide to learning Ruby (with code examples). By Matz, the creator of
Ruby. Translated into English.
* "Ruby Book List":http://www.rubygarden.org/ruby?RubyBookList
** A current list of printed material covering Ruby.
* "Ruby-Lang.org":http://www.ruby-lang.org/
** The official home page for the Ruby language.
Now, if you can't seem to find the contents link on the left-hand side of the page,
then here's a link to the "first page":chapter-1.html
of the (Poignant) Guide. If you want to venture onto my other projects, go
"whytheluckystiff.net":http://www.whytheluckystiff.net.
Welcome to the pirate radio of technical manuals.
chapters:
- About this Book:
- >
!i/about.the.poignant.guide-1.gif(What a fantastic voyage!)!
!i/about.the.poignant.guide-2.jpg(Small infants can do it.)!
!i/about.the.poignant.guide-3.gif(The thoughtful reader.)!
!i/about.the.poignant.guide-4.gif(Meep.)!
- Kon'nichi wa, Ruby:
- Opening This Book: >
Pretend that you've opened this book (although you probably _have_ opened this book),
just to find a huge onion right in the middle crease of the book. (The manufacturer of the book
has included the onion at my request.)
So you're like, "Wow, this book comes with an onion!" (Even if you don't particularly like onions,
I'm sure you can appreciate the logistics of shipping any sort of produce discreetly inside of an alleged
programming manual.)
Then you ask yourself, "Wait a minute. I thought this was a book on Ruby, the incredible new
programming language from Japan. And although I can appreciate the logistics of shipping any sort of
produce discreetly inside of an alleged programming manual: Why an onion? What am I supposed to do
with it?"
No. Please don't puzzle over it. You don't need to do anything with the onion. Set the onion aside
and let _it_ do something with _you_.
I'll be straight with you. I want you to cry. To weep. To whimper sweetly. This book is a *poignant*
guide to Ruby. That means code so beautiful that tears are shed. That means gallant tales and somber truths
that have you waking up the next morning in the arms of this book. Hugging it tightly to you all the day long.
If necessary, fashion a makeshift hip holster for _Why's (Poignant) Guide to Ruby_, so you can always have this
book's tender companionship.
You really must sob once. Or at least sniffle. And if not, then the onion will make it all happen for you.
- !^sidebar
What I'm Going to Do With the Massive Proceeds from this Book: >
Anyone who's written a book can tell you how easily an author is distracted by visions of grandeur.
In my experience, I stop twice for each paragraph, and four times for each panel of a comic, just to
envision the wealth and prosperity that this book will procure for my lifestyle. I fear that the writing
of this book will halt altogether to make way for the armada of SUVs and luxury towne cars that are
blazing away in my head.
Rather than stop my production of the (Poignant) Guide, I've reserved this space as a safety zone
for pouring my empty and vain wishes.
Today I was at this Italian restaraunt, Granado's, and I was paying my bill. Happened to notice (under
glass) a bottle of balsamic vinegar going for $150. Fairly small. I could conceal it in my
palm. Aged twenty-two years.
I've spent a lot of time thinking about that bottle. It is often an accessory in some of these
obsessive fantasies. In one fantasy, I walk into the restaraunt, toss a stack of greenery on the counter
and earnestly say to the cashier, "Quick! I have an important salad to make!"
In another, related fantasy, I am throwing away lettuce. Such roughage isn't befitting of my new
vinegar. No, I will have come to a point where the fame and the aristocracy will have corrupted me
to my core. My new lettuce will be cash. Cold, hard cash, Mrs. Price.
Soon, I will be expending hundreds for a block of myzithra cheese.
My imaginations have now gone beyond posessions, though. Certainly, I have thought through my
acquisition of grecian urns, motorcades, airlines, pyramids, dinosaur bones. Occassionally I'll
see wind-tossed cities on the news and I'll jot down on my shopping list: _Hurricane_.
But, now I'm seeing a larger goal. Simply put: what if I amassed such a fortune that the mints
couldn't print enough to keep up with my demand? So, everyone else would be forced to use Monopoly money as
actual currency. And you would have to win in Monopoly to keep food on the table. These would be
some seriously tense games. I mean you go to mortgage St. James Place and your kids start crying.
In addition, I think you'll begin to see the end of those who choose to use the Free Parking square
as "the underground coffers":http://groups.yahoo.com/group/monopoly/message/37 for city funds.
You've got to hand it to fun money, though. Fake money rules. You can get your hands on it
so quickly. For a moment, it seems like you're crazy rich. When I was a kid, I got with some
of the neighborhood kids and we built this little Tijuana on our street. We made our own pesos and
wore sombreros and everything!
One kid was selling hot tamales for two pesos each. _Two pesos!_ Did this kid know that the money was fake?
Was he out of his mind? Who invited this kid? Didn't he know this wasn't really Tijuana? Maybe he was really from Tijuana!
Maybe these were _real_ pesos! Let's go make more _real_ pesos!
I think we even had a tavern where you could get totally hammered off Kool-Aid. There's nothing like
a bunch of kids stumbling around, mumbling incoherently with punchy red clown lips.
- The Dog Story: >
So try this first bit of poignancy on for size:
One day I was walking down one of those busy roads covered with car dealerships (this was shortly after my wedding
was called off) and I found an orphaned dog on the road. A wooly, black dog with greenish red eyes. I was kind
of feeling like an orphan myself, so I took a couple balloons that were tied to a pole at the dealership
and I relocated them to the dog's collar. Then, I decided he would be my dog. I named him Bigelow.
We set off to get some Milkbones for Bigelow and, afterwards, head over to my place, where we could sit in
recliners and listen to Gorky's Zygotic Mynci. Oh, and we'd also need to stop by a thrift store and get
Bigelow his own recliner.
But Bigelow hadn't accepted me as his master. So five minutes later, the stupid dog took a different
crosswalk than I did and I never caught up. So whereas he had previously only been lost once, he was now lost
twice. I slowed my pace towards the life of Milkbones and an extra recliner. I had a dog for five minutes.
Stupid Benedict Arnold of a dog. I sat on a city bench and threw pinecones at a statue of three sheep
crossing a bridge. After that, I wept for hours. The tears just came. Now there's a little something
poignant to get you started.
I wonder where he went with all those balloons. That crazy dog must have looked like a party with
legs.
It wasn't much later that I pulled my own Bigelow. I printed out a bunch of pages on Ruby. Articles
found around the Web. I scanned through them on a train ride home one day. I flipped through them for
five minutes and then gave up. Not impressed.
I sat, staring out the window at the world, a life-sized blender mixing graffiti and iron
smelts before my eyes. _This world's too big for such a a little language_, I thought.
_Poor little thing doesn't stand a chance. Doesn't have legs to stand on. Doesn't have
arms to swim._
And yet, there I was. One little man on a flimsy little train (and I even still
had a baby tooth to lose at the time) out of billions of people living on a floating
blue rock. How can I knock Ruby? Who's to say that I'm not going to happen to
choke on my cell phone and die later that evening. Why's dead, Ruby lives on.
The gravestone:
bq. What's in his trachea?
Oh, look, a Nokia!
Just my luck. Finally get to have a good, long sleep underground, only to be constantly disturbed by
_Pachelbel's Canon_ going off in my stomach.
- The Red Sun Rises: >
So, now you're wondering why I changed my mind about Ruby. The quick answer is:
we clicked.
Like when you meet Somebody in college and they look like somebody who
used to hit you in the face with paintbrushes when you were a kid. And so, impulsively,
you conclude that this new Somebody is likely a non-friend. You wince at their
hair. You hang up phones loudly during crucial moments in their anecdotes. You use
your pogo stick right there where they are trying to walk!
Six months later, somehow, you and Somebody are sitting at a fountain having a perfectly good
chat. Their face doesn't look so much like that childhood nemesis. You've met
the Good Twin. You clicked.
So whereas I should probably be pounding your teeth in with hype about Ruby and the
tightly-knit cadre of pertinent ancronyms that accompany it everywhere
(whetting the collective whistles of
your bosses and their bosses' bosses), instead I will just let you coast. I'll let
you freefall through some code, interjecting occassionally with my own heartfelt
experiences. It'll be quite easy, quite natural.
I should offer you some sort of motivation, though. So, Smotchkkiss, I'm going to give my three
best reasons to learn Ruby and be done with it.
# *Brain health.* <p>Vitamin R. Goes straight to the head. Ruby will
teach you to _express_ your ideas through a computer. You will be writing
stories for a machine.</p> <p>Creative skills, people. Deduction. Reason. Nodding intelligently.
The language will become a tool for you to better connect your mind to the world.
I've noticed that many experienced users of Ruby seem to be clear thinkers and
objective. (In contrast to: heavily biased and coarse.)</p>
# *One man on one island.* <p>Ruby was born in Japan. Which is freaky. Japan is not known
for its software. And since programming languages are largely written in English, who
would suspect a language to come from Japan?</p> <p>And yet, here we have Ruby. Against the odds,
Yukihiro Matsumoto created Ruby on February 24, 1993. For the past ten years, he has steadily
brought Ruby to a global audience. It's triumphant and noble and all that. Support diversity.
Help us tilt the earth just a bit.</p>
# *Free.* <p>Using Ruby costs nothing. The code to Ruby itself is open for all of the world
to inhale/exhale. Heck, this book is free. It's all part of a great, big giveaway that should
have some big hitch to it.</p> <p>You'd think we'd make you buy vacuums or timeshare or fake Monets.
You'd think there'd be a 90 minute presentation where the owner of the company comes out at
the end and knuckles you into sealing the deal.</p> <p>Nope, free.</p>
With that, it's time for the book to begin. You can now get out your highlighter and start dragging
it along each captivating word from this sentence on. I think I have enough hairspray and
funny money on my person to keep me sustained until the final page.
- How Books Start: >
Now, if you ever have read a book, you know that no book can properly start without an exorbitant
amount of synergy. Yes, synergy. Maybe you didn't know this. Synergy means that you and I are
supposed to cooperate to make this a great reading experience.
We start off the book by getting
along well in the Introduction. This togetherness, this *synergy*, propels us through the book,
with me guiding you on your way. You give me a reassuring nod or snicker to indicate your progress.
I'm Peter Pan holding your hand. Come on, Wendy! Second star to the right and on till morning.
One problem here. I don't get along well with people. I don't hold hands very well.
Any of my staff will tell you. At the Opening Ceremonies of This Book (a catered event
with stadium seating), I discovered that the cucumber sandwiches weren't served in tea towels.
As a result, the butter hadn't set with the cucumbers right... Anyways, I made a big scene and
set fire to some of the advertising trucks outside. I smashed this spotlight to pieces and
so on. I had this loud maniacal laughing thing going on deep into that night. It was a real mess.
But, since I don't get along well with people, I hadn't invited anyone but myself to the Opening
Ceremonies of This Book. So it wasn't really that embarassing. I kept it under wraps and no
one found out about the whole ordeal.
So you've got to know that *synergy* doesn't actually mean *synergy* in this book. I can't do normal *synergy*.
No, in this book, *synergy* means *cartoon foxes*. What I'm saying is: this book will be
starting off with an exorbitant amount of *cartoon foxes*.
And I will be counting on you to turn them into *synergy*.
- A Quick (and Hopefully Painless) Ride Through Ruby (with Cartoon Foxes):
- >
!i/the.foxes-1.png(The foxes show up.)!
Yeah, these are the two. My asthma's kickin in so I've got to go take a puff of
medicated air just now. Be with you in a moment.
!i/the.foxes-2.png(Foxes in boxes.)!
I'm told that this chapter is best accompanied by a rag. Something you can mop your
face with as the sweat pours off your face.
Indeed, we'll be racing through the whole language. Like striking every match in a box
as quickly as can be done.
- Language and I MEAN Language: >
!i/the.foxes-3.png(Our friends, those two helpless foxies, finally realize
the gravity of their predicament.)!
My conscience won't let me call Ruby a _computer_ language. That would imply that the
language works primarily on the computer's terms. That the language is designed to accomodate
the computer, first and foremost. That therefore, we, the coders, are foreigners, seeking citizenship in the
computer's locale. It's the computer's language and we are translators for the world.
But what do you call the language when your brain begins to think in that language? When you
start to use the language's own words and colloquialisms to express yourself. Say, the computer
can't do that. How can it be the computer's language? It is ours, we speak it natively!
We can no longer truthfully call it a _computer_ language. It is _coderspeak_. It is the language
of our thoughts.
*Read the following aloud to yourself.*
bq. @5.times { print "Odelay!" }@
In English sentences, punctuation (such as periods, exclamations, parentheses) are silent. Punctuation
adds meaning to words, helps give cues as to what the author intended by a sentence. So let's read the
above as: _Five times print "Odelay!"._
Which is exactly what this small Ruby program does. Beck's
"mutated Spanish":http://www.whiskeyclone.net/ghost/L/lordonlyknows.html exclamation will print
five times on the computer screen.
*Read the following aloud to yourself.*
bq. @exit unless "restaurant".include? "aura"@
Here we're doing a basic reality check. Our program will *exit* (the program will end) *unless*
the word *restaurant* contains (or *includes*) the word *aura*. Again, in English: _Exit unless the word
restaurant includes the word aura._
Ever seen a programming language use question marks so effectively? Ruby uses some punctuation,
such as exclamations and question marks, to enhance readability of the code. We're asking a question
in the above code, so why not make that apparent?
*Read the following aloud to yourself.*
bq. @['toast', 'cheese', 'wine'].each { |food| print food.capitalize }@
While this bit of code is less readable and sentence-like than the previous examples, I'd still encourage
you to read it aloud. While Ruby may sometimes read like English, it sometimes reads as a shorter
English. Fully translated into English, you might read the above as: _With the words 'toast', 'cheese',
and 'wine': take each food and print it capitalized._
The computer then courteously responds: @Toast@, @Cheese@ and @Wine@.
At this point, you're probably wondering how these words actually fit together. Smotchkkiss is wondering
what the dots and brackets mean. I'm going to discuss the various _parts of speech_ next.
All you need to know thus far is that Ruby is basically built from sentences. They aren't exactly English
sentences. They are short collections of words and punctuation which encompass a single thought. These
sentences can form books. They can form pages. They can form entire novels, when strung together.
Novels that can be read by humans, but also by computers.
- !^sidebar
Concerning Commercial Uses of the (Poignant) Guide: >
This book is released under a Creative Commons license which allows unlimited commercial
use of this text. Basically, this means you can sell all these bootleg copies of my book
and keep the revenues for yourself. I trust my readers (and the world around them) to rip me off.
To put out some crappy Xerox edition with that time-tested clipart of praying hands on the cover.
Guys, the lawsuits just ain't worth the headache. So I'm just going to straight up endorse
authorized piracy, folks. Anybody who wants to read the book should be able to read it.
Anybody who wants to market the book or come up with special editions, I'm flattered.
Why would I want the $$$? IGNORE ALL OTHER SIDEBARS: I've lost the will to be a rich slob.
Sounds inhuman, but I like my little black-and-white television. Also my hanging plastic flower lamp.
I don't want to be a career writer. Cash isn't going inspire me. Pointless.
So, if money means nothing to the lucky stiff, why rip me off when you could co-opt shady
business practices to literally crush my psyche and leave me wheezing in some sooty iron lung?
Oh, and the irony of using my own works against me! Die, Poignant Boy!
To give you an idea of what I mean, here are a few underhanded concepts that could seriously
kill my willpower and force me to reconsider things like existence.
*IDEA ONE: BIG TOBACCO*
Buy a cigarette company. Use my cartoon foxes to fuel an aggressive ad campaign.
Here's a billboard for starters:
!i/the.foxes-0a.png(Addiction is like Pokemon!)!
Make it obvious that you're targeting children and the asthmatic. Then, once you've got
everyone going, have the *truth* people do an expose on me and my farm of inky foxes.
bq. *Sensible Hipster Standing on Curb in Urban Wilderness*: He calls himself the lucky stiff.
bq. (Pulls aside curtain to reveal grey corpse on a gurney.)
bq. *Hipster*: Some stiffs ain't so lucky.
bq. (Erratic zoom in. Superimposed cartoon foxes for subliminal Willy Wonka mind trip.)
Yo. Why you gotta dis Big Smokies like dat, Holmes?
*IDEA TWO: HEY, FIRING SQUAD*
Like I said, start selling copies of my book, but corrupt the text. These altered copies
would contain numerous blatant (and libelous) references to government agencies, such as the
U.S. Marshals and the Pentagon. You could make me look like a complete traitor. Like I have
all these plans to, you know, kill certain less desirable members of the U.S. Marshals or
the Pentagon.
Not that there are any less desirable members of the U.S. Marshals or the Pentagon. Yeah,
I didn't mean it like that.
Oh, crap.
Oh, crap. Oh, crap. Oh, crap.
Turn off the lights. Get down.
*IDEA THREE: BILLBOARDS, PART II*
How about making fun of asthmatics directly?
!i/the.foxes-0b.png(Call it a puffer! ROFL!)!
*IDEA FOUR: ALEC BALDWIN*
Adapt the book into a movie. And since, you know, I'm a character in this book,
you could get someone like Alec Baldwin to play me. Someone who's at a real lowpoint
in his career.
You could make it seem like I did tons of drugs. Like I was insane to work with.
Like I kept firing people and locking them in the scooter room and making them
wear outfits made of bread. Yeah, like I could actually be _baking_ people into
the outfits.
You could have this huge mold that I strap people into. Then, I pour all the dough
on them and actually bake them until the bread has risen and they've almost died.
And when the television crews come and I'm on Good Morning America, they'll ask,
"So, how many people have you employed in the production of your book?" And I'd
respond, "A baker's dozen!" and erupt into that loud maniacal laughing that would
force audience members to cup their hands over their ears.
Of course, in the throes of my insanity, I would declare war on the world. The
bread people would put up quite a fight. Until the U.S. Marshals (or the Pentagon)
engineer a giant robotic monkey brain (played by Burt Lancaster) to come after me.
Here's where you'll make me look completely lame. Not only will I sacrifice all of
the bread people (the Starchtroopers) to save myself, not only will I surrender to
the great monkey brain like a coward, but when I narrowly escape, I'll yell at the
audience. Screaming insistently that it's _MY_ movie and no one should see it any
more, I'll rip the screen in half and the film projector will spin with its reel
flapping in defeat. And that will be the end of the movie. People will be _so_
pissed.
Now, I've got to thinking. See, and actually, Alec Baldwin did a decent voiceover
in _The Royal Tenenbaums_. His career might be okay. You might not want to use him.
He might not do it.
Tell ya what. I'll play the part. I've made a career out of lowpoints.
- The Parts of Speech: >
Just like the white stripe down a skunk's back and the winding, white train of a bride, many of
Ruby's parts of speech have visual cues to help you identify them. Punctuation and capitalization
will help your brain to see bits of code and feel intense recognition.
Your mind will frequently yell _Hey, I know that guy!_ You'll also be able to name-drop in
conversations with other Rubyists.
Try to focus on the look of each of these parts of speech. The rest of the book will
detail the specifics. I give short descriptions for each part of speech, but you don't
have to understand the explanation. By the end of this chapter, you should be
able to recognize every part of a Ruby program.
h3. Variables
Any plain, lowercase word is a variable in ruby. Variables may consist of
letters, digits and underscores.
bq. @x@, @y@, @banana2@ or @phone_a_quail@ are examples.
Variables are like nicknames. Remember when everyone used to call you Stinky Pete?
People would say, "Get over here, Stinky Pete!" And everyone miraculously knew that
Stinky Pete was you.
With variables, you give a nickname to something you use frequently. For instance, let's
say you run an orphanage. It's a mean orphanage. And whenever Daddy Warbucks comes to buy
more kids, we insist that he pay us *one-hundred twenty-one dollars and eight cents* for the
kid's teddy bear, which the kid has become attached to over in the darker moments of living in
such nightmarish custody.
bq. @teddy_bear_fee = 121.08@
Later, when you ring him up at the cash register (a really souped-up cash register which runs
Ruby!), you'll need to add together all his charges into a *total*.
bq. @total = orphan_fee + teddy_bear_fee + gratuity@
Those variable nicknames sure help. And in the seedy underground of child sales, any help is
appreciated I'm sure.
!i/the.foxes-4a.png(They mock my examples.)!
h3. Numbers
The most basic type of number is an _integer_, a *series of digits* which can start
with a *plus or minus sign*.
bq. @1@, @23@, and @-10000@ are examples.
Commas are not allowed in numbers, but underscores are. So if you feel the need to
mark your thousands so the numbers are more readable, use an underscore.
bq. @population = 12_000_000_000@
Decimal numbers are called _floats_ in Ruby. Floats consist of numbers with *a decimal
place* or *scientific notation*.
bq. @3.14@, @-808.08@ and @12.043e-04@ are examples.
h3. Strings
Strings are any sort of characters (letters, digits, punctuation) surrounded by
quotes. Both single and double *quotes* are used to create strings.
bq. @"sealab"@, @'2021'@, or @"These cartoons are hilarious!"@
are examples.
When you enclose characters in quotes, they are stored together as a single string.
Think of a reporter who is jotting down the mouthnoises of a rambling celebrity. "I'm a lot
wiser," says Avril Lavigne. "Now I know what the business is like -- what you have to do and
how to work it."
<pre>
avril_quote = "I'm a lot wiser. Now I know
what the business is like -- what you have
to do and how to work it."
</pre>
So, just as we stored a number in the *teddy_bear_fee* variable, now we're storing a collection
of characters (a string) in the *avril_quote* variable. The reporter sends this quote
to the printers, who just happen to use Ruby to operate their printing press.
<pre>
<setup>
oprah_quote = "O"
avril_quote = "A"
ashlee_simpson_debacle = "D"
</setup>
print oprah_quote
print avril_quote
print ashlee_simpson_debacle
</pre>
!i/the.foxes-4b.png(They desire to be in my examples.)!
h3. Symbols
Symbols are words that look just like variables. Again, they may contain letters,
digits, or underscores. But they *start with a colon*.
bq. @:a@, @:b@, or @:ponce_de_leon@ are examples.
Symbols are lightweight strings. Usually, symbols are used in situations where you need a string
but you won't be printing it to the screen.
You could say a symbol is a bit easier on the computer. It's like an antacid. The colon indicates
the bubbles trickling up from your computer's stomach as it digests the symbol. Ah. Sweet, sweet
relief.
!i/the.foxes-4c.png(Chunky bacon!!)!
h3. Constants
Constants are words like variables, but constants are *capitalized*. If variables are the
nouns of Ruby, then think of constants as the proper nouns.
bq. @Time@, @Array@ or @Bunny_Lake_is_Missing@ are examples.
In English, proper nouns are capitalized. The Empire State Building. You can't just move The Empire
State Building. You can't just decide that the Empire State Building is something else.
Proper nouns are like that. They refer to something very specific and usually don't
change over time.
In the same way, constants can't be changed after they are set.
bq. @EmpireStateBuilding = "350 5th Avenue, NYC, NY"@
If we try to change the constant, Ruby will complain to us. Such things are frowned upon.
!i/the.foxes-4d.png(Come on, chunky bacon.)!
h3. Methods
If variables and constants are the nouns, then methods are the verbs.
Methods are usually attached to the end of variables and constants by a *dot*. You've already
seen methods at work.
bq. @front_door.open@
In the above, *open* is the method. It is the action, the verb. In some cases, you'll see actions chained together.
bq. @front_door.open.close@
We've instructed the computer to open the front door and then immediately close it.
bq. @front_door.is_open?@
The above is an action as well. We're instructing the computer to test the door to see if it's open.
The method could be called @Door.test_to_see_if_its_open@, but the @is_open?@ name is succinct and
just as correct. Both exclamation marks and question marks may be used in method names.
h3. Method arguments
A method may require more information in order to perform its action. If we want the computer
to paint the door, we should provide a color as well.
Method arguments are attached to the end of a method. The arguments are usually surrounded by
*parentheses* and separated by *commas*.
bq. @front_door.paint( 3, :red )@
The above paints the front door 3 coats of red.
Think of it as an inner tube the method is pulling along, containing its extra instructions.
The parentheses form the wet, round edges of the inner tube. The commas are the feet of each argument,
sticking over the edge. The last argument has its feet tucked under so they don't show.
Like a boat pulling many inner tubes, methods with arguments can be chained.
bq. @front_door.paint( 3, :red ).dry( 30 ).close()@
The above paints the front door 3 coats of red, dries for 30 minutes, and closes the door. Even though
the last method has no arguments, you can still put parentheses if you like. There is
no use dragging an empty inner tube, so the parentheses are normally dropped.
Some methods (such as @print@) are kernel methods. These methods are used throughout Ruby. Since they
are so common, you won't use the dot.
bq. @print "See, no dot."@
h3. Class methods
Like the methods described above (also called _instance_ methods), class methods
are usually attached after variables and constants. Rather than a dot, a *double colon* is used.
bq. @Door::new( :oak )@
As seen above, the @new@ class method is most often used to create things. In the above example, we're asking
Ruby to make a new oak door for us. Of course, Ruby has to have an understanding of how to make a
door--as well as a wealth of timber, lumberjacks, and those long, wiggily, two-man saws.
!i/the.foxes-4e.png(Plenty of chunky bacon to go around.)!
h3. Global variables
Variables which begin with a *dollar sign* are global.
bq. @$x@, @$1@, @$chunky@ and @$CHunKY_bACOn@ are examples.
Most variables are rather temporary in nature. Some parts of your program are like little houses.
You walk in and they have their own variables. In one house, you may have a @dad@ that represents
Archie, a travelling salesman and skeleton collector. In another house, @dad@ could represent
Peter, a lion tamer with a great love for flannel. Each house has its own meaning for @dad@.
With global variables, you can be guaranteed that the variable is the same in every little house.
The dollar sign is very appropriate. Every American home respects the value of the dollar. We're
crazy for the stuff. Try knocking on any door in America and hand them cash. I can guarantee
you won't get the same reaction if you knock on a door and offer Peter, a lion tamer with a great
love for flannel.
Global variables can be used anywhere in your program. They never go out of sight.
h3. Instance variables
Variables which begin with an *at* symbol are instance variables.
bq. @@x@, @@y@, and @@only_the_chunkiest_cut_of_bacon_I_have_ever_seen@ are examples.
These variables are often used to define the attributes of something. For example, you might
provide Ruby with the width of the @front_door@ by setting the @@width@ variable inside that
@front_door@. Instance variables are used to define characteristics of a single object in
Ruby.
Think of the *at* symbol as meaning *attribute*.
h3. Class variables
Variables which begin with *double at* symbols are class variables.
bq. @@@x@, @@@y@, and @@@i_will_take_your_chunky_bacon_and_raise_you_two@ are examples.
Class variables, too, are used to define attributes. But rather than defining an attribute
for a single object in Ruby, class variables give an attribute to many related objects in
Ruby. If instance variables set attributes for a single @front_door@, then class variables
set attributes for everything that is a @Door@.
Think of the *double at* prefix as meaning *attribute all*. Additionally, you can think of a swarm
of *AT-ATs* from _Star Wars_, which are all commanded by Ruby. You change a class variable
and not just one changes, they all change.
!i/the.foxes-4f.png(Woohoo! Chunky bacon accomplished!)!
h3. Blocks
Any code surrounded by *curly braces* is a block.
bq. @2.times { print "Yes, I've used chunky bacon in my examples, but never again!" }@ is an example.
With blocks, you can group a set of instructions together so that they can be passed
around your program.
The curly braces give the appearance of crab pincers that have snatched the code
and are holding it together. When you see these two pincers, remember that the
code inside has been pressed into a single unit.
It's like one of those little Hello Kitty boxes they sell at
the mall that's stuffed with tiny pencils and microscopic paper, all crammed into
a glittery transparent case that can be concealed in your palm for covert stationary
operations. Except that blocks don't require so much squinting.
The curly braces can also be traded for the words *do* and *end*, which is nice if your
block is longer than one line.
<pre>
loop do
print "Much better."
print "Ah. More space!"
print "My back was killin' me in those crab pincers."
end
<stdout>Much better.Ah. More space!My back was killin' me in those crab pincers.</stdout>
</pre>
h3. Block arguments
Block arguments are a set of variables surrounded by *pipe*
characters and separated by *commas*.
bq. @|en||x|@, @|x,y|@, and @|up, down, all_around|@ are examples.
Block arguments are used at the beginning of a block.
bq. @{ |x,y| x + y }@
In the above example, @|x,y|@ are the arguments. After the arguments, we have a bit of
code. The expression @x + y@ adds the two arguments together.
I like to think of the pipe characters as representing a tunnel. They give the appearance
of a chute that the variables are sliding down. (An @x@ goes down spread eagle, while the @y@
neatly crosses her legs.) This chute acts as a passageway between blocks and the world around them.
Variables are passed through this chute (or tunnel) into the block.
!i/the.foxes-4g.png(And then, the dismal truth.)!
h3. Ranges
A range is two values surrounded by *parentheses* and separated by *an ellipsis* (in the
form of two or three dots).
bq. @(1..3)@ is a range, representing the numbers 1 through 3.