Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Trskldn committed Dec 6, 2011
0 parents commit 3402ad6
Show file tree
Hide file tree
Showing 95 changed files with 7,795 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
@@ -0,0 +1,4 @@
dist
.project_env.rc
.path_progress
*.rbc
146 changes: 146 additions & 0 deletions README.rdoc
@@ -0,0 +1,146 @@
= EdgeCase Ruby Koans

The Ruby Koans walk you along the path to enlightenment in order to learn Ruby.
The goal is to learn the Ruby language, syntax, structure, and some common
functions and libraries. We also teach you culture. Testing is not just something we
pay lip service to, but something we live. It is essential in your quest to learn
and do great things in the language.

== The Structure

The koans are broken out into areas by file, hashes are covered in about_hashes.rb,
modules are introduced in about_modules.rb, etc. They are presented in order in the
path_to_enlightenment.rb file.

Each koan builds up your knowledge of Ruby and builds upon itself. It will stop at
the first place you need to correct.

Some koans simply need to have the correct answer substituted for an incorrect one.
Some, however, require you to supply your own answer. If you see the method +__+ (a
double underscore) listed, it is a hint to you to supply your own code in order to
make it work correctly.

== Installing Ruby

If you do not have Ruby setup, please visit http://ruby-lang.org/en/downloads/ for
operating specific instructions. In order to run this you need ruby and rake
installed. To check the installations simply type:

*nix platforms from any terminal window:

[~] $ ruby --version
[~] $ rake --version

Windows from the command prompt (cmd.exe)

c:\ruby --version
c:\rake --version

If you don't have rake installed, just run `gem install rake`

Any response for Ruby with a version number greater than 1.8 is fine (should be
around 1.8.6 or more). Any version of rake will do.

== The Path To Enlightenment

You can run the tests through rake or by calling the file itself (rake is the
recommended way to run them as we might build more functionality into this task).

*nix platforms, from the koans directory

[ruby_koans] $ rake # runs the default target :walk_the_path
[ruby_koans] $ ruby path_to_enlightenment.rb # simply call the file directly

Windows is the same thing

c:\ruby_koans\rake # runs the default target :walk_the_path
c:\ruby_koans\ruby path_to_enlightenment.rb # simply call the file directly

=== Red, Green, Refactor

In test-driven development the mantra has always been, red, green, refactor. Write a
failing test and run it (red), make the test pass (green), then refactor it (that is
look at the code and see if you can make it any better. In this case you will need
to run the koan and see it fail (red), make the test pass (green), then take a
moment and reflect upon the test to see what it is teaching you and improve the
code to better communicate its intent (refactor).

The very first time you run it you will see the following output:

[ ruby_koans ] $ rake
(in /Users/person/dev/ruby_koans)
cd koans

Thinking AboutAsserts
test_assert_truth has damaged your karma.

You have not yet reached enlightenment ...
<false> is not true.

Please meditate on the following code:
./about_asserts.rb:10:in `test_assert_truth'
path_to_enlightenment.rb:27

mountains are merely mountains

You have come to your first stage. If you notice it is telling you where to look for
the first solution:

Please meditate on the following code:
./about_asserts.rb:10:in `test_assert_truth'
path_to_enlightenment.rb:27

We then open up the about_asserts.rb file and look at the first test:

# We shall contemplate truth by testing reality, via asserts.
def test_assert_truth
assert false # This should be true
end

We then change the +false+ to +true+ and run the test again. After you are
done, think about what you are learning. In this case, ignore everything except
the method name (+test_assert_truth+) and the parts inside the method (everything
before the +end+).

In this case the goal is for you to see that if you pass a value to the +assert+
method, it will either ensure it is +true+ and continue on, or fail if in fact
the statement is +false+.

== Inspiration

A special thanks to Mike Clark and Ara Howard for inspiring this
project. Mike Clark wrote an excellent blog post about learning Ruby
through unit testing. This sparked an idea that has taken a bit to
solidify, that of bringing new rubyists into the community through
testing. Ara Howard then gave us the idea for the Koans in his ruby
quiz entry on Meta Koans (a must for any rubyist wanting to improve
their skills). Also, "The Little Lisper" taught us all the value of
the short questions/simple answers style of learning.

Mike Clark's post :: http://www.clarkware.com/cgi/blosxom/2005/03/18
Meta Koans :: http://rubyquiz.com/quiz67.html
The Little Lisper :: http://www.amazon.com/Little-LISPer-Third-Daniel-Friedman/dp/0023397632

== Other Resources

The Ruby Language :: http://ruby-lang.org
Try Ruby in your browser :: http://tryruby.org

Dave Thomas' introduction to Ruby Programming Ruby (the Pick Axe) :: http://pragprog.com/titles/ruby/programming-ruby

Brian Marick's fantastic guide for beginners Everyday Scripting with Ruby :: http://pragprog.com/titles/bmsft/everyday-scripting-with-ruby

= Other stuff

Author :: Jim Weirich <jim@weirichhouse.org>
Author :: Joe O'Brien <joe@edgecase.com>
Issue Tracker :: http://www.pivotaltracker.com/projects/48111
Requires :: Ruby 1.8.x or later and Rake (any recent version)

= License

http://i.creativecommons.org/l/by-nc-sa/3.0/88x31.png

RubyKoans is released under a Creative Commons,
Attribution-NonCommercial-ShareAlike, Version 3.0
(http://creativecommons.org/licenses/by-nc-sa/3.0/) License.
166 changes: 166 additions & 0 deletions Rakefile
@@ -0,0 +1,166 @@
#!/usr/bin/env ruby
# -*- ruby -*-

require 'rake/clean'
require 'rake/rdoctask'

SRC_DIR = 'src'
PROB_DIR = 'koans'
DIST_DIR = 'dist'

SRC_FILES = FileList["#{SRC_DIR}/*"]
KOAN_FILES = SRC_FILES.pathmap("#{PROB_DIR}/%f")

today = Time.now.strftime("%Y-%m-%d")
TAR_FILE = "#{DIST_DIR}/rubykoans-#{today}.tgz"
ZIP_FILE = "#{DIST_DIR}/rubykoans-#{today}.zip"

CLEAN.include("**/*.rbc")
CLOBBER.include(DIST_DIR)

module Koans
# Remove solution info from source
# __(a,b) => __
# _n_(number) => __
# # __ =>
def Koans.remove_solution(line)
line = line.gsub(/\b____\([^\)]+\)/, "____")
line = line.gsub(/\b___\([^\)]+\)/, "___")
line = line.gsub(/\b__\([^\)]+\)/, "__")
line = line.gsub(/\b_n_\([^\)]+\)/, "_n_")
line = line.gsub(%r(/\#\{__\}/), "/__/")
line = line.gsub(/\s*#\s*__\s*$/, '')
line
end

def Koans.make_koan_file(infile, outfile)
if infile =~ /edgecase/
cp infile, outfile
elsif infile =~ /autotest/
cp_r infile, outfile
else
open(infile) do |ins|
open(outfile, "w") do |outs|
state = :copy
ins.each do |line|
state = :skip if line =~ /^ *#--/
case state
when :copy
outs.puts remove_solution(line)
else
# do nothing
end
state = :copy if line =~ /^ *#\+\+/
end
end
end
end
end
end

module RubyImpls
# Calculate the list of relevant Ruby implementations.
def self.find_ruby_impls
rubys = `rvm list`.gsub(/=>/,'').split(/\n/).sort
expected.map { |impl|
last = rubys.grep(Regexp.new(Regexp.quote(impl))).last
last ? last.split.first : nil
}.compact
end

# Return a (cached) list of relevant Ruby implementations.
def self.list
@list ||= find_ruby_impls
end

# List of expected ruby implementations.
def self.expected
%w(ruby-1.8.6 ruby-1.8.7 ruby-1.9.2 jruby ree)
end
end

task :default => :walk_the_path

task :walk_the_path do
cd 'koans'
ruby 'path_to_enlightenment.rb'
end

Rake::RDocTask.new do |rd|
rd.main = "README.rdoc"
rd.rdoc_files.include("README.rdoc", "koans/*.rb")
end

directory DIST_DIR
directory PROB_DIR

file ZIP_FILE => KOAN_FILES + [DIST_DIR] do
sh "zip #{ZIP_FILE} #{PROB_DIR}/*"
end

file TAR_FILE => KOAN_FILES + [DIST_DIR] do
sh "tar zcvf #{TAR_FILE} #{PROB_DIR}"
end

desc "Create packaged files for distribution"
task :package => [TAR_FILE, ZIP_FILE]

desc "Upload the package files to the web server"
task :upload => [TAR_FILE, ZIP_FILE] do
sh "scp #{TAR_FILE} linode:sites/onestepback.org/download"
sh "scp #{ZIP_FILE} linode:sites/onestepback.org/download"
end

desc "Generate the Koans from the source files from scratch."
task :regen => [:clobber_koans, :gen]

desc "Generate the Koans from the changed source files."
task :gen => KOAN_FILES + [PROB_DIR + "/README.rdoc"]
task :clobber_koans do
rm_r PROB_DIR
end

file PROB_DIR + "/README.rdoc" => "README.rdoc" do |t|
cp "README.rdoc", t.name
end

SRC_FILES.each do |koan_src|
file koan_src.pathmap("#{PROB_DIR}/%f") => [PROB_DIR, koan_src] do |t|
Koans.make_koan_file koan_src, t.name
end
end

task :run do
puts 'koans'
Dir.chdir("src") do
puts "in #{Dir.pwd}"
sh "ruby path_to_enlightenment.rb"
end
end


desc "Pre-checkin tests (=> run_all)"
task :cruise => :run_all

desc "Run the completed koans againts a list of relevant Ruby Implementations"
task :run_all do
results = []
RubyImpls.list.each do |impl|
puts "=" * 40
puts "On Ruby #{impl}"
sh "rvm #{impl} rake run"
results << [impl, "RAN"]
puts
end
puts "=" * 40
puts "Summary:"
puts
results.each do |impl, res|
puts "#{impl} => RAN"
end
puts
RubyImpls.expected.each do |requested_impl|
impl_pattern = Regexp.new(Regexp.quote(requested_impl))
puts "No Results for #{requested_impl}" unless results.detect { |x| x.first =~ impl_pattern }
end
end
Binary file added keynote/RubyKoans.key
Binary file not shown.
66 changes: 66 additions & 0 deletions koans/GREED_RULES.txt
@@ -0,0 +1,66 @@
= Playing Greed

Greed is a dice game played among 2 or more players, using 5
six-sided dice.

== Playing Greed

Each player takes a turn consisting of one or more rolls of the dice.
On the first roll of the game, a player rolls all five dice which are
scored according to the following:

Three 1's => 1000 points
Three 6's => 600 points
Three 5's => 500 points
Three 4's => 400 points
Three 3's => 300 points
Three 2's => 200 points
One 1 => 100 points
One 5 => 50 points

A single die can only be counted once in each roll. For example,
a "5" can only count as part of a triplet (contributing to the 500
points) or as a single 50 points, but not both in the same roll.

Example Scoring

Throw Score
--------- ------------------
5 1 3 4 1 50 + 2 * 100 = 250
1 1 1 3 1 1000 + 100 = 1100
2 4 4 5 4 400 + 50 = 450

The dice not contributing to the score are called the non-scoring
dice. "3" and "4" are non-scoring dice in the first example. "3" is
a non-scoring die in the second, and "2" is a non-score die in the
final example.

After a player rolls and the score is calculated, the scoring dice are
removed and the player has the option of rolling again using only the
non-scoring dice. If all of the thrown dice are scoring, then the
player may roll all 5 dice in the next roll.

The player may continue to roll as long as each roll scores points. If
a roll has zero points, then the player loses not only their turn, but
also accumulated score for that turn. If a player decides to stop
rolling before rolling a zero-point roll, then the accumulated points
for the turn is added to his total score.

== Getting "In The Game"

Before a player is allowed to accumulate points, they must get at
least 300 points in a single turn. Once they have achieved 300 points
in a single turn, the points earned in that turn and each following
turn will be counted toward their total score.

== End Game

Once a player reaches 3000 (or more) points, the game enters the final
round where each of the other players gets one more turn. The winner
is the player with the highest score after the final round.

== References

Greed is described on Wikipedia at
http://en.wikipedia.org/wiki/Greed_(dice_game), however the rules are
a bit different from the rules given here.

0 comments on commit 3402ad6

Please sign in to comment.