diff --git a/Rakefile b/Rakefile index de5d5be2b..e42cd8c4d 100644 --- a/Rakefile +++ b/Rakefile @@ -18,12 +18,17 @@ ZIP_FILE = "#{DIST_DIR}/rubykoans-#{today}.zip" 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 @@ -84,14 +89,6 @@ task :upload => [TAR_FILE, ZIP_FILE] do sh "scp #{ZIP_FILE} linode:sites/onestepback.org/download" end -desc "Check that the require files match the about_* files" -task :check do - about_files = Dir['src/about_*.rb'].size - about_requires = `grep require src/path_to_enlightenment.rb | wc -l`.to_i - puts "# of about files: #{about_files}" - puts "# of about requires: #{about_requires}" -end - desc "Generate the Koans from the source files from scratch." task :regen => [:clobber_koans, :gen] diff --git a/koans/about_arrays.rb b/koans/about_arrays.rb index 8dc4dfcbc..2ed3181ee 100644 --- a/koans/about_arrays.rb +++ b/koans/about_arrays.rb @@ -3,7 +3,7 @@ class AboutArrays < EdgeCase::Koan def test_creating_arrays empty_array = Array.new - assert_equal Array, empty_array.class + assert_equal __, empty_array.class assert_equal __, empty_array.size end diff --git a/src/about_arrays.rb b/src/about_arrays.rb index 7782ab9bd..35c951d64 100644 --- a/src/about_arrays.rb +++ b/src/about_arrays.rb @@ -3,16 +3,16 @@ class AboutArrays < EdgeCase::Koan def test_creating_arrays empty_array = Array.new - assert_equal Array, empty_array.class + assert_equal __(Array), empty_array.class assert_equal __(0), empty_array.size end def test_array_literals array = Array.new - assert_equal [], array + assert_equal [], array # __ array[0] = 1 - assert_equal [1], array + assert_equal [1], array # __ array[1] = 2 assert_equal [1, __(2)], array @@ -45,9 +45,9 @@ def test_slicing_arrays end def test_arrays_and_ranges - assert_equal Range, (1..5).class - assert_not_equal [1,2,3,4,5], (1..5) - assert_equal [1,2,3,4,5], (1..5).to_a + assert_equal __(Range), (1..5).class + assert_not_equal [1,2,3,4,5], (1..5) # __ + assert_equal __([1,2,3,4,5]), (1..5).to_a assert_equal __([1,2,3,4]), (1...5).to_a end diff --git a/src/about_classes.rb b/src/about_classes.rb index 30a2ca9ea..b8b67ea12 100644 --- a/src/about_classes.rb +++ b/src/about_classes.rb @@ -130,7 +130,7 @@ def test_different_objects_have_difference_instance_variables fido = Dog6.new("Fido") rover = Dog6.new("Rover") - assert_not_equal rover.name, fido.name + assert_equal __(true), rover.name != fido.name end # ------------------------------------------------------------------ @@ -164,12 +164,12 @@ def test_inside_a_method_self_refers_to_the_containing_object def test_to_s_provides_a_string_version_of_the_object fido = Dog7.new("Fido") - assert_equal "Fido", fido.to_s + assert_equal __("Fido"), fido.to_s end def test_to_s_is_used_in_string_interpolation fido = Dog7.new("Fido") - assert_equal "My dog is Fido", "My dog is #{fido}" + assert_equal __("My dog is Fido"), "My dog is #{fido}" end def test_inspect_provides_a_more_complete_string_version diff --git a/src/about_exceptions.rb b/src/about_exceptions.rb index 296055231..a2efbb3a7 100644 --- a/src/about_exceptions.rb +++ b/src/about_exceptions.rb @@ -22,12 +22,12 @@ def test_rescue_clause assert_equal __(:exception_handled), result - assert ex.is_a?(StandardError), "Failure message." - assert ex.is_a?(RuntimeError), "Failure message." + assert ex.is_a?(___(StandardError)), "Failure message." + assert ex.is_a?(___(RuntimeError)), "Failure message." - assert RuntimeError.ancestors.include?(StandardError), + assert RuntimeError.ancestors.include?(StandardError), # __ "RuntimeError is a subclass of StandardError" - + assert_equal __("Oops"), ex.message end @@ -58,7 +58,7 @@ def test_ensure_clause end # Sometimes, we must know about the unknown - def test_asserting_an_error_is_raised + def test_asserting_an_error_is_raised # __ # A do-end is a block, a topic to explore more later assert_raise(___(MySpecialError)) do raise MySpecialError.new("New instances can be raised directly.") diff --git a/src/about_hashes.rb b/src/about_hashes.rb index e74793fdf..49b393915 100644 --- a/src/about_hashes.rb +++ b/src/about_hashes.rb @@ -3,8 +3,8 @@ class AboutHashes < EdgeCase::Koan def test_creating_hashes empty_hash = Hash.new - assert_equal Hash, empty_hash.class - assert_equal({}, empty_hash) + assert_equal __(Hash), empty_hash.class + assert_equal({}, empty_hash) # __ assert_equal __(0), empty_hash.size end @@ -25,7 +25,7 @@ def test_changing_hashes hash[:one] = "eins" expected = { :one => __("eins"), :two => "dos" } - assert_equal expected, hash + assert_equal __(true), expected == hash # Bonus Question: Why was "expected" broken out into a variable # rather than used as a literal? @@ -35,7 +35,7 @@ def test_hash_is_unordered hash1 = { :one => "uno", :two => "dos" } hash2 = { :two => "dos", :one => "uno" } - assert_equal hash1, hash2 + assert_equal __(true), hash1 == hash2 end def test_hash_keys @@ -43,7 +43,7 @@ def test_hash_keys assert_equal __(2), hash.keys.size assert_equal __(true), hash.keys.include?(:one) assert_equal __(true), hash.keys.include?(:two) - assert_equal Array, hash.keys.class + assert_equal __(Array), hash.keys.class end def test_hash_values @@ -51,16 +51,16 @@ def test_hash_values assert_equal __(2), hash.values.size assert_equal __(true), hash.values.include?("uno") assert_equal __(true), hash.values.include?("dos") - assert_equal Array, hash.values.class + assert_equal __(Array), hash.values.class end def test_combining_hashes hash = { "jim" => 53, "amy" => 20, "dan" => 23 } new_hash = hash.merge({ "jim" => 54, "jenny" => 26 }) - assert_not_equal hash, new_hash - + assert_equal __(true), hash != new_hash + expected = { "jim" => __(54), "amy" => 20, "dan" => 23, "jenny" => __(26) } - assert_equal expected, new_hash + assert_equal __(true), expected == new_hash end end diff --git a/src/about_iteration.rb b/src/about_iteration.rb index 08071baf3..324a116f8 100644 --- a/src/about_iteration.rb +++ b/src/about_iteration.rb @@ -12,7 +12,7 @@ def test_iterating_with_each array.each do |item| sum += item end - assert_equal 6, sum + assert_equal __(6), sum end def test_each_can_use_curly_brace_blocks_too diff --git a/src/about_message_passing.rb b/src/about_message_passing.rb index c2b871501..37126f252 100644 --- a/src/about_message_passing.rb +++ b/src/about_message_passing.rb @@ -11,19 +11,19 @@ def caught? def test_methods_can_be_called_directly mc = MessageCatcher.new - assert mc.caught? + assert mc.caught? # __ end def test_methods_can_be_invoked_by_sending_the_message mc = MessageCatcher.new - assert mc.send(:caught?) + assert mc.send(:caught?) # __ end def test_methods_can_be_invoked_more_dynamically mc = MessageCatcher.new - assert mc.send("caught?") + assert mc.send("caught?") # __ assert mc.send("caught" + __("?") ) # What do you need to add to the first string? assert mc.send("CAUGHT?".____(:downcase) ) # What would you need to do to the string? end @@ -74,7 +74,7 @@ def test_sending_undefined_messages_to_a_typical_object_results_in_errors exception = assert_raise(___(NoMethodError)) do typical.foobar end - assert_match(/foobar/, exception.message) + assert_match(/foobar/, exception.message) # __ end def test_calling_method_missing_causes_the_no_method_error @@ -83,7 +83,7 @@ def test_calling_method_missing_causes_the_no_method_error exception = assert_raise(___(NoMethodError)) do typical.method_missing(:foobar) end - assert_match(/foobar/, exception.message) + assert_match(/foobar/, exception.message) # __ # THINK ABOUT IT: # @@ -122,7 +122,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(NoMethodError) do # __ catcher.any_method end assert_equal __(false), catcher.respond_to?(:any_method) diff --git a/src/about_methods.rb b/src/about_methods.rb index 42a06cfd7..78ed5b697 100644 --- a/src/about_methods.rb +++ b/src/about_methods.rb @@ -19,10 +19,10 @@ def test_calling_global_methods_without_parentheses # considered to be syntactically invalid). def test_sometimes_missing_parentheses_are_ambiguous #-- - eval "assert_equal 5, my_global_method(2, 3)" # REMOVE CHECK + eval "assert_equal 5, my_global_method(2, 3)" # REMOVE CHECK # __ if false #++ - eval "assert_equal 5, my_global_method 2, 3" # ENABLE CHECK + eval "assert_equal 5, my_global_method 2, 3" # ENABLE CHECK # __ #-- end #++ diff --git a/src/about_modules.rb b/src/about_modules.rb index 9e26d00aa..261746a58 100644 --- a/src/about_modules.rb +++ b/src/about_modules.rb @@ -44,8 +44,8 @@ def test_normal_methods_are_available_in_the_object def test_module_methods_are_also_availble_in_the_object fido = Dog.new - assert_nothing_raised(Exception) do - fido.set_name("Rover") + assert_nothing_raised(Exception) do # __ + fido.set_name("Rover") end end diff --git a/src/about_regular_expressions.rb b/src/about_regular_expressions.rb index 01e39f2cd..55b85f678 100644 --- a/src/about_regular_expressions.rb +++ b/src/about_regular_expressions.rb @@ -3,11 +3,11 @@ class AboutRegularExpressions < EdgeCase::Koan def test_a_pattern_is_a_regular_expression - assert_equal Regexp, /pattern/.class + assert_equal __(Regexp), /pattern/.class end def test_a_regexp_can_search_a_string_for_matching_content - assert_equal "match", "some matching content"[/match/] + assert_equal __("match"), "some matching content"[/match/] end def test_a_failed_match_returns_nil diff --git a/src/about_scope.rb b/src/about_scope.rb index 796ff029b..f6843c870 100644 --- a/src/about_scope.rb +++ b/src/about_scope.rb @@ -29,8 +29,8 @@ def test_you_can_reference_nested_classes_using_the_scope_operator assert_equal __(:jims_dog), fido.identify assert_equal __(:joes_dog), rover.identify - assert_not_equal fido.class, rover.class - assert_not_equal Jims::Dog, Joes::Dog + assert_equal __(true), fido.class != rover.class + assert_equal __(true), Jims::Dog != Joes::Dog end # ------------------------------------------------------------------ diff --git a/src/edgecase.rb b/src/edgecase.rb index 1a5b882f0..1d7ef5fb9 100644 --- a/src/edgecase.rb +++ b/src/edgecase.rb @@ -3,6 +3,10 @@ require 'test/unit/assertions' +# -------------------------------------------------------------------- +# Support code for the Ruby Koans. +# -------------------------------------------------------------------- + class FillMeInError < StandardError end @@ -16,6 +20,8 @@ def in_ruby_version(*versions) yield if versions.any? { |v| ruby_version?(v) } end +# Standard, generic replacement value. +# If value19 is given, it is used inplace of value for Ruby 1.9. def __(value="FILL ME IN", value19=:mu) if RUBY_VERSION < "1.9" value @@ -24,6 +30,7 @@ def __(value="FILL ME IN", value19=:mu) end end +# Numeric replacement value. def _n_(value=999999, value19=:mu) if RUBY_VERSION < "1.9" value @@ -32,10 +39,12 @@ def _n_(value=999999, value19=:mu) end end +# Error object replacement value. def ___(value=FillMeInError) value end +# Method name replacement. class Object def ____(method=nil) if method