Skip to content
Browse files

Merge pull request #2 from neo/master

Pull back from neo organization
  • Loading branch information...
2 parents 5c5ffc6 + 8068f1f commit a1d3902b011cdceeb8853871fc454a8a26d7d580 @mdoel mdoel committed Mar 12, 2015
View
3 .gitignore
@@ -1,5 +1,8 @@
dist
.project_env.rc
.path_progress
+.rvmrc
+.ruby-version
*.rbc
koans/*
+*~
View
12 DEPLOYING
@@ -0,0 +1,12 @@
+= Deploying a new Ruby Koans ZIP file
+
+The "Download" button on the rubykoans.com web-site points to the
+download/rubykoans.zip file in the github repository. So to update the
+download target on the web-site, just rebuild the .zip file, commit
+and push the changes.
+
+ rake package
+ git add download
+ git push
+
+That's it.
View
83 README.rdoc
@@ -2,17 +2,17 @@
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.
+functions and libraries. We also teach you culture by basing the koans on tests.
+Testing is not just something we pay lip service to, but something we
+live. Testing is essential in your quest to learn and do great things in Ruby.
== 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.
+The koans are broken out into areas by file, hashes are covered in +about_hashes.rb+,
+modules are introduced in +about_modules.rb+, <em>etc</em>. 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
+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.
@@ -23,23 +23,23 @@ 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:
+operating specific instructions. In order to run the koans you need +ruby+ and
++rake+ installed. To check your installations simply type:
*nix platforms from any terminal window:
[~] $ ruby --version
[~] $ rake --version
-Windows from the command prompt (cmd.exe)
+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`
+If you don't have +rake+ installed, just run <code>gem install rake</code>
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.
+around 1.8.6 or more). Any version of +rake+ will do.
== Generating the Koans
@@ -54,10 +54,10 @@ If you need to regenerate the koans, thus wiping your current `koans`,
== The Path To Enlightenment
-You can run the tests through rake or by calling the file itself (rake is the
+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
+*nix platforms, from the +ruby_koans+ directory
[ruby_koans] $ rake # runs the default target :walk_the_path
[ruby_koans] $ ruby path_to_enlightenment.rb # simply call the file directly
@@ -69,14 +69,16 @@ Windows is the same thing
=== 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).
+In test-driven development the mantra has always been <em>red, green, refactor</em>.
+Write a failing test and run it (<em>red</em>), make the test pass (<em>green</em>),
+then look at the code and consider if you can make it any better (<em>refactor</em>).
-The very first time you run it you will see the following output:
+While walking the path to Ruby enlightenment you will need to run the koan and
+see it fail (<em>red</em>), make the test pass (<em>green</em>), 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 (<em>refactor</em>).
+
+The very first time you run the koans you will see the following output:
[ ruby_koans ] $ rake
(in /Users/person/dev/ruby_koans)
@@ -98,30 +100,57 @@ The very first time you run it you will see the following output:
mountains are merely mountains
your path thus far [X_________________________________________________] 0/280
-You have come to your first stage. If you notice it is telling you where to look for
+You have come to your first stage. 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:38:in `each_with_index'
path_to_enlightenment.rb:38
-We then open up the about_asserts.rb file and look at the first test:
+Open 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
+Change the +false+ to +true+ and re-run the test. 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
+method, it will either ensure it is +true+ and continue on, or fail if
the statement is +false+.
+=== Running the Koans automatically
+
+<em>This section is optional.</em>
+
+Normally the path to enlightenment looks like this:
+
+ cd ruby_koans
+ rake
+ # edit
+ rake
+ # edit
+ rake
+ # etc
+
+If you prefer, you can keep the koans running in the background so that after you
+make a change in your editor, the koans will immediately run again. This will
+hopefully keep your focus on learning Ruby instead of on the command line.
+
+Install the Ruby gem (library) called +watchr+ and then ask it to
+"watch" the koans for changes:
+
+ cd ruby_koans
+ rake
+ # decide to run rake automatically from now on as you edit
+ gem install watchr
+ watchr ./koans/koans.watchr
+
== Inspiration
A special thanks to Mike Clark and Ara Howard for inspiring this
@@ -148,8 +177,8 @@ Brian Marick's fantastic guide for beginners Everyday Scripting with Ruby ::
= Other stuff
-Author :: Jim Weirich <jim@weirichhouse.org>
-Author :: Joe O'Brien <joe@edgecase.com>
+Author :: Jim Weirich <jim@neo.org>
+Author :: Joe O'Brien <joe@objo.com>
Issue Tracker :: http://www.pivotaltracker.com/projects/48111
Requires :: Ruby 1.8.x or later and Rake (any recent version)
View
41 Rakefile
@@ -2,25 +2,17 @@
# -*- ruby -*-
require 'rake/clean'
-begin
- require 'rdoc/task'
-rescue LoadError => ex
- # No rdoc task availble.
-end
SRC_DIR = 'src'
PROB_DIR = 'koans'
-DIST_DIR = 'dist'
+DOWNLOAD_DIR = 'download'
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"
+ZIP_FILE = "#{DOWNLOAD_DIR}/rubykoans.zip"
CLEAN.include("**/*.rbc")
-CLOBBER.include(DIST_DIR)
module Koans
extend Rake::DSL if defined?(Rake::DSL)
@@ -40,7 +32,7 @@ module Koans
end
def Koans.make_koan_file(infile, outfile)
- if infile =~ /edgecase/
+ if infile =~ /neo/
cp infile, outfile
else
open(infile) do |ins|
@@ -90,30 +82,25 @@ task :walk_the_path do
ruby 'path_to_enlightenment.rb'
end
-if defined?(Rake::RDocTask)
- Rake::RDocTask.new do |rd|
- rd.main = "README.rdoc"
- rd.rdoc_files.include("README.rdoc", "${PROB_DIR}/*.rb")
- end
-end
-
-directory DIST_DIR
+directory DOWNLOAD_DIR
directory PROB_DIR
-file ZIP_FILE => KOAN_FILES + [DIST_DIR] do
- sh "zip #{ZIP_FILE} #{PROB_DIR}/*"
+desc "(re)Build zip file"
+task :zip => [:clobber_zip, :package]
+
+task :clobber_zip do
+ rm ZIP_FILE
end
-file TAR_FILE => KOAN_FILES + [DIST_DIR] do
- sh "tar zcvf #{TAR_FILE} #{PROB_DIR}"
+file ZIP_FILE => KOAN_FILES + [DOWNLOAD_DIR] do
+ sh "zip #{ZIP_FILE} #{PROB_DIR}/*"
end
desc "Create packaged files for distribution"
-task :package => [TAR_FILE, ZIP_FILE]
+task :package => [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"
+task :upload => [ZIP_FILE] do
sh "scp #{ZIP_FILE} linode:sites/onestepback.org/download"
end
@@ -138,7 +125,7 @@ end
task :run do
puts 'koans'
- Dir.chdir("${SRC_DIR}") do
+ Dir.chdir("#{SRC_DIR}") do
puts "in #{Dir.pwd}"
sh "ruby path_to_enlightenment.rb"
end
View
BIN download/rubykoans.zip
Binary file not shown.
View
4 src/about_array_assignment.rb
@@ -1,6 +1,6 @@
-require File.expand_path(File.dirname(__FILE__) + '/edgecase')
+require File.expand_path(File.dirname(__FILE__) + '/neo')
-class AboutArrayAssignment < EdgeCase::Koan
+class AboutArrayAssignment < Neo::Koan
def test_non_parallel_assignment
names = ["John", "Smith"]
assert_equal __(["John", "Smith"]), names
View
4 src/about_arrays.rb
@@ -1,6 +1,6 @@
-require File.expand_path(File.dirname(__FILE__) + '/edgecase')
+require File.expand_path(File.dirname(__FILE__) + '/neo')
-class AboutArrays < EdgeCase::Koan
+class AboutArrays < Neo::Koan
def test_creating_arrays
empty_array = Array.new
assert_equal __(Array), empty_array.class
View
4 src/about_asserts.rb
@@ -1,9 +1,9 @@
#!/usr/bin/env ruby
# -*- ruby -*-
-require File.expand_path(File.dirname(__FILE__) + '/edgecase')
+require File.expand_path(File.dirname(__FILE__) + '/neo')
-class AboutAsserts < EdgeCase::Koan
+class AboutAsserts < Neo::Koan
# We shall contemplate truth by testing reality, via asserts.
def test_assert_truth
View
8 src/about_blocks.rb
@@ -1,6 +1,6 @@
-require File.expand_path(File.dirname(__FILE__) + '/edgecase')
+require File.expand_path(File.dirname(__FILE__) + '/neo')
-class AboutBlocks < EdgeCase::Koan
+class AboutBlocks < Neo::Koan
def method_with_block
result = yield
result
@@ -23,7 +23,7 @@ def method_with_block_arguments
end
def test_blocks_can_take_arguments
- result = method_with_block_arguments do |argument|
+ method_with_block_arguments do |argument|
assert_equal __("Jim"), argument
end
end
@@ -70,7 +70,7 @@ def test_blocks_can_be_assigned_to_variables_and_called_explicitly
add_one = lambda { |n| n + 1 }
assert_equal __(11), add_one.call(10)
- # Alternative calling sequence
+ # Alternative calling syntax
assert_equal __(11), add_one[10]
end
View
4 src/about_class_methods.rb
@@ -1,6 +1,6 @@
-require File.expand_path(File.dirname(__FILE__) + '/edgecase')
+require File.expand_path(File.dirname(__FILE__) + '/neo')
-class AboutClassMethods < EdgeCase::Koan
+class AboutClassMethods < Neo::Koan
class Dog
end
View
6 src/about_classes.rb
@@ -1,6 +1,6 @@
-require File.expand_path(File.dirname(__FILE__) + '/edgecase')
+require File.expand_path(File.dirname(__FILE__) + '/neo')
-class AboutClasses < EdgeCase::Koan
+class AboutClasses < Neo::Koan
class Dog
end
@@ -147,7 +147,7 @@ def get_self
end
def to_s
- __(@name)
+ @name
end
def inspect
View
4 src/about_constants.rb
@@ -1,8 +1,8 @@
-require File.expand_path(File.dirname(__FILE__) + '/edgecase')
+require File.expand_path(File.dirname(__FILE__) + '/neo')
C = "top level"
-class AboutConstants < EdgeCase::Koan
+class AboutConstants < Neo::Koan
C = "nested"
View
12 src/about_control_statements.rb
@@ -1,6 +1,6 @@
-require File.expand_path(File.dirname(__FILE__) + '/edgecase')
+require File.expand_path(File.dirname(__FILE__) + '/neo')
-class AboutControlStatements < EdgeCase::Koan
+class AboutControlStatements < Neo::Koan
def test_if_then_else_statements
if true
@@ -131,4 +131,12 @@ def test_for_statement
assert_equal [__("FISH"), __("AND"), __("CHIPS")], result
end
+ def test_times_statement
+ sum = 0
+ 10.times do
+ sum += 1
+ end
+ assert_equal __(10), sum
+ end
+
end
View
6 src/about_dice_project.rb
@@ -1,4 +1,4 @@
-require File.expand_path(File.dirname(__FILE__) + '/edgecase')
+require File.expand_path(File.dirname(__FILE__) + '/neo')
# Implement a DiceSet Class here:
#
@@ -15,7 +15,7 @@ def roll(n)
end
#++
-class AboutDiceProject < EdgeCase::Koan
+class AboutDiceProject < Neo::Koan
def test_can_create_a_dice_set
dice = DiceSet.new
assert_not_nil dice
@@ -56,7 +56,7 @@ def test_dice_values_should_change_between_rolls
#
# If the rolls are random, then it is possible (although not
# likely) that two consecutive rolls are equal. What would be a
- # better way to test this.
+ # better way to test this?
end
def test_you_can_roll_different_numbers_of_dice
View
6 src/about_exceptions.rb
@@ -1,6 +1,6 @@
-require File.expand_path(File.dirname(__FILE__) + '/edgecase')
+require File.expand_path(File.dirname(__FILE__) + '/neo')
-class AboutExceptions < EdgeCase::Koan
+class AboutExceptions < Neo::Koan
class MySpecialError < RuntimeError
end
@@ -48,7 +48,7 @@ def test_ensure_clause
result = nil
begin
fail "Oops"
- rescue StandardError => ex
+ rescue StandardError
# no code here
ensure
result = :always_run
View
6 src/about_hashes.rb
@@ -1,10 +1,10 @@
-require File.expand_path(File.dirname(__FILE__) + '/edgecase')
+require File.expand_path(File.dirname(__FILE__) + '/neo')
-class AboutHashes < EdgeCase::Koan
+class AboutHashes < Neo::Koan
def test_creating_hashes
empty_hash = Hash.new
assert_equal __(Hash), empty_hash.class
- assert_equal({}, empty_hash) # __
+ assert_equal(__({}), empty_hash)
assert_equal __(0), empty_hash.size
end
View
4 src/about_inheritance.rb
@@ -1,6 +1,6 @@
-require File.expand_path(File.dirname(__FILE__) + '/edgecase')
+require File.expand_path(File.dirname(__FILE__) + '/neo')
-class AboutInheritance < EdgeCase::Koan
+class AboutInheritance < Neo::Koan
class Dog
attr_reader :name
View
24 src/about_iteration.rb
@@ -1,20 +1,20 @@
-require File.expand_path(File.dirname(__FILE__) + '/edgecase')
+require File.expand_path(File.dirname(__FILE__) + '/neo')
-class AboutIteration < EdgeCase::Koan
+class AboutIteration < Neo::Koan
# -- An Aside ------------------------------------------------------
- # Ruby 1.8 stores names as strings. Ruby 1.9 stores names as
- # symbols. So we use a version dependent method "as_name" to convert
- # to the right format in the koans. We will use "as_name" whenever
- # comparing to lists of methods.
+ # Ruby 1.8 stores names as strings. Ruby 1.9 and later stores names
+ # as symbols. So we use a version dependent method "as_name" to
+ # convert to the right format in the koans. We will use "as_name"
+ # whenever comparing to lists of methods.
in_ruby_version("1.8") do
def as_name(name)
name.to_s
end
end
- in_ruby_version("1.9") do
+ in_ruby_version("1.9", "2") do
def as_name(name)
name.to_sym
end
@@ -39,19 +39,17 @@ def test_iterating_with_each
def test_each_can_use_curly_brace_blocks_too
array = [1, 2, 3]
sum = 0
- array.each { |item|
- sum += item
- }
+ array.each { |item| sum += item }
assert_equal __(6), sum
end
def test_break_works_with_each_style_iterations
array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
sum = 0
- array.each { |item|
+ array.each do |item|
break if item > 3
sum += item
- }
+ end
assert_equal __(6), sum
end
@@ -86,7 +84,7 @@ def test_inject_will_blow_your_mind
result = [2, 3, 4].inject(0) { |sum, item| sum + item }
assert_equal __(9), result
- result2 = [2, 3, 4].inject(1) { |sum, item| sum * item }
+ result2 = [2, 3, 4].inject(1) { |product, item| product * item }
assert_equal __(24), result2
# Extra Credit:
View
4 src/about_java_interop.rb
@@ -1,4 +1,4 @@
-require File.expand_path(File.dirname(__FILE__) + '/edgecase')
+require File.expand_path(File.dirname(__FILE__) + '/neo')
include Java
@@ -11,7 +11,7 @@
# * Calling custom java class
# * Calling Ruby from java???
-class AboutJavaInterop < EdgeCase::Koan
+class AboutJavaInterop < Neo::Koan
def test_using_a_java_library_class
java_array = java.util.ArrayList.new
assert_equal __(Java::JavaUtil::ArrayList), java_array.class
View
31 src/about_keyword_arguments.rb
@@ -0,0 +1,31 @@
+require File.expand_path(File.dirname(__FILE__) + '/neo')
+
+class AboutKeywordArguments < Neo::Koan
+
+ def method_with_keyword_arguments(one: 1, two: 'two')
+ [one, two]
+ end
+
+ def test_keyword_arguments
+ assert_equal __(Array), method_with_keyword_arguments.class
+ assert_equal __([1, 'two']), method_with_keyword_arguments
+ assert_equal __(['one', 'two']), method_with_keyword_arguments(one: 'one')
+ assert_equal __([1, 2]), method_with_keyword_arguments(two: 2)
+ end
+
+ def method_with_keyword_arguments_with_mandatory_argument(one, two: 2, three: 3)
+ [one, two, three]
+ end
+
+ def test_keyword_arguments_with_wrong_number_of_arguments
+ exception = assert_raise (___(ArgumentError)) do
+ method_with_keyword_arguments_with_mandatory_argument
+ end
+ assert_match(/#{__("wrong number of arguments")}/, exception.message)
+ end
+
+ # THINK ABOUT IT:
+ #
+ # Keyword arguments always have a default value, making them optional to the caller
+
+end
View
23 src/about_message_passing.rb
@@ -1,6 +1,6 @@
-require File.expand_path(File.dirname(__FILE__) + '/edgecase')
+require File.expand_path(File.dirname(__FILE__) + '/neo')
-class AboutMessagePassing < EdgeCase::Koan
+class AboutMessagePassing < Neo::Koan
class MessageCatcher
def caught?
@@ -63,6 +63,13 @@ def test_sending_a_message_with_arguments
assert_equal __([3, 4, nil, 6]), mc.send(:add_a_payload, 3, 4, nil, 6)
end
+ # NOTE:
+ #
+ # Both obj.msg and obj.send(:msg) sends the message named :msg to
+ # the object. We use "send" when the name of the message can vary
+ # dynamically (e.g. calculated at run time), but by far the most
+ # common way of sending a message is just to say: obj.msg.
+
# ------------------------------------------------------------------
class TypicalObject
@@ -93,11 +100,11 @@ def test_calling_method_missing_causes_the_no_method_error
# NOTE:
#
# In Ruby 1.8 the method_missing method is public and can be
- # called as shown above. However, in Ruby 1.9 the method_missing
- # method is private. We explicitly made it public in the testing
- # framework so this example works in both versions of Ruby. Just
- # keep in mind you can't call method_missing like that in Ruby
- # 1.9. normally.
+ # called as shown above. However, in Ruby 1.9 (and later versions)
+ # the method_missing method is private. We explicitly made it
+ # public in the testing framework so this example works in both
+ # versions of Ruby. Just keep in mind you can't call
+ # method_missing like that after Ruby 1.9 normally.
#
# Thanks. We now return you to your regularly scheduled Ruby
# Koans.
@@ -122,7 +129,7 @@ def test_all_messages_are_caught
def test_catching_messages_makes_respond_to_lie
catcher = AllMessageCatcher.new
- assert_nothing_raised(NoMethodError) do # __
+ assert_nothing_raised do # __
catcher.any_method
end
assert_equal __(false), catcher.respond_to?(:any_method)
View
8 src/about_methods.rb
@@ -1,10 +1,10 @@
-require File.expand_path(File.dirname(__FILE__) + '/edgecase')
+require File.expand_path(File.dirname(__FILE__) + '/neo')
def my_global_method(a,b)
a + b
end
-class AboutMethods < EdgeCase::Koan
+class AboutMethods < Neo::Koan
def test_calling_global_methods
assert_equal __(5), my_global_method(2,3)
@@ -37,7 +37,7 @@ def test_sometimes_missing_parentheses_are_ambiguous
#
end
- # NOTE: wrong number of argument is not a SYNTAX error, but a
+ # NOTE: wrong number of arguments is not a SYNTAX error, but a
# runtime error.
def test_calling_global_methods_with_wrong_number_of_arguments
exception = assert_raise(___(ArgumentError)) do
@@ -130,7 +130,7 @@ def test_calling_private_methods_with_an_explicit_receiver
exception = assert_raise(___(NoMethodError)) do
self.my_private_method
end
- assert_match /#{__("private method `my_private_method' called ")}/, exception.message
+ assert_match /#{__("method `my_private_method'")}/, exception.message
end
# ------------------------------------------------------------------
View
6 src/about_modules.rb
@@ -1,6 +1,6 @@
-require File.expand_path(File.dirname(__FILE__) + '/edgecase')
+require File.expand_path(File.dirname(__FILE__) + '/neo')
-class AboutModules < EdgeCase::Koan
+class AboutModules < Neo::Koan
module Nameable
def set_name(new_name)
@name = new_name
@@ -44,7 +44,7 @@ def test_normal_methods_are_available_in_the_object
def test_module_methods_are_also_available_in_the_object
fido = Dog.new
- assert_nothing_raised(Exception) do # __
+ assert_nothing_raised do # __
fido.set_name("Rover")
end
end
View
4 src/about_nil.rb
@@ -1,6 +1,6 @@
-require File.expand_path(File.dirname(__FILE__) + '/edgecase')
+require File.expand_path(File.dirname(__FILE__) + '/neo')
-class AboutNil < EdgeCase::Koan
+class AboutNil < Neo::Koan
def test_nil_is_an_object
assert_equal __(true), nil.is_a?(Object), "Unlike NULL in other languages"
end
View
10 src/about_objects.rb
@@ -1,6 +1,6 @@
-require File.expand_path(File.dirname(__FILE__) + '/edgecase')
+require File.expand_path(File.dirname(__FILE__) + '/neo')
-class AboutObjects < EdgeCase::Koan
+class AboutObjects < Neo::Koan
def test_everything_is_an_object
assert_equal __(true), 1.is_a?(Object)
assert_equal __(true), 1.5.is_a?(Object)
@@ -30,12 +30,6 @@ def test_every_object_has_different_id
assert_equal __(true), obj.object_id != another_obj.object_id
end
- def test_some_system_objects_always_have_the_same_id
- assert_equal __(0), false.object_id
- assert_equal __(2), true.object_id
- assert_equal __(4), nil.object_id
- end
-
def test_small_integers_have_fixed_ids
assert_equal __(1), 0.object_id
assert_equal __(3), 1.object_id
View
4 src/about_open_classes.rb
@@ -1,6 +1,6 @@
-require File.expand_path(File.dirname(__FILE__) + '/edgecase')
+require File.expand_path(File.dirname(__FILE__) + '/neo')
-class AboutOpenClasses < EdgeCase::Koan
+class AboutOpenClasses < Neo::Koan
class Dog
def bark
"WOOF"
View
6 src/about_proxy_object_project.rb
@@ -1,4 +1,4 @@
-require File.expand_path(File.dirname(__FILE__) + '/edgecase')
+require File.expand_path(File.dirname(__FILE__) + '/neo')
# Project: Create a Proxy Class
#
@@ -42,7 +42,7 @@ def number_of_times_called(method)
# The proxy object should pass the following Koan:
#
-class AboutProxyObjectProject < EdgeCase::Koan
+class AboutProxyObjectProject < Neo::Koan
def test_proxy_method_returns_wrapped_object
# NOTE: The Television class is defined below
tv = Proxy.new(Television.new)
@@ -135,7 +135,7 @@ def on?
end
# Tests for the Television class. All of theses tests should pass.
-class TelevisionTest < EdgeCase::Koan
+class TelevisionTest < Neo::Koan
def test_it_turns_on
tv = Television.new
View
4 src/about_regular_expressions.rb
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
-require File.expand_path(File.dirname(__FILE__) + '/edgecase')
+require File.expand_path(File.dirname(__FILE__) + '/neo')
-class AboutRegularExpressions < EdgeCase::Koan
+class AboutRegularExpressions < Neo::Koan
def test_a_pattern_is_a_regular_expression
assert_equal __(Regexp), /pattern/.class
end
View
10 src/about_sandwich_code.rb
@@ -1,11 +1,11 @@
-require File.expand_path(File.dirname(__FILE__) + '/edgecase')
+require File.expand_path(File.dirname(__FILE__) + '/neo')
-class AboutSandwichCode < EdgeCase::Koan
+class AboutSandwichCode < Neo::Koan
def count_lines(file_name)
file = open(file_name)
count = 0
- while line = file.gets
+ while file.gets
count += 1
end
count
@@ -66,7 +66,7 @@ def file_sandwich(file_name)
def count_lines2(file_name)
file_sandwich(file_name) do |file|
count = 0
- while line = file.gets
+ while file.gets
count += 1
end
count
@@ -99,7 +99,7 @@ def test_finding_lines2
def count_lines3(file_name)
open(file_name) do |file|
count = 0
- while line = file.gets
+ while file.gets
count += 1
end
count
View
6 src/about_scope.rb
@@ -1,6 +1,6 @@
-require File.expand_path(File.dirname(__FILE__) + '/edgecase')
+require File.expand_path(File.dirname(__FILE__) + '/neo')
-class AboutScope < EdgeCase::Koan
+class AboutScope < Neo::Koan
module Jims
class Dog
def identify
@@ -19,7 +19,7 @@ def identify
def test_dog_is_not_available_in_the_current_scope
assert_raise(___(NameError)) do
- fido = Dog.new
+ Dog.new
end
end
View
7 src/about_scoring_project.rb
@@ -1,4 +1,4 @@
-require File.expand_path(File.dirname(__FILE__) + '/edgecase')
+require File.expand_path(File.dirname(__FILE__) + '/neo')
# Greed is a dice game where you roll up to five dice to accumulate
# points. The following "score" function will be used to calculate the
@@ -54,7 +54,7 @@ def score(dice)
#++
end
-class AboutScoringProject < EdgeCase::Koan
+class AboutScoringProject < Neo::Koan
def test_score_of_an_empty_list_is_zero
assert_equal 0, score([])
end
@@ -90,6 +90,9 @@ def test_score_of_other_triples_is_100x
def test_score_of_mixed_is_sum
assert_equal 250, score([2,5,2,2,3])
assert_equal 550, score([5,5,5,5])
+ assert_equal 1100, score([1,1,1,1])
+ assert_equal 1200, score([1,1,1,1,1])
+ assert_equal 1150, score([1,1,1,5,1])
end
end
View
12 src/about_strings.rb
@@ -1,6 +1,6 @@
-require File.expand_path(File.dirname(__FILE__) + '/edgecase')
+require File.expand_path(File.dirname(__FILE__) + '/neo')
-class AboutStrings < EdgeCase::Koan
+class AboutStrings < Neo::Koan
def test_double_quoted_strings_are_strings
string = "Hello, World"
assert_equal __(true), string.is_a?(String)
@@ -42,6 +42,7 @@ def test_flexible_quotes_can_handle_multiple_lines
}
assert_equal __(54), long_string.length
assert_equal __(3), long_string.lines.count
+ assert_equal __("\n"), long_string[0,1]
end
def test_here_documents_can_also_handle_multiple_lines
@@ -51,6 +52,7 @@ def test_here_documents_can_also_handle_multiple_lines
EOS
assert_equal __(53), long_string.length
assert_equal __(2), long_string.lines.count
+ assert_equal __("I"), long_string[0,1]
end
def test_plus_will_concatenate_two_strings
@@ -149,16 +151,16 @@ def test_you_can_get_a_single_character_from_a_string
end
in_ruby_version("1.8") do
- def test_in_ruby_1_8_single_characters_are_represented_by_integers
+ def test_in_older_ruby_single_characters_are_represented_by_integers
assert_equal __(97, 'a'), ?a
assert_equal __(true, false), ?a == 97
assert_equal __(true), ?b == (?a + 1)
end
end
- in_ruby_version("1.9") do
- def test_in_ruby_1_9_single_characters_are_represented_by_strings
+ in_ruby_version("1.9", "2") do
+ def test_in_modern_ruby_single_characters_are_represented_by_strings
assert_equal __('a'), ?a
assert_equal __(false), ?a == 97
end
View
8 src/about_symbols.rb
@@ -1,6 +1,6 @@
-require File.expand_path(File.dirname(__FILE__) + '/edgecase')
+require File.expand_path(File.dirname(__FILE__) + '/neo')
-class AboutSymbols < EdgeCase::Koan
+class AboutSymbols < Neo::Koan
def test_symbols_are_symbols
symbol = :ruby
assert_equal __(true), symbol.is_a?(Symbol)
@@ -50,14 +50,14 @@ def test_symbols_can_be_made_from_strings
def test_symbols_with_spaces_can_be_built
symbol = :"cats and dogs"
- assert_equal symbol, __("cats and dogs").to_sym
+ assert_equal __("cats and dogs").to_sym, symbol
end
def test_symbols_with_interpolation_can_be_built
value = "and"
symbol = :"cats #{value} dogs"
- assert_equal symbol, __("cats and dogs").to_sym
+ assert_equal __("cats and dogs").to_sym, symbol
end
def test_to_s_is_called_on_interpolated_symbols
View
4 src/about_to_str.rb
@@ -1,6 +1,6 @@
-require File.expand_path(File.dirname(__FILE__) + '/edgecase')
+require File.expand_path(File.dirname(__FILE__) + '/neo')
-class AboutToStr < EdgeCase::Koan
+class AboutToStr < Neo::Koan
class CanNotBeTreatedAsString
def to_s
View
7 src/about_triangle_project.rb
@@ -1,9 +1,9 @@
-require File.expand_path(File.dirname(__FILE__) + '/edgecase')
+require File.expand_path(File.dirname(__FILE__) + '/neo')
# You need to write the triangle method in the file 'triangle.rb'
-require 'triangle.rb'
+require './triangle'
-class AboutTriangleProject < EdgeCase::Koan
+class AboutTriangleProject < Neo::Koan
def test_equilateral_triangles_have_equal_sides
assert_equal :equilateral, triangle(2, 2, 2)
assert_equal :equilateral, triangle(10, 10, 10)
@@ -22,4 +22,3 @@ def test_scalene_triangles_have_no_equal_sides
assert_equal :scalene, triangle(5, 4, 2)
end
end
-
View
9 src/about_triangle_project_2.rb
@@ -1,9 +1,9 @@
-require File.expand_path(File.dirname(__FILE__) + '/edgecase')
+require File.expand_path(File.dirname(__FILE__) + '/neo')
# You need to write the triangle method in the file 'triangle.rb'
-require 'triangle.rb'
+require './triangle.rb'
-class AboutTriangleProject2 < EdgeCase::Koan
+class AboutTriangleProject2 < Neo::Koan
# The first assignment did not talk about how to handle errors.
# Let's handle that part now.
def test_illegal_triangles_throw_exceptions
@@ -12,6 +12,5 @@ def test_illegal_triangles_throw_exceptions
assert_raise(TriangleError) do triangle(1, 1, 3) end
assert_raise(TriangleError) do triangle(2, 4, 2) end
# HINT: for tips, see http://stackoverflow.com/questions/3834203/ruby-koan-151-raising-exceptions
- end
+ end
end
-
View
4 src/about_true_and_false.rb
@@ -1,6 +1,6 @@
-require File.expand_path(File.dirname(__FILE__) + '/edgecase')
+require File.expand_path(File.dirname(__FILE__) + '/neo')
-class AboutTrueAndFalse < EdgeCase::Koan
+class AboutTrueAndFalse < Neo::Koan
def truth_value(condition)
if condition
:true_stuff
View
47 src/array_test.rb
@@ -1,47 +0,0 @@
-require 'test_helper'
-
-class ArrayTest < EdgeCase::TestCase
-
- def test_basic_arrays
- food = [:peanut, :button, :and, :jelly]
- assert_equal __, food[0]
- assert_equal __, food.size
- end
-
- def test_array_access
- food = [:peanut, :button, :and, :jelly]
- assert_equal __, food.first
- assert_equal __, food.last
- assert_equal __, food[0]
- assert_equal __, food[2]
- assert_equal __, food[(food.size() - 1)]
- end
-
- def test_arrays_with_other_objects
- food = [:peanut, :button, :and, :jelly, 1, nil]
- assert_equal __, food.size
- assert_equal __, food.last
- assert_equal __, food[5]
- end
-
- def test_adding_to_an_array_with_shovel_shovel
- food = [:peanut, :button, :and, :jelly]
- food << 'sandwich'
- assert_equal __, food.size
- assert_equal __, food.first
- end
-
- def test_adding_to_an_array_with_push
- food = [:peanut, :button, :and, :jelly]
- food.push('sandwich')
- assert_equal __, food.last
- end
-
- def test_adding_to_an_array_with_unshift
- food = [:peanut, :button, :and, :jelly]
- food.unshift('a')
- assert_equal __, food.first
- end
-
-end
-
View
1 src/code_mash.rb
@@ -1 +0,0 @@
-require File.expand_path(File.dirname(__FILE__) + '/edgecase')
View
11 src/first_test.rb
@@ -1,11 +0,0 @@
-require 'test/unit'
-
-class TestSomething < Test::Unit::TestCase
- def test_assert
- assert true
- assert_equal 1, 1
- assert_equal 1, 1.0
- end
-end
-
-
View
115 src/edgecase.rb → src/neo.rb
@@ -1,7 +1,6 @@
#!/usr/bin/env ruby
# -*- ruby -*-
-require 'test/unit/assertions'
begin
require 'win32console'
rescue LoadError
@@ -65,7 +64,7 @@ def ____(method=nil)
end
end
- in_ruby_version("1.9") do
+ in_ruby_version("1.9", "2") do
public :method_missing
end
end
@@ -83,7 +82,7 @@ def side_padding(width)
end
end
-module EdgeCase
+module Neo
class << self
def simple_output
ENV['SIMPLE_KOAN_OUTPUT'] == 'true'
@@ -139,21 +138,69 @@ def using_win32console
end
end
- class Sensei
- attr_reader :failure, :failed_test, :pass_count
+ module Assertions
+ FailedAssertionError = Class.new(StandardError)
- in_ruby_version("1.8") do
- AssertionError = Test::Unit::AssertionFailedError
+ def flunk(msg)
+ raise FailedAssertionError, msg
end
- in_ruby_version("1.9") do
- if defined?(MiniTest)
- AssertionError = MiniTest::Assertion
- else
- AssertionError = Test::Unit::AssertionFailedError
+ def assert(condition, msg=nil)
+ msg ||= "Failed assertion."
+ flunk(msg) unless condition
+ true
+ end
+
+ def assert_equal(expected, actual, msg=nil)
+ msg ||= "Expected #{expected.inspect} to equal #{actual.inspect}"
+ assert(expected == actual, msg)
+ end
+
+ def assert_not_equal(expected, actual, msg=nil)
+ msg ||= "Expected #{expected.inspect} to not equal #{actual.inspect}"
+ assert(expected != actual, msg)
+ end
+
+ def assert_nil(actual, msg=nil)
+ msg ||= "Expected #{actual.inspect} to be nil"
+ assert(nil == actual, msg)
+ end
+
+ def assert_not_nil(actual, msg=nil)
+ msg ||= "Expected #{actual.inspect} to not be nil"
+ assert(nil != actual, msg)
+ end
+
+ def assert_match(pattern, actual, msg=nil)
+ msg ||= "Expected #{actual.inspect} to match #{pattern.inspect}"
+ assert pattern =~ actual, msg
+ end
+
+ def assert_raise(exception)
+ begin
+ yield
+ rescue Exception => ex
+ expected = ex.is_a?(exception)
+ assert(expected, "Exception #{exception.inspect} expected, but #{ex.inspect} was raised")
+ return ex
end
+ flunk "Exception #{exception.inspect} expected, but nothing raised"
end
+ def assert_nothing_raised
+ begin
+ yield
+ rescue Exception => ex
+ flunk "Expected nothing to be raised, but exception #{exception.inspect} was raised"
+ end
+ end
+ end
+
+ class Sensei
+ attr_reader :failure, :failed_test, :pass_count
+
+ FailedAssertionError = Assertions::FailedAssertionError
+
def initialize
@pass_count = 0
@failure = nil
@@ -195,7 +242,7 @@ def observe(step)
@failure = step.failure
add_progress(@pass_count)
@observations << Color.red("#{step.koan_file}##{step.name} has damaged your karma.")
- throw :edgecase_exit
+ throw :neo_exit
end
end
@@ -204,7 +251,7 @@ def failed?
end
def assert_failed?
- failure.is_a?(AssertionError)
+ failure.is_a?(FailedAssertionError)
end
def instruct
@@ -221,7 +268,7 @@ def instruct
def show_progress
bar_width = 50
- total_tests = EdgeCase::Koan.total_tests
+ total_tests = Neo::Koan.total_tests
scale = bar_width.to_f/total_tests
print Color.green("your path thus far [")
happy_steps = (pass_count*scale).to_i
@@ -237,7 +284,7 @@ def show_progress
end
def end_screen
- if EdgeCase.simple_output
+ if Neo.simple_output
boring_end_screen
else
artistic_end_screen
@@ -268,13 +315,13 @@ def artistic_end_screen
,:::::::::::, ::::::::::::,
:::::::::::, ,::::::::::::
::::::::::::: ,::::::::::::
-:::::::::::: Ruby Koans ::::::::::::,
-::::::::::::#{ ruby_version },::::::::::::,
-:::::::::::, , ::::::::::::
-,:::::::::::::, brought to you by ,,::::::::::::,
+:::::::::::: Ruby Koans ::::::::::::
+::::::::::::#{ ruby_version },::::::::::::
+:::::::::::, , :::::::::::
+,:::::::::::::, brought to you by ,,::::::::::::
:::::::::::::: ,::::::::::::
::::::::::::::, ,:::::::::::::
- ::::::::::::, EdgeCase Software Artisans , ::::::::::::
+ ::::::::::::, Neo Software Artisans , ::::::::::::
:,::::::::: :::: :::::::::::::
,::::::::::: ,: ,,:::::::::::::,
:::::::::::: ,::::::::::::::,
@@ -309,11 +356,7 @@ def guide_through_error
puts Color.red(indent(failure.message).join)
puts
puts "Please meditate on the following code:"
- if assert_failed?
- puts embolden_first_line_only(indent(find_interesting_lines(failure.backtrace)))
- else
- puts embolden_first_line_only(indent(failure.backtrace))
- end
+ puts embolden_first_line_only(indent(find_interesting_lines(failure.backtrace)))
puts
end
@@ -336,7 +379,7 @@ def indent(text)
def find_interesting_lines(backtrace)
backtrace.reject { |line|
- line =~ /test\/unit\/|edgecase\.rb|minitest/
+ line =~ /neo\.rb/
}
end
@@ -366,7 +409,7 @@ def a_zenlike_statement
end
class Koan
- include Test::Unit::Assertions
+ include Assertions
attr_reader :name, :failure, :koan_count, :step_count, :koan_file
@@ -396,19 +439,19 @@ def meditate
setup
begin
send(name)
- rescue StandardError, EdgeCase::Sensei::AssertionError => ex
+ rescue StandardError, Neo::Sensei::FailedAssertionError => ex
failed(ex)
ensure
begin
teardown
- rescue StandardError, EdgeCase::Sensei::AssertionError => ex
+ rescue StandardError, Neo::Sensei::FailedAssertionError => ex
failed(ex) if passed?
end
end
self
end
- # Class methods for the EdgeCase test suite.
+ # Class methods for the Neo test suite.
class << self
def inherited(subclass)
subclasses << subclass
@@ -465,17 +508,17 @@ def total_tests
class ThePath
def walk
- sensei = EdgeCase::Sensei.new
+ sensei = Neo::Sensei.new
each_step do |step|
sensei.observe(step.meditate)
end
sensei.instruct
end
def each_step
- catch(:edgecase_exit) {
+ catch(:neo_exit) {
step_count = 0
- EdgeCase::Koan.subclasses.each_with_index do |koan,koan_index|
+ Neo::Koan.subclasses.each_with_index do |koan,koan_index|
koan.testmethods.each do |method_name|
step = koan.new(method_name, koan.to_s, koan_index+1, step_count+=1)
yield step
@@ -487,6 +530,6 @@ def each_step
end
END {
- EdgeCase::Koan.command_line(ARGV)
- EdgeCase::ThePath.new.walk
+ Neo::Koan.command_line(ARGV)
+ Neo::ThePath.new.walk
}
View
3 src/path_to_enlightenment.rb
@@ -12,6 +12,9 @@
require 'about_symbols'
require 'about_regular_expressions'
require 'about_methods'
+in_ruby_version("2") do
+ require 'about_keyword_arguments'
+end
require 'about_constants'
require 'about_control_statements'
require 'about_true_and_false'
View
7 src/test_helper.rb
@@ -1,7 +0,0 @@
-require 'test/unit'
-
-def __
- "FILL ME IN"
-end
-
-EdgeCase = Test::Unit

0 comments on commit a1d3902

Please sign in to comment.
Something went wrong with that request. Please try again.