mirror of
https://github.com/edgecase/ruby_koans.git
synced 2026-04-15 07:23:19 -04:00
Merged src directory with existing koans.
This commit is contained in:
@@ -1,12 +1,12 @@
|
|||||||
= Playing Greed
|
= Playing Greed
|
||||||
|
|
||||||
Greed is a dice game played among 2 or more players, using 5
|
Greed is a dice game played amoung 2 or more players, using 5
|
||||||
six-sided dice.
|
six-sided dice.
|
||||||
|
|
||||||
== Playing Greed
|
== Playing Greed
|
||||||
|
|
||||||
Each player takes a turn consisting of one or more rolls of the dice.
|
Each player takes a turn consisting of one or more rolls of the dice.
|
||||||
On the first roll of the game, a player rolls all five dice which are
|
On the first roll of the game, a player rolls all six dice which are
|
||||||
scored according to the following:
|
scored according to the following:
|
||||||
|
|
||||||
Three 1's => 1000 points
|
Three 1's => 1000 points
|
||||||
@@ -37,7 +37,7 @@ final example.
|
|||||||
|
|
||||||
After a player rolls and the score is calculated, the scoring dice are
|
After a player rolls and the score is calculated, the scoring dice are
|
||||||
removed and the player has the option of rolling again using only the
|
removed and the player has the option of rolling again using only the
|
||||||
non-scoring dice. If all of the dice are scoring, then the player
|
non-scoring dice. If there all no non-scoring dice), then the player
|
||||||
may roll all 5 dice in the next roll.
|
may roll all 5 dice in the next roll.
|
||||||
|
|
||||||
The player may continue to roll as long as each roll scores points. If
|
The player may continue to roll as long as each roll scores points. If
|
||||||
|
|||||||
@@ -40,6 +40,7 @@ class AboutArrays < EdgeCase::Koan
|
|||||||
assert_equal __, array[2,2]
|
assert_equal __, array[2,2]
|
||||||
assert_equal __, array[2,20]
|
assert_equal __, array[2,20]
|
||||||
assert_equal __, array[4,0]
|
assert_equal __, array[4,0]
|
||||||
|
assert_equal __, array[4,100]
|
||||||
assert_equal __, array[5,0]
|
assert_equal __, array[5,0]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ class AboutAsserts < EdgeCase::Koan
|
|||||||
|
|
||||||
# We shall contemplate truth by testing reality, via asserts.
|
# We shall contemplate truth by testing reality, via asserts.
|
||||||
def test_assert_truth
|
def test_assert_truth
|
||||||
assert false # This should be true
|
assert true # This should be true
|
||||||
end
|
end
|
||||||
|
|
||||||
# Enlightenment may be more easily achieved with appropriate
|
# Enlightenment may be more easily achieved with appropriate
|
||||||
@@ -19,7 +19,7 @@ class AboutAsserts < EdgeCase::Koan
|
|||||||
# To understand reality, we must compare our expectations against
|
# To understand reality, we must compare our expectations against
|
||||||
# reality.
|
# reality.
|
||||||
def test_assert_equality
|
def test_assert_equality
|
||||||
expected_value = 3
|
expected_value = __
|
||||||
actual_value = 1 + 1
|
actual_value = 1 + 1
|
||||||
|
|
||||||
assert expected_value == actual_value
|
assert expected_value == actual_value
|
||||||
@@ -27,7 +27,7 @@ class AboutAsserts < EdgeCase::Koan
|
|||||||
|
|
||||||
# Some ways of asserting equality are better than others.
|
# Some ways of asserting equality are better than others.
|
||||||
def test_a_better_way_of_asserting_equality
|
def test_a_better_way_of_asserting_equality
|
||||||
expected_value = 3
|
expected_value = __
|
||||||
actual_value = 1 + 1
|
actual_value = 1 + 1
|
||||||
|
|
||||||
assert_equal expected_value, actual_value
|
assert_equal expected_value, actual_value
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ class AboutControlStatements < EdgeCase::Koan
|
|||||||
assert_equal __, result
|
assert_equal __, result
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_if_then_statements
|
def test_if_then_else_statements
|
||||||
result = :default_value
|
result = :default_value
|
||||||
if true
|
if true
|
||||||
result = :true_value
|
result = :true_value
|
||||||
|
|||||||
@@ -6,10 +6,10 @@ class AboutExceptions < EdgeCase::Koan
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_exceptions_inherit_from_Exception
|
def test_exceptions_inherit_from_Exception
|
||||||
assert MySpecialError.ancestors.include?(RuntimeError)
|
assert_equal __, MySpecialError.ancestors[1]
|
||||||
assert MySpecialError.ancestors.include?(StandardError)
|
assert_equal __, MySpecialError.ancestors[2]
|
||||||
assert MySpecialError.ancestors.include?(Exception)
|
assert_equal __, MySpecialError.ancestors[3]
|
||||||
assert MySpecialError.ancestors.include?(Object)
|
assert_equal __, MySpecialError.ancestors[4]
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_rescue_clause
|
def test_rescue_clause
|
||||||
@@ -40,7 +40,7 @@ class AboutExceptions < EdgeCase::Koan
|
|||||||
result = :exception_handled
|
result = :exception_handled
|
||||||
end
|
end
|
||||||
|
|
||||||
assert_equal __(:exception_handled), result
|
assert_equal __, result
|
||||||
assert_equal __, ex.message
|
assert_equal __, ex.message
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -38,11 +38,20 @@ class AboutHashes < EdgeCase::Koan
|
|||||||
assert_equal hash1, hash2
|
assert_equal hash1, hash2
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_hash_keys_and_values
|
def test_hash_keys
|
||||||
hash = { :one => "uno", :two => "dos" }
|
hash = { :one => "uno", :two => "dos" }
|
||||||
|
assert_equal __, hash.keys.size
|
||||||
|
assert_equal __, hash.keys.include?(:one)
|
||||||
|
assert_equal __, hash.keys.include?(:two)
|
||||||
|
assert_equal Array, hash.keys.class
|
||||||
|
end
|
||||||
|
|
||||||
assert_equal __, hash.keys.sort
|
def test_hash_values
|
||||||
assert_equal __, hash.values.sort
|
hash = { :one => "uno", :two => "dos" }
|
||||||
|
assert_equal __, hash.keys.size
|
||||||
|
assert_equal __, hash.values.include?("uno")
|
||||||
|
assert_equal __, hash.values.include?("dos")
|
||||||
|
assert_equal Array, hash.values.class
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_combining_hashes
|
def test_combining_hashes
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ class AboutMessagePassing < EdgeCase::Koan
|
|||||||
|
|
||||||
assert mc.send("caught?")
|
assert mc.send("caught?")
|
||||||
assert mc.send("caught" + __ ) # What do you need to add to the first string?
|
assert mc.send("caught" + __ ) # What do you need to add to the first string?
|
||||||
assert mc.send("CAUGHT?".__ ) # What would you need to do to the string?
|
assert mc.send("CAUGHT?".____ ) # What would you need to do to the string?
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_send_with_underscores_will_also_send_messages
|
def test_send_with_underscores_will_also_send_messages
|
||||||
@@ -96,7 +96,7 @@ class AboutMessagePassing < EdgeCase::Koan
|
|||||||
|
|
||||||
class AllMessageCatcher
|
class AllMessageCatcher
|
||||||
def method_missing(method_name, *args, &block)
|
def method_missing(method_name, *args, &block)
|
||||||
"Someone called #{method_name} with (#{args.join(", ")})"
|
"Someone called #{method_name} with <#{args.join(", ")}>"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ class AboutMethods < EdgeCase::Koan
|
|||||||
# (NOTE: We are Using eval below because the example code is
|
# (NOTE: We are Using eval below because the example code is
|
||||||
# considered to be syntactically invalid).
|
# considered to be syntactically invalid).
|
||||||
def test_sometimes_missing_parenthesis_are_ambiguous
|
def test_sometimes_missing_parenthesis_are_ambiguous
|
||||||
eval "assert_equal 5, my_global_method 2, 3"
|
eval "assert_equal 5, my_global_method 2, 3" # ENABLE CHECK
|
||||||
#
|
#
|
||||||
# Ruby doesn't know if you mean:
|
# Ruby doesn't know if you mean:
|
||||||
#
|
#
|
||||||
@@ -36,12 +36,12 @@ class AboutMethods < EdgeCase::Koan
|
|||||||
exception = assert_raise(___) do
|
exception = assert_raise(___) do
|
||||||
my_global_method
|
my_global_method
|
||||||
end
|
end
|
||||||
assert_equal __, exception.message
|
assert_match(/__/, exception.message)
|
||||||
|
|
||||||
exception = assert_raise(___) do
|
exception = assert_raise(___) do
|
||||||
my_global_method(1,2,3)
|
my_global_method(1,2,3)
|
||||||
end
|
end
|
||||||
assert_equal __, exception.message
|
assert_match(/__/, exception.message)
|
||||||
end
|
end
|
||||||
|
|
||||||
# ------------------------------------------------------------------
|
# ------------------------------------------------------------------
|
||||||
|
|||||||
@@ -2,34 +2,23 @@ require 'edgecase'
|
|||||||
|
|
||||||
class AboutNil < EdgeCase::Koan
|
class AboutNil < EdgeCase::Koan
|
||||||
def test_nil_is_an_object
|
def test_nil_is_an_object
|
||||||
#
|
assert_equal __, nil.is_a?(Object), "Unlike NULL in other languages"
|
||||||
# Hint: '!'s negate the response from what follows.
|
|
||||||
#
|
|
||||||
assert !nil.is_a?(Object), "Unlike NULL in other languages"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_you_dont_get_null_pointer_errors_when_calling_methods_on_nil
|
def test_you_dont_get_null_pointer_errors_when_calling_methods_on_nil
|
||||||
#
|
# What happens when you call a method that doesn't exist. The
|
||||||
# What is the Exception that is thrown when you call a method that
|
# following begin/rescue/end code block captures the exception and
|
||||||
# does not exist?
|
# make some assertions about it.
|
||||||
#
|
begin
|
||||||
# Hint: launch irb and try the code in the block below.
|
|
||||||
#
|
|
||||||
# Don't be confused by the code below yet. It's using blocks
|
|
||||||
# which are explained later on in about_blocks.rb. For now,
|
|
||||||
# think about it like running nil.some_method_nil_doesnt_know_about
|
|
||||||
# in a sandbox and catching the error class into the exception
|
|
||||||
# variable.
|
|
||||||
#
|
|
||||||
exception = assert_raise(___) do
|
|
||||||
nil.some_method_nil_doesnt_know_about
|
nil.some_method_nil_doesnt_know_about
|
||||||
end
|
rescue Exception => ex
|
||||||
|
# What exception has been caught?
|
||||||
|
assert_equal __, ex.class
|
||||||
|
|
||||||
#
|
# What message was attached to the exception?
|
||||||
# What is the error message itself? What substring or pattern could
|
# (HINT: replace __ with part of the error message.)
|
||||||
# you test against in order to have a good idea what the string is?
|
assert_match(/__/, ex.message)
|
||||||
#
|
end
|
||||||
assert_match /__/, exception.message
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_nil_has_a_few_methods_defined_on_it
|
def test_nil_has_a_few_methods_defined_on_it
|
||||||
|
|||||||
@@ -15,7 +15,10 @@ require 'edgecase'
|
|||||||
class Proxy
|
class Proxy
|
||||||
def initialize(target_object)
|
def initialize(target_object)
|
||||||
@object = target_object
|
@object = target_object
|
||||||
|
# ADD MORE CODE HERE
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# WRITE CODE HERE
|
||||||
end
|
end
|
||||||
|
|
||||||
# The proxy object should pass the following Koan:
|
# The proxy object should pass the following Koan:
|
||||||
|
|||||||
@@ -14,6 +14,14 @@ def ___(value=FillMeInError)
|
|||||||
value
|
value
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class Object
|
||||||
|
def ____(method=nil)
|
||||||
|
if method
|
||||||
|
self.send(method)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
module EdgeCase
|
module EdgeCase
|
||||||
class Sensei
|
class Sensei
|
||||||
attr_reader :failure, :failed_test
|
attr_reader :failure, :failed_test
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ require 'about_blocks'
|
|||||||
require 'about_sandwich_code'
|
require 'about_sandwich_code'
|
||||||
require 'about_scoring_project'
|
require 'about_scoring_project'
|
||||||
require 'about_classes'
|
require 'about_classes'
|
||||||
|
require 'about_open_classes'
|
||||||
require 'about_dice_project'
|
require 'about_dice_project'
|
||||||
require 'about_inheritance'
|
require 'about_inheritance'
|
||||||
require 'about_modules'
|
require 'about_modules'
|
||||||
|
|||||||
@@ -42,7 +42,6 @@ class AboutArrays < EdgeCase::Koan
|
|||||||
assert_equal __([]), array[4,0]
|
assert_equal __([]), array[4,0]
|
||||||
assert_equal __([]), array[4,100]
|
assert_equal __([]), array[4,100]
|
||||||
assert_equal __(nil), array[5,0]
|
assert_equal __(nil), array[5,0]
|
||||||
assert_equal __(nil), array[5,0]
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_arrays_and_ranges
|
def test_arrays_and_ranges
|
||||||
@@ -57,7 +56,7 @@ class AboutArrays < EdgeCase::Koan
|
|||||||
|
|
||||||
assert_equal __([:peanut, :butter, :and]), array[0..2]
|
assert_equal __([:peanut, :butter, :and]), array[0..2]
|
||||||
assert_equal __([:peanut, :butter]), array[0...2]
|
assert_equal __([:peanut, :butter]), array[0...2]
|
||||||
assert_equal ([:and, :jelly]), array[2..-1]
|
assert_equal __([:and, :jelly]), array[2..-1]
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_pushing_and_popping_arrays
|
def test_pushing_and_popping_arrays
|
||||||
|
|||||||
@@ -7,13 +7,13 @@ class AboutAsserts < EdgeCase::Koan
|
|||||||
|
|
||||||
# We shall contemplate truth by testing reality, via asserts.
|
# We shall contemplate truth by testing reality, via asserts.
|
||||||
def test_assert_truth
|
def test_assert_truth
|
||||||
assert __(true) # This should be true
|
assert true # This should be true
|
||||||
end
|
end
|
||||||
|
|
||||||
# Enlightenment may be more easily achieved with appropriate
|
# Enlightenment may be more easily achieved with appropriate
|
||||||
# messages.
|
# messages.
|
||||||
def test_assert_with_message
|
def test_assert_with_message
|
||||||
assert __(true), "This should be true -- Please fix this"
|
assert false, "This should be true -- Please fix this"
|
||||||
end
|
end
|
||||||
|
|
||||||
# To understand reality, we must compare our expectations against
|
# To understand reality, we must compare our expectations against
|
||||||
|
|||||||
@@ -19,11 +19,11 @@ class AboutClassMethods < EdgeCase::Koan
|
|||||||
|
|
||||||
def test_objects_have_methods
|
def test_objects_have_methods
|
||||||
fido = Dog.new
|
fido = Dog.new
|
||||||
assert_equal __(45), fido.methods.size
|
assert_equal __(44), fido.methods.size
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_classes_have_methods
|
def test_classes_have_methods
|
||||||
assert_equal __(80), Dog.methods.size
|
assert_equal __(79), Dog.methods.size
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_you_can_define_methods_on_individual_objects
|
def test_you_can_define_methods_on_individual_objects
|
||||||
@@ -34,9 +34,12 @@ class AboutClassMethods < EdgeCase::Koan
|
|||||||
assert_equal __(:fidos_wag), fido.wag
|
assert_equal __(:fidos_wag), fido.wag
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_other_objects_are_unaffected_by_these_singleton_methods
|
def test_other_objects_are_not_affected_by_these_singleton_methods
|
||||||
fido = Dog.new
|
fido = Dog.new
|
||||||
rover = Dog.new
|
rover = Dog.new
|
||||||
|
def fido.wag
|
||||||
|
:fidos_wag
|
||||||
|
end
|
||||||
|
|
||||||
assert_raise(___(NoMethodError)) do
|
assert_raise(___(NoMethodError)) do
|
||||||
rover.wag
|
rover.wag
|
||||||
@@ -45,24 +48,24 @@ class AboutClassMethods < EdgeCase::Koan
|
|||||||
|
|
||||||
# ------------------------------------------------------------------
|
# ------------------------------------------------------------------
|
||||||
|
|
||||||
def Dog.bark
|
class Dog2
|
||||||
:class_level_bark
|
def wag
|
||||||
end
|
:instance_level_wag
|
||||||
|
|
||||||
class Dog
|
|
||||||
def bark
|
|
||||||
:instance_level_bark
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def Dog2.wag
|
||||||
|
:class_level_wag
|
||||||
|
end
|
||||||
|
|
||||||
def test_since_classes_are_objects_you_can_define_singleton_methods_on_them_too
|
def test_since_classes_are_objects_you_can_define_singleton_methods_on_them_too
|
||||||
assert_equal __(:class_level_bark), Dog.bark
|
assert_equal __(:class_level_wag), Dog2.wag
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_class_methods_are_independent_of_instance_methods
|
def test_class_methods_are_independent_of_instance_methods
|
||||||
fido = Dog.new
|
fido = Dog2.new
|
||||||
assert_equal __(:instance_level_bark), fido.bark
|
assert_equal __(:instance_level_wag), fido.wag
|
||||||
assert_equal __(:class_level_bark), Dog.bark
|
assert_equal __(:class_level_wag), Dog2.wag
|
||||||
end
|
end
|
||||||
|
|
||||||
# ------------------------------------------------------------------
|
# ------------------------------------------------------------------
|
||||||
@@ -155,7 +158,7 @@ class AboutClassMethods < EdgeCase::Koan
|
|||||||
# end
|
# end
|
||||||
#
|
#
|
||||||
# Which do you prefer and why?
|
# Which do you prefer and why?
|
||||||
# Are there times you might prefer the other way?
|
# Are there times you might prefer one over the other?
|
||||||
|
|
||||||
# ------------------------------------------------------------------
|
# ------------------------------------------------------------------
|
||||||
|
|
||||||
|
|||||||
@@ -79,7 +79,7 @@ class AboutMethods < EdgeCase::Koan
|
|||||||
def method_with_explicit_return
|
def method_with_explicit_return
|
||||||
:a_non_return_value
|
:a_non_return_value
|
||||||
return :return_value
|
return :return_value
|
||||||
:anoher_non_return_value
|
:another_non_return_value
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_method_with_explicit_return
|
def test_method_with_explicit_return
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ require 'edgecase'
|
|||||||
# score([3,4,5,3,3]) => 350 points
|
# score([3,4,5,3,3]) => 350 points
|
||||||
# score([1,5,1,2,4]) => 250 points
|
# score([1,5,1,2,4]) => 250 points
|
||||||
#
|
#
|
||||||
# More scoing examples are given in the tests below:
|
# More scoring examples are given in the tests below:
|
||||||
#
|
#
|
||||||
# Your goal is to write the score method.
|
# Your goal is to write the score method.
|
||||||
|
|
||||||
|
|||||||
@@ -128,7 +128,7 @@ EOS
|
|||||||
assert_equal __('The value is #{value}'), string
|
assert_equal __('The value is #{value}'), string
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_any_ruby_expression_my_be_interpolated
|
def test_any_ruby_expression_may_be_interpolated
|
||||||
string = "The square root of 5 is #{Math.sqrt(5)}"
|
string = "The square root of 5 is #{Math.sqrt(5)}"
|
||||||
assert_equal __("The square root of 5 is 2.23606797749979"), string
|
assert_equal __("The square root of 5 is 2.23606797749979"), string
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ require 'edgecase'
|
|||||||
# You need to write the triangle method in the file 'triangle.rb'
|
# You need to write the triangle method in the file 'triangle.rb'
|
||||||
require 'triangle.rb'
|
require 'triangle.rb'
|
||||||
|
|
||||||
class AboutTriangleAssignment < EdgeCase::Koan
|
class AboutTriangleAssignment2 < EdgeCase::Koan
|
||||||
# The first assignment did not talk about how to handle errors.
|
# The first assignment did not talk about how to handle errors.
|
||||||
# Let's handle that part now.
|
# Let's handle that part now.
|
||||||
def test_illegal_triangles_throw_exceptions
|
def test_illegal_triangles_throw_exceptions
|
||||||
|
|||||||
Reference in New Issue
Block a user