-require 'edgecase'
+require File.expand_path(File.dirname(__FILE__) + '/edgecase')
class AboutStrings < EdgeCase::Koan
def test_double_quoted_strings_are_strings
assert_equal __, string[7..9]
end
- def test_you_can_get_a_single_character_from_a_string
- string = "Bacon, lettuce and tomato"
- assert_equal __, string[1]
+ in_ruby_version("1.8") do
+ def test_in_ruby_1_8_single_characters_are_represented_by_integers
+ assert_equal __, ?a
+ assert_equal __, ?a == 97
+
+ assert_equal __, ?b == (?a + 1)
+ end
+ end
+
+ in_ruby_version("1.9") do
+ def test_in_ruby_1_9_single_characters_are_represented_by_strings
+ assert_equal __, ?a
+ assert_equal __, ?a == 97
+ end
+ end
- # Surprised?
+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_single_characters_are_represented_by_integers
- assert_equal __, ?a
- assert_equal __, ?a == 97
+ def test_in_ruby_1_9_you_can_get_a_single_character_from_a_string
+ string = "Bacon, lettuce and tomato"
+ assert_equal "__", string[1]
- assert_equal __, ?b == (?a + 1)
+ # Surprised?
+ end
end
+
def test_strings_can_be_split
string = "Sausage Egg Cheese"
words = string.split
assert_equal [__, __, __, __], words
# NOTE: Patterns are formed from Regular Expressions. Ruby has a
- # very powerful Regular Expression library. Unfortunately, time
- # does not permit us to explore it in detail in Ruby 101.
+ # very powerful Regular Expression library. We will become
+ # enlightened about them soon.
end
def test_strings_can_be_joined
words = ["Now", "is", "the", "time"]
assert_equal __, words.join(" ")
end
+
+ def test_strings_are_unique_objects
+ a = "a string"
+ b = "a string"
+
+ assert_equal __, a == b
+ assert_equal __, a.object_id == b.object_id
+ end
end