mirror of
https://github.com/edgecase/ruby_koans.git
synced 2026-04-17 07:53:20 -04:00
Back ported a lot of changes made to the Koans directory.
Evidently, a lot of changes / pull requests were made to the koans directory and not to the src directory. Perhaps we should remove the koans directory entirely from the repo.
This commit is contained in:
@@ -28,13 +28,14 @@ class AboutProxyObjectProject < EdgeCase::Koan
|
|||||||
# NOTE: The Television class is defined below
|
# NOTE: The Television class is defined below
|
||||||
tv = Proxy.new(Television.new)
|
tv = Proxy.new(Television.new)
|
||||||
|
|
||||||
|
# HINT: Proxy class is defined above, may need tweaking...
|
||||||
|
|
||||||
assert tv.instance_of?(Proxy)
|
assert tv.instance_of?(Proxy)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_tv_methods_still_perform_their_function
|
def test_tv_methods_still_perform_their_function
|
||||||
tv = Proxy.new(Television.new)
|
tv = Proxy.new(Television.new)
|
||||||
|
|
||||||
# HINT Proxy class is defined above, may need tweaking...
|
|
||||||
tv.channel = 10
|
tv.channel = 10
|
||||||
tv.power
|
tv.power
|
||||||
|
|
||||||
|
|||||||
@@ -141,6 +141,13 @@ EOS
|
|||||||
assert_equal __, string[7..9]
|
assert_equal __, string[7..9]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_you_can_get_a_single_character_from_a_string
|
||||||
|
string = "Bacon, lettuce and tomato"
|
||||||
|
assert_equal __, string[1]
|
||||||
|
|
||||||
|
# Surprised?
|
||||||
|
end
|
||||||
|
|
||||||
in_ruby_version("1.8") do
|
in_ruby_version("1.8") do
|
||||||
def test_in_ruby_1_8_single_characters_are_represented_by_integers
|
def test_in_ruby_1_8_single_characters_are_represented_by_integers
|
||||||
assert_equal __, ?a
|
assert_equal __, ?a
|
||||||
@@ -157,26 +164,6 @@ EOS
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
in_ruby_version("1.8") do
|
|
||||||
def test_in_ruby_1_8_you_can_get_a_single_character_from_a_string
|
|
||||||
string = "Bacon, lettuce and tomato"
|
|
||||||
assert_equal __, string[1]
|
|
||||||
|
|
||||||
# Surprised?
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
in_ruby_version("1.9") do
|
|
||||||
|
|
||||||
def test_in_ruby_1_9_you_can_get_a_single_character_from_a_string
|
|
||||||
string = "Bacon, lettuce and tomato"
|
|
||||||
assert_equal "__", string[1]
|
|
||||||
|
|
||||||
# Surprised?
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
def test_strings_can_be_split
|
def test_strings_can_be_split
|
||||||
string = "Sausage Egg Cheese"
|
string = "Sausage Egg Cheese"
|
||||||
words = string.split
|
words = string.split
|
||||||
|
|||||||
@@ -84,7 +84,7 @@ class AboutSymbols < EdgeCase::Koan
|
|||||||
# interesting string operations are available on symbols.
|
# interesting string operations are available on symbols.
|
||||||
|
|
||||||
def test_symbols_cannot_be_concatenated
|
def test_symbols_cannot_be_concatenated
|
||||||
# Exceptions will be pondered further farther down the path
|
# Exceptions will be pondered further down the path
|
||||||
assert_raise(___) do
|
assert_raise(___) do
|
||||||
:cats + :dogs
|
:cats + :dogs
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ class AboutTriangleProject2 < EdgeCase::Koan
|
|||||||
assert_raise(TriangleError) do triangle(3, 4, -5) end
|
assert_raise(TriangleError) do triangle(3, 4, -5) end
|
||||||
assert_raise(TriangleError) do triangle(1, 1, 3) end
|
assert_raise(TriangleError) do triangle(1, 1, 3) end
|
||||||
assert_raise(TriangleError) do triangle(2, 4, 2) end
|
assert_raise(TriangleError) do triangle(2, 4, 2) end
|
||||||
#HINT: for tips, see http://stackoverflow.com/questions/3834203/ruby-koan-151-raising-exceptions
|
# HINT: for tips, see http://stackoverflow.com/questions/3834203/ruby-koan-151-raising-exceptions
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ begin
|
|||||||
require 'win32console'
|
require 'win32console'
|
||||||
rescue LoadError
|
rescue LoadError
|
||||||
end
|
end
|
||||||
|
|
||||||
# --------------------------------------------------------------------
|
# --------------------------------------------------------------------
|
||||||
# Support code for the Ruby Koans.
|
# Support code for the Ruby Koans.
|
||||||
# --------------------------------------------------------------------
|
# --------------------------------------------------------------------
|
||||||
@@ -121,6 +122,7 @@ module EdgeCase
|
|||||||
def using_windows?
|
def using_windows?
|
||||||
File::ALT_SEPARATOR
|
File::ALT_SEPARATOR
|
||||||
end
|
end
|
||||||
|
|
||||||
def using_win32console
|
def using_win32console
|
||||||
defined? Win32::Console
|
defined? Win32::Console
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -126,7 +126,7 @@ class AboutClasses < EdgeCase::Koan
|
|||||||
# Why is this so?
|
# Why is this so?
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_different_objects_have_difference_instance_variables
|
def test_different_objects_have_different_instance_variables
|
||||||
fido = Dog6.new("Fido")
|
fido = Dog6.new("Fido")
|
||||||
rover = Dog6.new("Rover")
|
rover = Dog6.new("Rover")
|
||||||
|
|
||||||
|
|||||||
@@ -67,7 +67,7 @@ class AboutConstants < EdgeCase::Koan
|
|||||||
end
|
end
|
||||||
|
|
||||||
# QUESTION: Which has precedence: The constant in the lexical scope,
|
# QUESTION: Which has precedence: The constant in the lexical scope,
|
||||||
# or the constant from the inheritance heirarachy?
|
# or the constant from the inheritance hierarchy?
|
||||||
|
|
||||||
# ------------------------------------------------------------------
|
# ------------------------------------------------------------------
|
||||||
|
|
||||||
@@ -81,7 +81,7 @@ class AboutConstants < EdgeCase::Koan
|
|||||||
assert_equal __(4), MyAnimals::Oyster.new.legs_in_oyster
|
assert_equal __(4), MyAnimals::Oyster.new.legs_in_oyster
|
||||||
end
|
end
|
||||||
|
|
||||||
# QUESTION: Now Which has precedence: The constant in the lexical
|
# QUESTION: Now which has precedence: The constant in the lexical
|
||||||
# scope, or the constant from the inheritance heirarachy? Why is it
|
# scope, or the constant from the inheritance hierarchy? Why is it
|
||||||
# different than the previous answer?
|
# different than the previous answer?
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -59,12 +59,20 @@ class AboutControlStatements < EdgeCase::Koan
|
|||||||
|
|
||||||
def test_unless_statement
|
def test_unless_statement
|
||||||
result = :default_value
|
result = :default_value
|
||||||
unless false
|
unless false # same as saying 'if !false', which evaluates as 'if true'
|
||||||
result = :false_value
|
result = :false_value
|
||||||
end
|
end
|
||||||
assert_equal __(:false_value), result
|
assert_equal __(:false_value), result
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_unless_statement_evaluate_true
|
||||||
|
result = :default_value
|
||||||
|
unless true # same as saying 'if !true', which evaluates as 'if false'
|
||||||
|
result = :true_value
|
||||||
|
end
|
||||||
|
assert_equal __(:default_value), result
|
||||||
|
end
|
||||||
|
|
||||||
def test_unless_statement_modifier
|
def test_unless_statement_modifier
|
||||||
result = :default_value
|
result = :default_value
|
||||||
result = :false_value unless false
|
result = :false_value unless false
|
||||||
|
|||||||
@@ -20,6 +20,18 @@ class AboutHashes < EdgeCase::Koan
|
|||||||
assert_equal __(nil), hash[:doesnt_exist]
|
assert_equal __(nil), hash[:doesnt_exist]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_accessing_hashes_with_fetch
|
||||||
|
hash = { :one => "uno" }
|
||||||
|
assert_equal "uno", hash.fetch(:one)
|
||||||
|
assert_raise(___(IndexError)) do
|
||||||
|
hash.fetch(:doesnt_exist)
|
||||||
|
end
|
||||||
|
|
||||||
|
# THINK ABOUT IT:
|
||||||
|
#
|
||||||
|
# Why might you want to use #fetch instead of #[] when accessing hash keys?
|
||||||
|
end
|
||||||
|
|
||||||
def test_changing_hashes
|
def test_changing_hashes
|
||||||
hash = { :one => "uno", :two => "dos" }
|
hash = { :one => "uno", :two => "dos" }
|
||||||
hash[:one] = "eins"
|
hash[:one] = "eins"
|
||||||
@@ -63,4 +75,42 @@ class AboutHashes < EdgeCase::Koan
|
|||||||
expected = { "jim" => __(54), "amy" => 20, "dan" => 23, "jenny" => __(26) }
|
expected = { "jim" => __(54), "amy" => 20, "dan" => 23, "jenny" => __(26) }
|
||||||
assert_equal __(true), expected == new_hash
|
assert_equal __(true), expected == new_hash
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_default_value
|
||||||
|
hash1 = Hash.new
|
||||||
|
hash1[:one] = 1
|
||||||
|
|
||||||
|
assert_equal __(1), hash1[:one]
|
||||||
|
assert_equal __(nil), hash1[:two]
|
||||||
|
|
||||||
|
hash2 = Hash.new("dos")
|
||||||
|
hash2[:one] = 1
|
||||||
|
|
||||||
|
assert_equal __(1), hash2[:one]
|
||||||
|
assert_equal __("dos"), hash2[:two]
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_default_value_is_the_same_object
|
||||||
|
hash = Hash.new([])
|
||||||
|
|
||||||
|
hash[:one] << "uno"
|
||||||
|
hash[:two] << "dos"
|
||||||
|
|
||||||
|
assert_equal __(["uno", "dos"]), hash[:one]
|
||||||
|
assert_equal __(["uno", "dos"]), hash[:two]
|
||||||
|
assert_equal __(["uno", "dos"]), hash[:three]
|
||||||
|
|
||||||
|
assert_equal __(true), hash[:one].object_id == hash[:two].object_id
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_default_value_with_block
|
||||||
|
hash = Hash.new {|hash, key| hash[key] = [] }
|
||||||
|
|
||||||
|
hash[:one] << "uno"
|
||||||
|
hash[:two] << "dos"
|
||||||
|
|
||||||
|
assert_equal __(["uno"]), hash[:one]
|
||||||
|
assert_equal __(["dos"]), hash[:two]
|
||||||
|
assert_equal __([]), hash[:three]
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -72,6 +72,7 @@ class AboutMethods < EdgeCase::Koan
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_calling_with_variable_arguments
|
def test_calling_with_variable_arguments
|
||||||
|
assert_equal __(Array), method_with_var_args.class
|
||||||
assert_equal __([]), method_with_var_args
|
assert_equal __([]), method_with_var_args
|
||||||
assert_equal __([:one]), method_with_var_args(:one)
|
assert_equal __([:one]), method_with_var_args(:one)
|
||||||
assert_equal __([:one, :two]), method_with_var_args(:one, :two)
|
assert_equal __([:one, :two]), method_with_var_args(:one, :two)
|
||||||
|
|||||||
@@ -47,6 +47,8 @@ class AboutProxyObjectProject < EdgeCase::Koan
|
|||||||
# NOTE: The Television class is defined below
|
# NOTE: The Television class is defined below
|
||||||
tv = Proxy.new(Television.new)
|
tv = Proxy.new(Television.new)
|
||||||
|
|
||||||
|
# HINT: Proxy class is defined above, may need tweaking...
|
||||||
|
|
||||||
assert tv.instance_of?(Proxy)
|
assert tv.instance_of?(Proxy)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -84,6 +84,8 @@ class AboutRegularExpressions < EdgeCase::Koan
|
|||||||
def test_shortcut_character_classes_are_negated_with_capitals
|
def test_shortcut_character_classes_are_negated_with_capitals
|
||||||
assert_equal __("the number is "), "the number is 42"[/\D+/]
|
assert_equal __("the number is "), "the number is 42"[/\D+/]
|
||||||
assert_equal __("space:"), "space: \t\n"[/\S+/]
|
assert_equal __("space:"), "space: \t\n"[/\S+/]
|
||||||
|
# ... a programmer would most likely do
|
||||||
|
assert_equal __(" = "), "variable_1 = 42"[/[^a-zA-Z0-9_]+/]
|
||||||
assert_equal __(" = "), "variable_1 = 42"[/\W+/]
|
assert_equal __(" = "), "variable_1 = 42"[/\W+/]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ class AboutTriangleProject2 < EdgeCase::Koan
|
|||||||
assert_raise(TriangleError) do triangle(3, 4, -5) end
|
assert_raise(TriangleError) do triangle(3, 4, -5) end
|
||||||
assert_raise(TriangleError) do triangle(1, 1, 3) end
|
assert_raise(TriangleError) do triangle(1, 1, 3) end
|
||||||
assert_raise(TriangleError) do triangle(2, 4, 2) end
|
assert_raise(TriangleError) do triangle(2, 4, 2) end
|
||||||
|
# HINT: for tips, see http://stackoverflow.com/questions/3834203/ruby-koan-151-raising-exceptions
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,10 @@
|
|||||||
# -*- ruby -*-
|
# -*- ruby -*-
|
||||||
|
|
||||||
require 'test/unit/assertions'
|
require 'test/unit/assertions'
|
||||||
|
begin
|
||||||
|
require 'win32console'
|
||||||
|
rescue LoadError
|
||||||
|
end
|
||||||
|
|
||||||
# --------------------------------------------------------------------
|
# --------------------------------------------------------------------
|
||||||
# Support code for the Ruby Koans.
|
# Support code for the Ruby Koans.
|
||||||
@@ -107,7 +111,9 @@ module EdgeCase
|
|||||||
def use_colors?
|
def use_colors?
|
||||||
return false if ENV['NO_COLOR']
|
return false if ENV['NO_COLOR']
|
||||||
if ENV['ANSI_COLOR'].nil?
|
if ENV['ANSI_COLOR'].nil?
|
||||||
! using_windows?
|
if using_windows?
|
||||||
|
using_win32console
|
||||||
|
end
|
||||||
else
|
else
|
||||||
ENV['ANSI_COLOR'] =~ /^(t|y)/i
|
ENV['ANSI_COLOR'] =~ /^(t|y)/i
|
||||||
end
|
end
|
||||||
@@ -116,6 +122,10 @@ module EdgeCase
|
|||||||
def using_windows?
|
def using_windows?
|
||||||
File::ALT_SEPARATOR
|
File::ALT_SEPARATOR
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def using_win32console
|
||||||
|
defined? Win32::Console
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class Sensei
|
class Sensei
|
||||||
|
|||||||
Reference in New Issue
Block a user