Skip to content

Commit 649daba

Browse files
committed
First checkin
0 parents  commit 649daba

File tree

98 files changed

+7809
-0
lines changed

Some content is hidden

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

98 files changed

+7809
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
dist
2+
.project_env.rc
3+
.path_progress
4+
*.rbc

MIT-LICENSE

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Copyright (c) 2009 EdgeCase
2+
3+
Permission is hereby granted, free of charge, to any person obtaining
4+
a copy of this software and associated documentation files (the
5+
"Software"), to deal in the Software without restriction, including
6+
without limitation the rights to use, copy, modify, merge, publish,
7+
distribute, sublicense, and/or sell copies of the Software, and to
8+
permit persons to whom the Software is furnished to do so, subject to
9+
the following conditions:
10+
11+
The above copyright notice and this permission notice shall be
12+
included in all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.rdoc

+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
= EdgeCase 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. Testing is not just something we
6+
pay lip service to, but something we live. It is essential in your quest to learn
7+
and do great things in the language.
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, etc. They are presented in order in the
13+
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 this you need ruby and rake
27+
installed. To check the 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+
Any response for Ruby with a version number greater than 1.8 is fine (should be
40+
around 1.8.6 or more). Any version of rake will do.
41+
42+
== The Path To Enlightenment
43+
44+
You can run the tests through rake or by calling the file itself (rake is the
45+
recommended way to run them as we might build more functionality into this task).
46+
47+
*nix platforms, from the koans directory
48+
49+
[ruby_koans] $ rake # runs the default target :walk_the_path
50+
[ruby_koans] $ ruby path_to_enlightenment.rb # simply call the file directly
51+
52+
Windows is the same thing
53+
54+
c:\ruby_koans\rake # runs the default target :walk_the_path
55+
c:\ruby_koans\ruby path_to_enlightenment.rb # simply call the file directly
56+
57+
=== Red, Green, Refactor
58+
59+
In test-driven development the mantra has always been, red, green, refactor. Write a
60+
failing test and run it (red), make the test pass (green), then refactor it (that is
61+
look at the code and see if you can make it any better. In this case you will need
62+
to run the koan and see it fail (red), make the test pass (green), then take a
63+
moment and reflect upon the test to see what it is teaching you and improve the
64+
code to better communicate its intent (refactor).
65+
66+
The very first time you run it you will see the following output:
67+
68+
[ ruby_koans ] $ rake
69+
(in /Users/person/dev/ruby_koans)
70+
cd koans
71+
72+
Thinking AboutAsserts
73+
test_assert_truth has damaged your karma.
74+
75+
You have not yet reached enlightenment ...
76+
<false> is not true.
77+
78+
Please meditate on the following code:
79+
./about_asserts.rb:10:in `test_assert_truth'
80+
path_to_enlightenment.rb:27
81+
82+
mountains are merely mountains
83+
84+
You have come to your first stage. If you notice it is telling you where to look for
85+
the first solution:
86+
87+
Please meditate on the following code:
88+
./about_asserts.rb:10:in `test_assert_truth'
89+
path_to_enlightenment.rb:27
90+
91+
We then open up the about_asserts.rb file and look at the first test:
92+
93+
# We shall contemplate truth by testing reality, via asserts.
94+
def test_assert_truth
95+
assert false # This should be true
96+
end
97+
98+
We then change the +false+ to +true+ and run the test again. After you are
99+
done, think about what you are learning. In this case, ignore everything except
100+
the method name (+test_assert_truth+) and the parts inside the method (everything
101+
before the +end+).
102+
103+
In this case the goal is for you to see that if you pass a value to the +assert+
104+
method, it will either ensure it is +true+ and continue on, or fail if in fact
105+
the statement is +false+.
106+
107+
== Inspiration
108+
109+
A special thanks to Mike Clark and Ara Howard for inspiring this
110+
project. Mike Clark wrote an excellent blog post about learning Ruby
111+
through unit testing. This sparked an idea that has taken a bit to
112+
solidify, that of bringing new rubyists into the community through
113+
testing. Ara Howard then gave us the idea for the Koans in his ruby
114+
quiz entry on Meta Koans (a must for any rubyist wanting to improve
115+
their skills). Also, "The Little Lisper" taught us all the value of
116+
the short questions/simple answers style of learning.
117+
118+
Mike Clark's post :: http://www.clarkware.com/cgi/blosxom/2005/03/18
119+
Meta Koans :: http://rubyquiz.com/quiz67.html
120+
The Little Lisper :: http://www.amazon.com/Little-LISPer-Third-Daniel-Friedman/dp/0023397632
121+
122+
== Other Resources
123+
124+
The Ruby Language :: http://ruby-lang.org
125+
Try Ruby in your browser :: http://tryruby.org
126+
127+
Dave Thomas' introduction to Ruby Programming Ruby (the Pick Axe) :: http://pragprog.com/titles/ruby/programming-ruby
128+
129+
Brian Marick's fantastic guide for beginners Everyday Scripting with Ruby :: http://pragprog.com/titles/bmsft/everyday-scripting-with-ruby
130+
131+
= Other stuff
132+
133+
Author :: Jim Weirich <[email protected]>
134+
Author :: Joe O'Brien <[email protected]>
135+
Issue Tracker :: http://www.pivotaltracker.com/projects/48111
136+
Requires :: Ruby 1.8.x or later and Rake (any recent version)

Rakefile

+166
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
#!/usr/bin/env ruby
2+
# -*- ruby -*-
3+
4+
require 'rake/clean'
5+
require 'rake/rdoctask'
6+
7+
SRC_DIR = 'src'
8+
PROB_DIR = 'koans'
9+
DIST_DIR = 'dist'
10+
11+
SRC_FILES = FileList["#{SRC_DIR}/*"]
12+
KOAN_FILES = SRC_FILES.pathmap("#{PROB_DIR}/%f")
13+
14+
today = Time.now.strftime("%Y-%m-%d")
15+
TAR_FILE = "#{DIST_DIR}/rubykoans-#{today}.tgz"
16+
ZIP_FILE = "#{DIST_DIR}/rubykoans-#{today}.zip"
17+
18+
CLEAN.include("**/*.rbc")
19+
CLOBBER.include(DIST_DIR)
20+
21+
module Koans
22+
# Remove solution info from source
23+
# __(a,b) => __
24+
# _n_(number) => __
25+
# # __ =>
26+
def Koans.remove_solution(line)
27+
line = line.gsub(/\b____\([^\)]+\)/, "____")
28+
line = line.gsub(/\b___\([^\)]+\)/, "___")
29+
line = line.gsub(/\b__\([^\)]+\)/, "__")
30+
line = line.gsub(/\b_n_\([^\)]+\)/, "_n_")
31+
line = line.gsub(%r(/\#\{__\}/), "/__/")
32+
line = line.gsub(/\s*#\s*__\s*$/, '')
33+
line
34+
end
35+
36+
def Koans.make_koan_file(infile, outfile)
37+
if infile =~ /edgecase/
38+
cp infile, outfile
39+
elsif infile =~ /autotest/
40+
cp_r infile, outfile
41+
else
42+
open(infile) do |ins|
43+
open(outfile, "w") do |outs|
44+
state = :copy
45+
ins.each do |line|
46+
state = :skip if line =~ /^ *#--/
47+
case state
48+
when :copy
49+
outs.puts remove_solution(line)
50+
else
51+
# do nothing
52+
end
53+
state = :copy if line =~ /^ *#\+\+/
54+
end
55+
end
56+
end
57+
end
58+
end
59+
end
60+
61+
module RubyImpls
62+
# Calculate the list of relevant Ruby implementations.
63+
def self.find_ruby_impls
64+
rubys = `rvm list`.gsub(/=>/,'').split(/\n/).sort
65+
expected.map { |impl|
66+
last = rubys.grep(Regexp.new(Regexp.quote(impl))).last
67+
last ? last.split.first : nil
68+
}.compact
69+
end
70+
71+
# Return a (cached) list of relevant Ruby implementations.
72+
def self.list
73+
@list ||= find_ruby_impls
74+
end
75+
76+
# List of expected ruby implementations.
77+
def self.expected
78+
%w(ruby-1.8.6 ruby-1.8.7 ruby-1.9.2 jruby ree)
79+
end
80+
end
81+
82+
task :default => :walk_the_path
83+
84+
task :walk_the_path do
85+
cd 'koans'
86+
ruby 'path_to_enlightenment.rb'
87+
end
88+
89+
Rake::RDocTask.new do |rd|
90+
rd.main = "README.rdoc"
91+
rd.rdoc_files.include("README.rdoc", "koans/*.rb")
92+
end
93+
94+
directory DIST_DIR
95+
directory PROB_DIR
96+
97+
file ZIP_FILE => KOAN_FILES + [DIST_DIR] do
98+
sh "zip #{ZIP_FILE} #{PROB_DIR}/*"
99+
end
100+
101+
file TAR_FILE => KOAN_FILES + [DIST_DIR] do
102+
sh "tar zcvf #{TAR_FILE} #{PROB_DIR}"
103+
end
104+
105+
desc "Create packaged files for distribution"
106+
task :package => [TAR_FILE, ZIP_FILE]
107+
108+
desc "Upload the package files to the web server"
109+
task :upload => [TAR_FILE, ZIP_FILE] do
110+
sh "scp #{TAR_FILE} linode:sites/onestepback.org/download"
111+
sh "scp #{ZIP_FILE} linode:sites/onestepback.org/download"
112+
end
113+
114+
desc "Generate the Koans from the source files from scratch."
115+
task :regen => [:clobber_koans, :gen]
116+
117+
desc "Generate the Koans from the changed source files."
118+
task :gen => KOAN_FILES + [PROB_DIR + "/README.rdoc"]
119+
task :clobber_koans do
120+
rm_r PROB_DIR
121+
end
122+
123+
file PROB_DIR + "/README.rdoc" => "README.rdoc" do |t|
124+
cp "README.rdoc", t.name
125+
end
126+
127+
SRC_FILES.each do |koan_src|
128+
file koan_src.pathmap("#{PROB_DIR}/%f") => [PROB_DIR, koan_src] do |t|
129+
Koans.make_koan_file koan_src, t.name
130+
end
131+
end
132+
133+
task :run do
134+
puts 'koans'
135+
Dir.chdir("src") do
136+
puts "in #{Dir.pwd}"
137+
sh "ruby path_to_enlightenment.rb"
138+
end
139+
end
140+
141+
142+
desc "Pre-checkin tests (=> run_all)"
143+
task :cruise => :run_all
144+
145+
desc "Run the completed koans againts a list of relevant Ruby Implementations"
146+
task :run_all do
147+
results = []
148+
RubyImpls.list.each do |impl|
149+
puts "=" * 40
150+
puts "On Ruby #{impl}"
151+
sh "rvm #{impl} rake run"
152+
results << [impl, "RAN"]
153+
puts
154+
end
155+
puts "=" * 40
156+
puts "Summary:"
157+
puts
158+
results.each do |impl, res|
159+
puts "#{impl} => RAN"
160+
end
161+
puts
162+
RubyImpls.expected.each do |requested_impl|
163+
impl_pattern = Regexp.new(Regexp.quote(requested_impl))
164+
puts "No Results for #{requested_impl}" unless results.detect { |x| x.first =~ impl_pattern }
165+
end
166+
end

assert.rb

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
require 'test/unit'
2+
3+
class TestTruth < Test::Unit::TestCase
4+
def test_true
5+
assert true
6+
end
7+
end

keynote/RubyKoans.key

377 KB
Binary file not shown.

0 commit comments

Comments
 (0)