Skip to content

Commit c7a56e5

Browse files
committed
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.
1 parent 0659d2a commit c7a56e5

1 file changed

Lines changed: 15 additions & 3 deletions

File tree

src/about_keyword_arguments.rb

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,20 @@ def test_keyword_arguments_with_wrong_number_of_arguments
2424
assert_match(/#{__("wrong number of arguments")}/, exception.message)
2525
end
2626

27-
# THINK ABOUT IT:
28-
#
29-
# Keyword arguments always have a default value, making them optional to the caller
27+
def method_with_mandatory_keyword_arguments(one:, two: 'two')
28+
[one, two]
29+
end
30+
31+
def test_mandatory_keyword_arguments
32+
assert_equal __(['one', 'two']), method_with_mandatory_keyword_arguments(one: 'one')
33+
assert_equal __([1, 2]), method_with_mandatory_keyword_arguments(two: 2, one: 1)
34+
end
35+
36+
def test_mandatory_keyword_arguments_without_mandatory_argument
37+
exception = assert_raise(___(ArgumentError)) do
38+
method_with_mandatory_keyword_arguments
39+
end
40+
assert_match(/#{__("missing keyword: :one")}/, exception.message)
41+
end
3042

3143
end

0 commit comments

Comments
 (0)