Add keyword arguments (with correct answers).

This commit is contained in:
Jim Weirich
2013-04-09 09:45:23 -04:00
parent 58238ef54a
commit f03e0d21ee
5 changed files with 40 additions and 36 deletions

View File

@@ -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

View File

@@ -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.

View File

@@ -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
# ------------------------------------------------------------------

View File

@@ -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

View File

@@ -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'