From c7a56e50fac2851e2ced74fd8691827ea4ff3222 Mon Sep 17 00:00:00 2001 From: Varun Iyer Date: Mon, 15 Aug 2022 14:12:39 -0700 Subject: [PATCH] Add a koan for mandatory keyword arguments Previously, `about_keyword_arguments.rb` asked the reader to reflect on the fact that keyword arguments must have default values. However, this does not seem to be true anymore in Ruby. This commit replaces that comment with two koans that guide the reader through validating and using a method with mandatory keyword arguments. --- src/about_keyword_arguments.rb | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/about_keyword_arguments.rb b/src/about_keyword_arguments.rb index 1addb39..3c9856b 100644 --- a/src/about_keyword_arguments.rb +++ b/src/about_keyword_arguments.rb @@ -24,8 +24,20 @@ class AboutKeywordArguments < Neo::Koan 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 + def method_with_mandatory_keyword_arguments(one:, two: 'two') + [one, two] + end + + def test_mandatory_keyword_arguments + assert_equal __(['one', 'two']), method_with_mandatory_keyword_arguments(one: 'one') + assert_equal __([1, 2]), method_with_mandatory_keyword_arguments(two: 2, one: 1) + end + + def test_mandatory_keyword_arguments_without_mandatory_argument + exception = assert_raise(___(ArgumentError)) do + method_with_mandatory_keyword_arguments + end + assert_match(/#{__("missing keyword: :one")}/, exception.message) + end end