mirror of
https://github.com/edgecase/ruby_koans.git
synced 2026-04-15 07:23:19 -04:00
Clean up about_koans and add another example
This commit is contained in:
@@ -6,24 +6,18 @@ class AboutSymbols < EdgeCase::Koan
|
|||||||
assert_equal __, symbol.is_a?(Symbol)
|
assert_equal __, symbol.is_a?(Symbol)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_symbols_are_not_strings
|
def test_symbols_can_be_compared
|
||||||
symbol = :ruby
|
symbol1 = :a_symbol
|
||||||
assert_equal __, symbol.is_a?(String)
|
symbol2 = :a_symbol
|
||||||
assert_equal __, symbol.eql?("ruby")
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_symbols_have_unique_identity
|
|
||||||
symbol1 = :identity
|
|
||||||
symbol2 = :identity
|
|
||||||
symbol3 = :something_else
|
symbol3 = :something_else
|
||||||
|
|
||||||
assert symbol1 == __
|
assert symbol1 == __
|
||||||
assert symbol1 != __
|
assert symbol1 != __
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_identical_symbols_are_represented_by_a_single_internal_object
|
def test_identical_symbols_are_a_single_internal_object
|
||||||
symbol1 = :identity
|
symbol1 = :a_symbol
|
||||||
symbol2 = :identity
|
symbol2 = :a_symbol
|
||||||
|
|
||||||
assert symbol1.equal?(__)
|
assert symbol1.equal?(__)
|
||||||
assert_equal __, symbol2.object_id
|
assert_equal __, symbol2.object_id
|
||||||
@@ -35,7 +29,7 @@ class AboutSymbols < EdgeCase::Koan
|
|||||||
assert_equal __, all_symbols.include?(:test_method_names_are_symbols)
|
assert_equal __, all_symbols.include?(:test_method_names_are_symbols)
|
||||||
end
|
end
|
||||||
|
|
||||||
RubyConstant = "This string is assigned to a constant."
|
RubyConstant = "What is the sound of one hand clapping?"
|
||||||
def test_constants_become_symbols
|
def test_constants_become_symbols
|
||||||
all_symbols = Symbol.all_symbols
|
all_symbols = Symbol.all_symbols
|
||||||
|
|
||||||
@@ -53,14 +47,29 @@ class AboutSymbols < EdgeCase::Koan
|
|||||||
assert_equal symbol, __.to_sym
|
assert_equal symbol, __.to_sym
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_interpolated_symbols_become_strings
|
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."
|
||||||
|
|
||||||
assert_equal __, string
|
assert_equal __, string
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_symbols_are_not_strings
|
||||||
|
symbol = :ruby
|
||||||
|
assert_equal __, symbol.is_a?(String)
|
||||||
|
assert_equal __, symbol.eql?("ruby")
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_symbols_do_not_have_string_methods
|
||||||
|
symbol = :not_a_string
|
||||||
|
assert_equal __, symbol.respond_to?(:each_char)
|
||||||
|
assert_equal __, symbol.respond_to?(:reverse)
|
||||||
|
end
|
||||||
|
# It's important to realize that symbols are not "immutable
|
||||||
|
# strings", though they are immutable. None of the
|
||||||
|
# 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
|
||||||
assert_raise(__) do
|
assert_raise(__) do
|
||||||
:cats + :dogs
|
:cats + :dogs
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -6,24 +6,18 @@ class AboutSymbols < EdgeCase::Koan
|
|||||||
assert_equal __(true), symbol.is_a?(Symbol)
|
assert_equal __(true), symbol.is_a?(Symbol)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_symbols_are_not_strings
|
def test_symbols_can_be_compared
|
||||||
symbol = :ruby
|
symbol1 = :a_symbol
|
||||||
assert_equal __(false), symbol.is_a?(String)
|
symbol2 = :a_symbol
|
||||||
assert_equal __(false), symbol.eql?("ruby")
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_symbols_have_unique_identity
|
|
||||||
symbol1 = :identity
|
|
||||||
symbol2 = :identity
|
|
||||||
symbol3 = :something_else
|
symbol3 = :something_else
|
||||||
|
|
||||||
assert symbol1 == __(symbol2)
|
assert symbol1 == __(symbol2)
|
||||||
assert symbol1 != __(symbol2)
|
assert symbol1 != __(symbol3)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_identical_symbols_are_represented_by_a_single_internal_object
|
def test_identical_symbols_are_a_single_internal_object
|
||||||
symbol1 = :identity
|
symbol1 = :a_symbol
|
||||||
symbol2 = :identity
|
symbol2 = :a_symbol
|
||||||
|
|
||||||
assert symbol1.equal?(__(symbol2))
|
assert symbol1.equal?(__(symbol2))
|
||||||
assert_equal __(symbol1.object_id), symbol2.object_id
|
assert_equal __(symbol1.object_id), symbol2.object_id
|
||||||
@@ -35,7 +29,7 @@ class AboutSymbols < EdgeCase::Koan
|
|||||||
assert_equal __(true), all_symbols.include?(:test_method_names_are_symbols)
|
assert_equal __(true), all_symbols.include?(:test_method_names_are_symbols)
|
||||||
end
|
end
|
||||||
|
|
||||||
RubyConstant = "This string is assigned to a constant."
|
RubyConstant = "What is the sound of one hand clapping?"
|
||||||
def test_constants_become_symbols
|
def test_constants_become_symbols
|
||||||
all_symbols = Symbol.all_symbols
|
all_symbols = Symbol.all_symbols
|
||||||
|
|
||||||
@@ -53,14 +47,29 @@ class AboutSymbols < EdgeCase::Koan
|
|||||||
assert_equal symbol, __("cats and dogs").to_sym
|
assert_equal symbol, __("cats and dogs").to_sym
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_interpolated_symbols_become_strings
|
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."
|
||||||
|
|
||||||
assert_equal __('It is raining cats and dogs.'), string
|
assert_equal __('It is raining cats and dogs.'), string
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_symbols_are_not_strings
|
||||||
|
symbol = :ruby
|
||||||
|
assert_equal __(false), symbol.is_a?(String)
|
||||||
|
assert_equal __(false), symbol.eql?("ruby")
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_symbols_do_not_have_string_methods
|
||||||
|
symbol = :not_a_string
|
||||||
|
assert_equal __(false), symbol.respond_to?(:each_char)
|
||||||
|
assert_equal __(false), symbol.respond_to?(:reverse)
|
||||||
|
end
|
||||||
|
# It's important to realize that symbols are not "immutable
|
||||||
|
# strings", though they are immutable. None of the
|
||||||
|
# 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
|
||||||
assert_raise(__(NoMethodError)) do
|
assert_raise(__(NoMethodError)) do
|
||||||
:cats + :dogs
|
:cats + :dogs
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user