Merge pull request #47 from ahmed80dz/master
authorMatt Darby <matt@protectedmethod.com>
Fri, 7 Oct 2011 23:24:23 +0000 (16:24 -0700)
committerMatt Darby <matt@protectedmethod.com>
Fri, 7 Oct 2011 23:24:23 +0000 (16:24 -0700)
colors in windows using win32console gem

koans/about_classes.rb
koans/about_control_statements.rb
koans/about_hashes.rb
koans/about_iteration.rb
koans/about_methods.rb
koans/about_regular_expressions.rb
src/about_modules.rb
src/about_nil.rb
src/about_symbols.rb

index fce0be0..3fce646 100644 (file)
@@ -126,7 +126,7 @@ class AboutClasses < EdgeCase::Koan
     # Why is this so?
   end
 
-  def test_different_objects_have_difference_instance_variables
+  def test_different_objects_have_different_instance_variables
     fido = Dog6.new("Fido")
     rover = Dog6.new("Rover")
 
index f243ac8..71a7af0 100644 (file)
@@ -59,12 +59,20 @@ class AboutControlStatements < EdgeCase::Koan
 
   def test_unless_statement
     result = :default_value
-    unless false
+    unless false    # same as saying 'if !false', which evaluates as 'if true'
       result = :false_value
     end
     assert_equal __, result
   end
 
+  def test_unless_statement_evaluate_true
+    result = :default_value
+    unless true    # same as saying 'if !true', which evaluates as 'if false'
+      result = :true_value
+    end
+    assert_equal __, result
+  end
+
   def test_unless_statement_modifier
     result = :default_value
     result = :false_value unless false
index 2324b04..511e3c1 100644 (file)
@@ -20,6 +20,18 @@ class AboutHashes < EdgeCase::Koan
     assert_equal __, hash[:doesnt_exist]
   end
 
+  def test_accessing_hashes_with_fetch
+    hash = { :one => "uno" }
+    assert_equal "uno", hash.fetch(:one)
+    assert_raise(___) do
+      hash.fetch(:doesnt_exist)
+    end
+
+    # THINK ABOUT IT:
+    #
+    # Why might you want to use #fetch instead of #[] when accessing hash keys?
+  end
+
   def test_changing_hashes
     hash = { :one => "uno", :two => "dos" }
     hash[:one] = "eins"
@@ -77,4 +89,28 @@ class AboutHashes < EdgeCase::Koan
     assert_equal __, hash2[:one]
     assert_equal __, hash2[:two]
   end
+
+  def test_default_value_is_the_same_object
+    hash = Hash.new([])
+
+    hash[:one] << "uno"
+    hash[:two] << "dos"
+
+    assert_equal __, hash[:one]
+    assert_equal __, hash[:two]
+    assert_equal __, hash[:three]
+
+    assert_equal __, hash[:one].object_id == hash[:two].object_id
+  end
+
+  def test_default_value_with_block
+    hash = Hash.new {|hash, key| hash[key] = [] }
+
+    hash[:one] << "uno"
+    hash[:two] << "dos"
+
+    assert_equal __, hash[:one]
+    assert_equal __, hash[:two]
+    assert_equal __, hash[:three]
+  end
 end
index b48c278..823128f 100644 (file)
@@ -1,9 +1,16 @@
 require File.expand_path(File.dirname(__FILE__) + '/edgecase')
 
 class AboutIteration < EdgeCase::Koan
+  in_ruby_version("1.8") do
+    def test_each_is_a_method_on_arrays
+      assert_equal __, [].methods.include?("each")
+    end
+  end
 
-  def test_each_is_a_method_on_arrays
-    assert_equal __, [].methods.include?(:each)
+  in_ruby_version("1.9") do
+    def test_each_is_a_method_on_arrays
+      assert_equal __, [].methods.include?(:each)
+    end
   end
 
   def test_iterating_with_each
index a4df4b8..5fd7725 100644 (file)
@@ -62,6 +62,7 @@ class AboutMethods < EdgeCase::Koan
   end
 
   def test_calling_with_variable_arguments
+    assert_equal __, method_with_var_args.class
     assert_equal __, method_with_var_args
     assert_equal __, method_with_var_args(:one)
     assert_equal __, method_with_var_args(:one, :two)
index 8344911..790bea3 100644 (file)
@@ -84,6 +84,8 @@ class AboutRegularExpressions < EdgeCase::Koan
   def test_shortcut_character_classes_are_negated_with_capitals
     assert_equal __, "the number is 42"[/\D+/]
     assert_equal __, "space: \t\n"[/\S+/]
+    # ... a programmer would most likely do
+    assert_equal __, "variable_1 = 42"[/[^a-zA-Z0-9_]+/]
     assert_equal __, "variable_1 = 42"[/\W+/]
   end
 
index 261746a..334b175 100644 (file)
@@ -42,7 +42,7 @@ class AboutModules < EdgeCase::Koan
     assert_equal __("WOOF"), fido.bark
   end
 
-  def test_module_methods_are_also_availble_in_the_object
+  def test_module_methods_are_also_available_in_the_object
     fido = Dog.new
     assert_nothing_raised(Exception) do # __
       fido.set_name("Rover")
index 1e670a0..9df4f9b 100644 (file)
@@ -8,7 +8,7 @@ class AboutNil < EdgeCase::Koan
   def test_you_dont_get_null_pointer_errors_when_calling_methods_on_nil
     # What happens when you call a method that doesn't exist.  The
     # following begin/rescue/end code block captures the exception and
-    # make some assertions about it.
+    # makes some assertions about it.
     begin
       nil.some_method_nil_doesnt_know_about
     rescue Exception => ex
index d618e7c..95cdffd 100644 (file)
@@ -84,7 +84,7 @@ class AboutSymbols < EdgeCase::Koan
   # interesting string operations are available on symbols.
 
   def test_symbols_cannot_be_concatenated
-    # Exceptions will be pondered further father down the path
+    # Exceptions will be pondered further down the path
     assert_raise(___(NoMethodError)) do
       :cats + :dogs
     end