NRuby
191 строка · 7.0 Кб
1= Ruby Koans
2
3The Ruby Koans walk you along the path to enlightenment in order to learn Ruby.
4The goal is to learn the Ruby language, syntax, structure, and some common
5functions and libraries. We also teach you culture by basing the koans on tests.
6Testing is not just something we pay lip service to, but something we
7live. Testing is essential in your quest to learn and do great things in Ruby.
8
9== The Structure
10
11The koans are broken out into areas by file, hashes are covered in +about_hashes.rb+,
12modules are introduced in +about_modules.rb+, <em>etc</em>. They are presented in
13order in the +path_to_enlightenment.rb+ file.
14
15Each koan builds up your knowledge of Ruby and builds upon itself. It will stop at
16the first place you need to correct.
17
18Some koans simply need to have the correct answer substituted for an incorrect one.
19Some, however, require you to supply your own answer. If you see the method +__+ (a
20double underscore) listed, it is a hint to you to supply your own code in order to
21make it work correctly.
22
23== Installing Ruby
24
25If you do not have Ruby setup, please visit http://ruby-lang.org/en/downloads/ for
26operating specific instructions. In order to run the koans you need +ruby+ and
27+rake+ installed. To check your installations simply type:
28
29*nix platforms from any terminal window:
30
31[~] $ ruby --version
32[~] $ rake --version
33
34Windows from the command prompt (+cmd.exe+)
35
36c:\ruby --version
37c:\rake --version
38
39If you don't have +rake+ installed, just run <code>gem install rake</code>
40
41Any response for Ruby with a version number greater than 1.8 is fine (should be
42around 1.8.6 or more). Any version of +rake+ will do.
43
44== Generating the Koans
45
46A fresh checkout will not include the koans, you will need to generate
47them.
48
49[ruby_koans] $ rake gen # generates the koans directory
50
51If you need to regenerate the koans, thus wiping your current `koans`,
52
53[ruby_koans] $ rake regen # regenerates the koans directory, wiping the original
54
55== The Path To Enlightenment
56
57You can run the tests through +rake+ or by calling the file itself (+rake+ is the
58recommended way to run them as we might build more functionality into this task).
59
60*nix platforms, from the +ruby_koans+ directory
61
62[ruby_koans] $ rake # runs the default target :walk_the_path
63[ruby_koans] $ ruby path_to_enlightenment.rb # simply call the file directly
64
65Windows is the same thing
66
67c:\ruby_koans\rake # runs the default target :walk_the_path
68c:\ruby_koans\ruby path_to_enlightenment.rb # simply call the file directly
69
70=== Red, Green, Refactor
71
72In test-driven development the mantra has always been <em>red, green, refactor</em>.
73Write a failing test and run it (<em>red</em>), make the test pass (<em>green</em>),
74then look at the code and consider if you can make it any better (<em>refactor</em>).
75
76While walking the path to Ruby enlightenment you will need to run the koan and
77see it fail (<em>red</em>), make the test pass (<em>green</em>), then take a moment
78and reflect upon the test to see what it is teaching you and improve the code to
79better communicate its intent (<em>refactor</em>).
80
81The very first time you run the koans you will see the following output:
82
83[ ruby_koans ] $ rake
84(in /Users/person/dev/ruby_koans)
85/usr/bin/ruby1.8 path_to_enlightenment.rb
86
87AboutAsserts#test_assert_truth has damaged your karma.
88
89The Master says:
90You have not yet reached enlightenment.
91
92The answers you seek...
93<false> is not true.
94
95Please meditate on the following code:
96./about_asserts.rb:10:in `test_assert_truth'
97path_to_enlightenment.rb:38:in `each_with_index'
98path_to_enlightenment.rb:38
99
100mountains are merely mountains
101your path thus far [X_________________________________________________] 0/280 (0%)
102
103You have come to your first stage. Notice it is telling you where to look for
104the first solution:
105
106Please meditate on the following code:
107./about_asserts.rb:10:in `test_assert_truth'
108path_to_enlightenment.rb:38:in `each_with_index'
109path_to_enlightenment.rb:38
110
111Open the +about_asserts.rb+ file and look at the first test:
112
113# We shall contemplate truth by testing reality, via asserts.
114def test_assert_truth
115assert false # This should be true
116end
117
118Change the +false+ to +true+ and re-run the test. After you are
119done, think about what you are learning. In this case, ignore everything except
120the method name (+test_assert_truth+) and the parts inside the method (everything
121before the +end+).
122
123In this case the goal is for you to see that if you pass a value to the +assert+
124method, it will either ensure it is +true+ and continue on, or fail if
125the statement is +false+.
126
127=== Running the Koans automatically
128
129<em>This section is optional.</em>
130
131Normally the path to enlightenment looks like this:
132
133cd ruby_koans
134rake
135# edit
136rake
137# edit
138rake
139# etc
140
141If you prefer, you can keep the koans running in the background so that after you
142make a change in your editor, the koans will immediately run again. This will
143hopefully keep your focus on learning Ruby instead of on the command line.
144
145Install the Ruby gem (library) called +observr+ and then ask it to
146"watch" the koans for changes:
147
148cd ruby_koans
149rake
150# decide to run rake automatically from now on as you edit
151gem install observr
152observr ./koans/koans.watchr
153
154== Inspiration
155
156A special thanks to Mike Clark and Ara Howard for inspiring this
157project. Mike Clark wrote an excellent blog post about learning Ruby
158through unit testing. This sparked an idea that has taken a bit to
159solidify, that of bringing new rubyists into the community through
160testing. Ara Howard then gave us the idea for the Koans in his ruby
161quiz entry on Meta Koans (a must for any rubyist wanting to improve
162their skills). Also, "The Little Lisper" taught us all the value of
163the short questions/simple answers style of learning.
164
165Mike Clark's post :: http://www.clarkware.com/cgi/blosxom/2005/03/18
166Meta Koans :: http://rubyquiz.com/quiz67.html
167The Little Lisper :: http://www.amazon.com/Little-LISPer-Third-Daniel-Friedman/dp/0023397632
168
169== Other Resources
170
171The Ruby Language :: http://ruby-lang.org
172Try Ruby in your browser :: http://tryruby.org
173
174Dave Thomas' introduction to Ruby Programming Ruby (the Pick Axe) :: http://pragprog.com/titles/ruby/programming-ruby
175
176Brian Marick's fantastic guide for beginners Everyday Scripting with Ruby :: http://pragprog.com/titles/bmsft/everyday-scripting-with-ruby
177
178= Other stuff
179
180Author :: Jim Weirich <jim@neo.org>
181Author :: Joe O'Brien <joe@objo.com>
182Issue Tracker :: https://github.com/edgecase/ruby_koans/issues
183Requires :: Ruby 1.8.x or later and Rake (any recent version)
184
185= License
186
187http://i.creativecommons.org/l/by-nc-sa/3.0/88x31.png
188
189RubyKoans is released under a Creative Commons,
190Attribution-NonCommercial-ShareAlike, Version 3.0
191(http://creativecommons.org/licenses/by-nc-sa/3.0/) License.
192