mirror of
https://github.com/edgecase/ruby_koans.git
synced 2026-04-14 23:13:20 -04:00
Updated for JRuby
This commit is contained in:
@@ -3,7 +3,7 @@ require File.expand_path(File.dirname(__FILE__) + '/edgecase')
|
|||||||
def my_global_method(a,b)
|
def my_global_method(a,b)
|
||||||
a + b
|
a + b
|
||||||
end
|
end
|
||||||
|
|
||||||
class AboutMethods < EdgeCase::Koan
|
class AboutMethods < EdgeCase::Koan
|
||||||
|
|
||||||
def test_calling_global_methods
|
def test_calling_global_methods
|
||||||
@@ -36,19 +36,19 @@ class AboutMethods < EdgeCase::Koan
|
|||||||
# Rewrite the eval string to continue.
|
# Rewrite the eval string to continue.
|
||||||
#
|
#
|
||||||
end
|
end
|
||||||
|
|
||||||
# NOTE: wrong number of argument is not a SYNTAX error, but a
|
# NOTE: wrong number of argument is not a SYNTAX error, but a
|
||||||
# runtime error.
|
# runtime error.
|
||||||
def test_calling_global_methods_with_wrong_number_of_arguments
|
def test_calling_global_methods_with_wrong_number_of_arguments
|
||||||
exception = assert_raise(___(ArgumentError)) do
|
exception = assert_raise(___(ArgumentError)) do
|
||||||
my_global_method
|
my_global_method
|
||||||
end
|
end
|
||||||
assert_match(/#{__("wrong number of arguments")}/, exception.message)
|
assert_match(/#{__("wrong (number|#) of arguments")}/, exception.message)
|
||||||
|
|
||||||
exception = assert_raise(___(ArgumentError)) do
|
exception = assert_raise(___(ArgumentError)) do
|
||||||
my_global_method(1,2,3)
|
my_global_method(1,2,3)
|
||||||
end
|
end
|
||||||
assert_match(/#{__("wrong number of arguments")}/, exception.message)
|
assert_match(/#{__("wrong (number|#) of arguments")}/, exception.message)
|
||||||
end
|
end
|
||||||
|
|
||||||
# ------------------------------------------------------------------
|
# ------------------------------------------------------------------
|
||||||
@@ -142,7 +142,7 @@ class AboutMethods < EdgeCase::Koan
|
|||||||
"tail"
|
"tail"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_calling_methods_in_other_objects_require_explicit_receiver
|
def test_calling_methods_in_other_objects_require_explicit_receiver
|
||||||
rover = Dog.new
|
rover = Dog.new
|
||||||
assert_equal __("Fido"), rover.name
|
assert_equal __("Fido"), rover.name
|
||||||
|
|||||||
@@ -25,15 +25,21 @@ class AboutSymbols < EdgeCase::Koan
|
|||||||
|
|
||||||
def test_method_names_become_symbols
|
def test_method_names_become_symbols
|
||||||
all_symbols = Symbol.all_symbols
|
all_symbols = Symbol.all_symbols
|
||||||
|
assert_equal __(true), all_symbols.include?(:test_method_names_become_symbols)
|
||||||
assert_equal __(true), all_symbols.include?(:test_method_names_are_symbols)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
RubyConstant = "What is the sound of one hand clapping?"
|
# THINK ABOUT IT:
|
||||||
def test_constants_become_symbols
|
#
|
||||||
all_symbols = Symbol.all_symbols
|
# Why do we capture the list of symbols before we check for the
|
||||||
|
# method name?
|
||||||
|
|
||||||
assert_equal true, all_symbols.include?(__(:RubyConstant))
|
in_ruby_version("mri") do
|
||||||
|
RubyConstant = "What is the sound of one hand clapping?"
|
||||||
|
def test_constants_become_symbols
|
||||||
|
all_symbols = Symbol.all_symbols
|
||||||
|
|
||||||
|
assert_equal __(true), all_symbols.include?(__(:RubyConstant))
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_symbols_can_be_made_from_strings
|
def test_symbols_can_be_made_from_strings
|
||||||
@@ -47,6 +53,13 @@ class AboutSymbols < EdgeCase::Koan
|
|||||||
assert_equal symbol, __("cats and dogs").to_sym
|
assert_equal symbol, __("cats and dogs").to_sym
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_symbols_with_spaces_can_be_built
|
||||||
|
value = "and"
|
||||||
|
symbol = :"cats #{value} dogs"
|
||||||
|
|
||||||
|
assert_equal symbol, __("cats and dogs").to_sym
|
||||||
|
end
|
||||||
|
|
||||||
def test_to_s_is_called_on_interpolated_symbols
|
def test_to_s_is_called_on_interpolated_symbols
|
||||||
symbol = :cats
|
symbol = :cats
|
||||||
string = "It is raining #{symbol} and dogs."
|
string = "It is raining #{symbol} and dogs."
|
||||||
@@ -65,13 +78,23 @@ class AboutSymbols < EdgeCase::Koan
|
|||||||
assert_equal __(false), symbol.respond_to?(:each_char)
|
assert_equal __(false), symbol.respond_to?(:each_char)
|
||||||
assert_equal __(false), symbol.respond_to?(:reverse)
|
assert_equal __(false), symbol.respond_to?(:reverse)
|
||||||
end
|
end
|
||||||
|
|
||||||
# It's important to realize that symbols are not "immutable
|
# It's important to realize that symbols are not "immutable
|
||||||
# strings", though they are immutable. None of the
|
# strings", though they are immutable. None of the
|
||||||
# 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 father down the path
|
# Exceptions will be pondered further father down the path
|
||||||
assert_raise(___(NoMethodError)) do
|
assert_raise(___(NoMethodError)) do
|
||||||
:cats + :dogs
|
:cats + :dogs
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_symbols_can_be_dynamically_created
|
||||||
|
assert_equal __(:catsdogs), ("cats" + "dogs").to_sym
|
||||||
|
end
|
||||||
|
|
||||||
|
# THINK ABOUT IT:
|
||||||
|
#
|
||||||
|
# Why is it not a good idea to dynamically create a lot of symbols?
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -6,8 +6,14 @@ require 'test/unit/assertions'
|
|||||||
class FillMeInError < StandardError
|
class FillMeInError < StandardError
|
||||||
end
|
end
|
||||||
|
|
||||||
def in_ruby_version(version)
|
def ruby_version?(version)
|
||||||
yield if RUBY_VERSION =~ /^#{version}/
|
RUBY_VERSION =~ /^#{version}/ ||
|
||||||
|
(version == 'jruby' && defined?(JRUBY_VERSION)) ||
|
||||||
|
(version == 'mri' && ! defined?(JRUBY_VERSION))
|
||||||
|
end
|
||||||
|
|
||||||
|
def in_ruby_version(*versions)
|
||||||
|
yield if versions.any? { |v| ruby_version?(v) }
|
||||||
end
|
end
|
||||||
|
|
||||||
def __(value="FILL ME IN", value19=:mu)
|
def __(value="FILL ME IN", value19=:mu)
|
||||||
@@ -162,7 +168,7 @@ module EdgeCase
|
|||||||
print Color.green('.'*happy_steps)
|
print Color.green('.'*happy_steps)
|
||||||
if failed?
|
if failed?
|
||||||
print Color.red('X')
|
print Color.red('X')
|
||||||
print Color.blue('_'*(bar_width-1-happy_steps))
|
print Color.cyan('_'*(bar_width-1-happy_steps))
|
||||||
end
|
end
|
||||||
print Color.green(']')
|
print Color.green(']')
|
||||||
print " #{pass_count}/#{total_tests}"
|
print " #{pass_count}/#{total_tests}"
|
||||||
@@ -211,13 +217,13 @@ ENDTEXT
|
|||||||
def encourage
|
def encourage
|
||||||
puts
|
puts
|
||||||
puts "The Master says:"
|
puts "The Master says:"
|
||||||
puts Color.blue(" You have not yet reached enlightenment.")
|
puts Color.cyan(" You have not yet reached enlightenment.")
|
||||||
if ((recents = progress.last(5)) && recents.size == 5 && recents.uniq.size == 1)
|
if ((recents = progress.last(5)) && recents.size == 5 && recents.uniq.size == 1)
|
||||||
puts Color.blue(" I sense frustration. Do not be afraid to ask for help.")
|
puts Color.cyan(" I sense frustration. Do not be afraid to ask for help.")
|
||||||
elsif progress.last(2).size == 2 && progress.last(2).uniq.size == 1
|
elsif progress.last(2).size == 2 && progress.last(2).uniq.size == 1
|
||||||
puts Color.blue(" Do not lose hope.")
|
puts Color.cyan(" Do not lose hope.")
|
||||||
elsif progress.last.to_i > 0
|
elsif progress.last.to_i > 0
|
||||||
puts Color.blue(" You are progressing. Excellent. #{progress.last} completed.")
|
puts Color.cyan(" You are progressing. Excellent. #{progress.last} completed.")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -242,12 +248,13 @@ ENDTEXT
|
|||||||
first_line = false
|
first_line = false
|
||||||
Color.red(t)
|
Color.red(t)
|
||||||
else
|
else
|
||||||
Color.blue(t)
|
Color.cyan(t)
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
def indent(text)
|
def indent(text)
|
||||||
|
text = text.split(/\n/) if text.is_a?(String)
|
||||||
text.collect{|t| " #{t}"}
|
text.collect{|t| " #{t}"}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user