diff --git a/src/about_keyword_arguments.rb b/src/about_keyword_arguments.rb new file mode 100644 index 0000000..7b37e1b --- /dev/null +++ b/src/about_keyword_arguments.rb @@ -0,0 +1,31 @@ +require File.expand_path(File.dirname(__FILE__) + '/edgecase') + +class AboutMethods < EdgeCase::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_keywork_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_keywork_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 diff --git a/src/about_message_passing.rb b/src/about_message_passing.rb index 882716e..11a0a79 100644 --- a/src/about_message_passing.rb +++ b/src/about_message_passing.rb @@ -93,11 +93,11 @@ class AboutMessagePassing < EdgeCase::Koan # 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. diff --git a/src/about_methods.rb b/src/about_methods.rb index eabe7aa..b720010 100644 --- a/src/about_methods.rb +++ b/src/about_methods.rb @@ -77,36 +77,6 @@ class AboutMethods < EdgeCase::Koan assert_equal __([:one]), method_with_var_args(:one) assert_equal __([:one, :two]), method_with_var_args(:one, :two) end - # ------------------------------------------------------------------ - - if ruby_version?('2.0') - def method_with_keyword_arguments(one: 1, two: 'two') - [one, two] - end - - def test_keyword_arguments - assert_equal __, method_with_keyword_arguments.class - assert_equal __, method_with_keyword_arguments - assert_equal __, method_with_keyword_arguments(one: 'one') - assert_equal __, method_with_keyword_arguments(two: 2) - end - - def method_with_keywork_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 (__) do - method_with_keywork_arguments_with_mandatory_argument - end - assert_match(/__/, exception.message) - end - - # THINK ABOUT IT: - # - # Keyword arguments always have a default value, making them optional to the caller - - end # ------------------------------------------------------------------ diff --git a/src/edgecase.rb b/src/edgecase.rb index bd65fdf..713f55d 100644 --- a/src/edgecase.rb +++ b/src/edgecase.rb @@ -65,7 +65,7 @@ class Object end end - in_ruby_version("1.9") do + in_ruby_version("1.9", "2") do public :method_missing end end diff --git a/src/path_to_enlightenment.rb b/src/path_to_enlightenment.rb index 64621bf..9e8ccbe 100644 --- a/src/path_to_enlightenment.rb +++ b/src/path_to_enlightenment.rb @@ -12,6 +12,9 @@ require 'about_strings' 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'