diff --git a/koans/about_methods.rb b/koans/about_methods.rb index cbebd30..fde97e9 100644 --- a/koans/about_methods.rb +++ b/koans/about_methods.rb @@ -3,7 +3,7 @@ require File.expand_path(File.dirname(__FILE__) + '/edgecase') def my_global_method(a,b) a + b end - + class AboutMethods < EdgeCase::Koan def test_calling_global_methods @@ -29,19 +29,19 @@ class AboutMethods < EdgeCase::Koan # Rewrite the eval string to continue. # end - + # NOTE: wrong number of argument is not a SYNTAX error, but a # runtime error. def test_calling_global_methods_with_wrong_number_of_arguments exception = assert_raise(___) do my_global_method end - assert_match(/__/, exception.message) + assert_match(/#{__ of arguments")}/, exception.message) exception = assert_raise(___) do my_global_method(1,2,3) end - assert_match(/__/, exception.message) + assert_match(/#{__ of arguments")}/, exception.message) end # ------------------------------------------------------------------ @@ -135,7 +135,7 @@ class AboutMethods < EdgeCase::Koan "tail" end end - + def test_calling_methods_in_other_objects_require_explicit_receiver rover = Dog.new assert_equal __, rover.name diff --git a/koans/about_symbols.rb b/koans/about_symbols.rb index e78c65a..b94c3b8 100644 --- a/koans/about_symbols.rb +++ b/koans/about_symbols.rb @@ -25,15 +25,21 @@ class AboutSymbols < EdgeCase::Koan def test_method_names_become_symbols all_symbols = Symbol.all_symbols - - assert_equal __, all_symbols.include?(:test_method_names_are_symbols) + assert_equal __, all_symbols.include?(:test_method_names_become_symbols) end - RubyConstant = "What is the sound of one hand clapping?" - def test_constants_become_symbols - all_symbols = Symbol.all_symbols + # THINK ABOUT IT: + # + # Why do we capture the list of symbols before we check for the + # method name? - assert_equal true, all_symbols.include?(__) + in_ruby_version("mri") do + RubyConstant = "What is the sound of one hand clapping?" + def test_constants_become_symbols + all_symbols = Symbol.all_symbols + + assert_equal __, all_symbols.include?(__) + end end def test_symbols_can_be_made_from_strings @@ -47,6 +53,13 @@ class AboutSymbols < EdgeCase::Koan assert_equal symbol, __.to_sym end + def test_symbols_with_spaces_can_be_built + value = "and" + symbol = :"cats #{value} dogs" + + assert_equal symbol, __.to_sym + end + def test_to_s_is_called_on_interpolated_symbols symbol = :cats string = "It is raining #{symbol} and dogs." @@ -65,13 +78,23 @@ class AboutSymbols < EdgeCase::Koan assert_equal __, symbol.respond_to?(:each_char) assert_equal __, symbol.respond_to?(:reverse) end + # It's important to realize that symbols are not "immutable # strings", though they are immutable. None of the # interesting string operations are available on symbols. + def test_symbols_cannot_be_concatenated # Exceptions will be pondered further father down the path assert_raise(___) do :cats + :dogs end end + + def test_symbols_can_be_dynamically_created + assert_equal __, ("cats" + "dogs").to_sym + end + + # THINK ABOUT IT: + # + # Why is it not a good idea to dynamically create a lot of symbols? end diff --git a/koans/edgecase.rb b/koans/edgecase.rb index 6ee835e..1a5b882 100644 --- a/koans/edgecase.rb +++ b/koans/edgecase.rb @@ -6,8 +6,14 @@ require 'test/unit/assertions' class FillMeInError < StandardError end -def in_ruby_version(version) - yield if RUBY_VERSION =~ /^#{version}/ +def ruby_version?(version) + RUBY_VERSION =~ /^#{version}/ || + (version == 'jruby' && defined?(JRUBY_VERSION)) || + (version == 'mri' && ! defined?(JRUBY_VERSION)) +end + +def in_ruby_version(*versions) + yield if versions.any? { |v| ruby_version?(v) } end def __(value="FILL ME IN", value19=:mu) @@ -248,6 +254,7 @@ ENDTEXT end def indent(text) + text = text.split(/\n/) if text.is_a?(String) text.collect{|t| " #{t}"} end