Skip to content

Commit

Permalink
working
Browse files Browse the repository at this point in the history
  • Loading branch information
Danish Khan committed Mar 7, 2011
1 parent f19f043 commit d10614c
Showing 1 changed file with 25 additions and 25 deletions.
50 changes: 25 additions & 25 deletions koans/about_strings.rb
Expand Up @@ -68,31 +68,31 @@ def test_plus_equals_will_concatenate_to_the_end_of_a_string
hi = "Hello, "
there = "World"
hi += there
assert_equal __, hi
assert_equal "Hello, World", hi
end

def test_plus_equals_also_will_leave_the_original_string_unmodified
original_string = "Hello, "
hi = original_string
there = "World"
hi += there
assert_equal __, original_string
assert_equal "Hello, ", original_string
end

def test_the_shovel_operator_will_also_append_content_to_a_string
hi = "Hello, "
there = "World"
hi << there
assert_equal __, hi
assert_equal __, there
assert_equal "Hello, World", hi
assert_equal "World", there
end

def test_the_shovel_operator_modifies_the_original_string
original_string = "Hello, "
hi = original_string
there = "World"
hi << there
assert_equal __, original_string
assert_equal "Hello, World", original_string

# THINK ABOUT IT:
#
Expand All @@ -102,76 +102,76 @@ def test_the_shovel_operator_modifies_the_original_string

def test_double_quoted_string_interpret_escape_characters
string = "\n"
assert_equal __, string.size
assert_equal 1, string.size
end

def test_single_quoted_string_do_not_interpret_escape_characters
string = '\n'
assert_equal __, string.size
assert_equal 2, string.size
end

def test_single_quotes_sometimes_interpret_escape_characters
string = '\\\''
assert_equal __, string.size
assert_equal __, string
assert_equal 2, string.size
assert_equal "\\'", string
end

def test_double_quoted_strings_interpolate_variables
value = 123
string = "The value is #{value}"
assert_equal __, string
assert_equal "The value is 123", string
end

def test_single_quoted_strings_do_not_interpolate
value = 123
string = 'The value is #{value}'
assert_equal __, string
assert_equal "The value is \#{value}", string
end

def test_any_ruby_expression_may_be_interpolated
string = "The square root of 5 is #{Math.sqrt(5)}"
assert_equal __, string
assert_equal "The square root of 5 is 2.23606797749979", string
end

def test_you_can_get_a_substring_from_a_string
string = "Bacon, lettuce and tomato"
assert_equal __, string[7,3]
assert_equal __, string[7..9]
assert_equal "let", string[7,3]
assert_equal "let", string[7..9]
end

def test_you_can_get_a_single_character_from_a_string
string = "Bacon, lettuce and tomato"
assert_equal __, string[1]
assert_equal "a", string[1]

# Surprised?
end

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 97, ?a
assert_equal true, ?a == 97

assert_equal __, ?b == (?a + 1)
assert_equal true, ?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
assert_equal "a", ?a
assert_equal false, ?a == 97
end
end

def test_strings_can_be_split
string = "Sausage Egg Cheese"
words = string.split
assert_equal [__, __, __], words
assert_equal ["Sausage", "Egg", "Cheese"], words
end

def test_strings_can_be_split_with_different_patterns
string = "the:rain:in:spain"
words = string.split(/:/)
assert_equal [__, __, __, __], words
assert_equal ["the", "rain", "in", "spain"], words

# NOTE: Patterns are formed from Regular Expressions. Ruby has a
# very powerful Regular Expression library. We will become
Expand All @@ -180,14 +180,14 @@ def test_strings_can_be_split_with_different_patterns

def test_strings_can_be_joined
words = ["Now", "is", "the", "time"]
assert_equal __, words.join(" ")
assert_equal "Now is the time", 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
assert_equal true, a == b
assert_equal false, a.object_id == b.object_id
end
end

0 comments on commit d10614c

Please sign in to comment.