Skip to content
This repository was archived by the owner on Jul 24, 2022. It is now read-only.

Commit 881d9cd

Browse files
committed
my solutions to the ruby koans path
0 parents  commit 881d9cd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+4103
-0
lines changed

.path_progress

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0,5,5,6,8,8,11,15,18,21,23,24,31,38,39,39,39,40,41,41,42,43,45,46,48,49,50,50,50,52,54,56,57,60,65,66,70,73,74,75,76,77,78,78,78,79,81,83,83,83,84,85,86,86,86,86,87,88,88,88,89,89,91,91,92,93,94,94,96,97,97,97,98,98,101,102,102,104,105,106,107,110,113,113,113,113,113,113,114,115,120,124,126,127,129,131,129,131,135,145,146,150,150,153,154,154,154,155,158,159,160,165,165,168,168,168,171,177,177,178,182,187,190,183,189,190,183,189,189,190,183,183,190,183,190,189,189,190,187,187,190,187,187,192,197,199,205,205,206,209,210,226,227,228,229,231,234,235,238,239,239,239,242,245,247,253,256,257,258,259,259,260,260,261,263,276,276,276,277,279

GREED_RULES.txt

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
= Playing Greed
2+
3+
Greed is a dice game played among 2 or more players, using 5
4+
six-sided dice.
5+
6+
== Playing Greed
7+
8+
Each player takes a turn consisting of one or more rolls of the dice.
9+
On the first roll of the game, a player rolls all five dice which are
10+
scored according to the following:
11+
12+
Three 1's => 1000 points
13+
Three 6's => 600 points
14+
Three 5's => 500 points
15+
Three 4's => 400 points
16+
Three 3's => 300 points
17+
Three 2's => 200 points
18+
One 1 => 100 points
19+
One 5 => 50 points
20+
21+
A single die can only be counted once in each roll. For example,
22+
a "5" can only count as part of a triplet (contributing to the 500
23+
points) or as a single 50 points, but not both in the same roll.
24+
25+
Example Scoring
26+
27+
Throw Score
28+
--------- ------------------
29+
5 1 3 4 1 50 + 2 * 100 = 250
30+
1 1 1 3 1 1000 + 100 = 1100
31+
2 4 4 5 4 400 + 50 = 450
32+
33+
The dice not contributing to the score are called the non-scoring
34+
dice. "3" and "4" are non-scoring dice in the first example. "3" is
35+
a non-scoring die in the second, and "2" is a non-score die in the
36+
final example.
37+
38+
After a player rolls and the score is calculated, the scoring dice are
39+
removed and the player has the option of rolling again using only the
40+
non-scoring dice. If all of the thrown dice are scoring, then the
41+
player may roll all 5 dice in the next roll.
42+
43+
The player may continue to roll as long as each roll scores points. If
44+
a roll has zero points, then the player loses not only their turn, but
45+
also accumulated score for that turn. If a player decides to stop
46+
rolling before rolling a zero-point roll, then the accumulated points
47+
for the turn is added to his total score.
48+
49+
== Getting "In The Game"
50+
51+
Before a player is allowed to accumulate points, they must get at
52+
least 300 points in a single turn. Once they have achieved 300 points
53+
in a single turn, the points earned in that turn and each following
54+
turn will be counted toward their total score.
55+
56+
== End Game
57+
58+
Once a player reaches 3000 (or more) points, the game enters the final
59+
round where each of the other players gets one more turn. The winner
60+
is the player with the highest score after the final round.
61+
62+
== References
63+
64+
Greed is described on Wikipedia at
65+
http://en.wikipedia.org/wiki/Greed_(dice_game), however the rules are
66+
a bit different from the rules given here.

README.rdoc

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

Rakefile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env ruby
2+
# -*- ruby -*-
3+
4+
require 'rake/clean'
5+
require 'rake/testtask'
6+
7+
task :default => :test
8+
9+
task :test do
10+
ruby 'path_to_enlightenment.rb'
11+
end
12+

about_array_assignment.rb

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
require File.expand_path(File.dirname(__FILE__) + '/neo')
2+
3+
class AboutArrayAssignment < Neo::Koan
4+
def test_non_parallel_assignment
5+
names = ["John", "Smith"]
6+
assert_equal ["John", "Smith"], names
7+
end
8+
9+
def test_parallel_assignments
10+
first_name, last_name = ["John", "Smith"]
11+
assert_equal "John", first_name
12+
assert_equal "Smith", last_name
13+
end
14+
15+
def test_parallel_assignments_with_extra_values
16+
first_name, last_name = ["John", "Smith", "III"]
17+
assert_equal "John", first_name
18+
assert_equal "Smith", last_name
19+
end
20+
21+
def test_parallel_assignments_with_splat_operator
22+
first_name, *last_name = ["John", "Smith", "III"]
23+
assert_equal "John", first_name
24+
assert_equal ["Smith", "III"], last_name
25+
end
26+
27+
def test_parallel_assignments_with_too_few_variables
28+
first_name, last_name = ["Cher"]
29+
assert_equal "Cher", first_name
30+
assert_equal nil, last_name
31+
end
32+
33+
def test_parallel_assignments_with_subarrays
34+
first_name, last_name = [["Willie", "Rae"], "Johnson"]
35+
assert_equal ["Willie", "Rae"], first_name
36+
assert_equal "Johnson", last_name
37+
end
38+
39+
def test_parallel_assignment_with_one_variable
40+
first_name, = ["John", "Smith"]
41+
assert_equal "John", first_name
42+
end
43+
44+
def test_swapping_with_parallel_assignment
45+
first_name = "Roy"
46+
last_name = "Rob"
47+
first_name, last_name = last_name, first_name
48+
assert_equal "Rob", first_name
49+
assert_equal "Roy", last_name
50+
end
51+
end

0 commit comments

Comments
 (0)