mirror of
https://github.com/edgecase/ruby_koans.git
synced 2026-04-15 07:23:19 -04:00
Updated koans from new regex section.
This commit is contained in:
@@ -1,34 +1,35 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
require File.expand_path(File.dirname(__FILE__) + '/edgecase')
|
require File.expand_path(File.dirname(__FILE__) + '/edgecase')
|
||||||
|
|
||||||
class AboutRegularExpressions < EdgeCase::Koan
|
class AboutRegularExpressions < EdgeCase::Koan
|
||||||
def test_a_pattern_is_a_regular_expression
|
def test_a_pattern_is_a_regular_expression
|
||||||
assert_equal Regexp, /pattern/.class
|
assert_equal Regexp, /pattern/.class
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_a_regexp_can_search_a_string_for_matching_content
|
def test_a_regexp_can_search_a_string_for_matching_content
|
||||||
assert_equal "match", "some matching content"[/match/]
|
assert_equal "match", "some matching content"[/match/]
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_a_failed_match_returns_nil
|
def test_a_failed_match_returns_nil
|
||||||
assert_equal __, "some matching content"[/missing/]
|
assert_equal __, "some matching content"[/missing/]
|
||||||
end
|
end
|
||||||
|
|
||||||
# ------------------------------------------------------------------
|
# ------------------------------------------------------------------
|
||||||
|
|
||||||
def test_question_mark_means_optional
|
def test_question_mark_means_optional
|
||||||
assert_equal __, "abbcccddddeeeee"[/ab?/]
|
assert_equal __, "abbcccddddeeeee"[/ab?/]
|
||||||
assert_equal __, "abbcccddddeeeee"[/az?/]
|
assert_equal __, "abbcccddddeeeee"[/az?/]
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_plus_means_one_or_more
|
def test_plus_means_one_or_more
|
||||||
assert_equal __, "abbcccddddeeeee"[/bc+/]
|
assert_equal __, "abbcccddddeeeee"[/bc+/]
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_asterisk_means_zero_or_more
|
def test_asterisk_means_zero_or_more
|
||||||
assert_equal __, "abbcccddddeeeee"[/ab*/]
|
assert_equal __, "abbcccddddeeeee"[/ab*/]
|
||||||
assert_equal __, "abbcccddddeeeee"[/az*/]
|
assert_equal __, "abbcccddddeeeee"[/az*/]
|
||||||
assert_equal __, "abbcccddddeeeee"[/z*/]
|
assert_equal __, "abbcccddddeeeee"[/z*/]
|
||||||
|
|
||||||
# THINK ABOUT IT:
|
# THINK ABOUT IT:
|
||||||
#
|
#
|
||||||
# When would * fail to match?
|
# When would * fail to match?
|
||||||
@@ -36,101 +37,101 @@ class AboutRegularExpressions < EdgeCase::Koan
|
|||||||
|
|
||||||
# THINK ABOUT IT:
|
# THINK ABOUT IT:
|
||||||
#
|
#
|
||||||
# We say that the repition operators above are "greedy."
|
# We say that the repetition operators above are "greedy."
|
||||||
#
|
#
|
||||||
# Why?
|
# Why?
|
||||||
|
|
||||||
# ------------------------------------------------------------------
|
# ------------------------------------------------------------------
|
||||||
|
|
||||||
def test_the_left_most_match_wins
|
def test_the_left_most_match_wins
|
||||||
assert_equal __, "abbccc az"[/az*/]
|
assert_equal __, "abbccc az"[/az*/]
|
||||||
end
|
end
|
||||||
|
|
||||||
# ------------------------------------------------------------------
|
# ------------------------------------------------------------------
|
||||||
|
|
||||||
def test_character_classes_give_options_for_a_character
|
def test_character_classes_give_options_for_a_character
|
||||||
animals = ["cat", "bat", "rat", "zat"]
|
animals = ["cat", "bat", "rat", "zat"]
|
||||||
assert_equal __, animals.select { |a| a[/[cbr]at/] }
|
assert_equal __, animals.select { |a| a[/[cbr]at/] }
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_slash_d_is_a_shortcut_for_a_digit_character_class
|
def test_slash_d_is_a_shortcut_for_a_digit_character_class
|
||||||
assert_equal __, "the number is 42"[/[0123456789]+/]
|
assert_equal __, "the number is 42"[/[0123456789]+/]
|
||||||
assert_equal __, "the number is 42"[/\d+/]
|
assert_equal __, "the number is 42"[/\d+/]
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_character_classes_can_include_ranges
|
def test_character_classes_can_include_ranges
|
||||||
assert_equal __, "the number is 42"[/[0-9]+/]
|
assert_equal __, "the number is 42"[/[0-9]+/]
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_slash_s_is_a_shortcut_for_a_whitespace_character_class
|
def test_slash_s_is_a_shortcut_for_a_whitespace_character_class
|
||||||
assert_equal __, "space: \t\n"[/\s+/]
|
assert_equal __, "space: \t\n"[/\s+/]
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_slash_w_is_a_shortcut_for_a_word_character_class
|
def test_slash_w_is_a_shortcut_for_a_word_character_class
|
||||||
# NOTE: This is more like how a programmer might define a word.
|
# NOTE: This is more like how a programmer might define a word.
|
||||||
assert_equal __, "variable_1 = 42"[/[a-zA-Z0-9_]+/]
|
assert_equal __, "variable_1 = 42"[/[a-zA-Z0-9_]+/]
|
||||||
assert_equal __, "variable_1 = 42"[/\w+/]
|
assert_equal __, "variable_1 = 42"[/\w+/]
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_period_is_a_shortcut_for_any_non_newline_character
|
def test_period_is_a_shortcut_for_any_non_newline_character
|
||||||
assert_equal __, "abc\n123"[/a.+/]
|
assert_equal __, "abc\n123"[/a.+/]
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_a_character_class_can_be_negated
|
def test_a_character_class_can_be_negated
|
||||||
assert_equal __, "the number is 42"[/[^0-9]+/]
|
assert_equal __, "the number is 42"[/[^0-9]+/]
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_shortcut_character_classes_are_negated_with_capitals
|
def test_shortcut_character_classes_are_negated_with_capitals
|
||||||
assert_equal __, "the number is 42"[/\D+/]
|
assert_equal __, "the number is 42"[/\D+/]
|
||||||
assert_equal __, "space: \t\n"[/\S+/]
|
assert_equal __, "space: \t\n"[/\S+/]
|
||||||
assert_equal __, "variable_1 = 42"[/\W+/]
|
assert_equal __, "variable_1 = 42"[/\W+/]
|
||||||
end
|
end
|
||||||
|
|
||||||
# ------------------------------------------------------------------
|
# ------------------------------------------------------------------
|
||||||
|
|
||||||
def test_slash_a_anchors_to_the_start_of_the_string
|
def test_slash_a_anchors_to_the_start_of_the_string
|
||||||
assert_equal __, "start end"[/\Astart/]
|
assert_equal __, "start end"[/\Astart/]
|
||||||
assert_equal __, "start end"[/\Aend/]
|
assert_equal __, "start end"[/\Aend/]
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_slash_z_anchors_to_the_end_of_the_string
|
def test_slash_z_anchors_to_the_end_of_the_string
|
||||||
assert_equal __, "start end"[/end\z/]
|
assert_equal __, "start end"[/end\z/]
|
||||||
assert_equal __, "start end"[/start\z/]
|
assert_equal __, "start end"[/start\z/]
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_caret_anchors_to_the_start_of_lines
|
def test_caret_anchors_to_the_start_of_lines
|
||||||
assert_equal __, "num 42\n2 lines"[/^\d+/]
|
assert_equal __, "num 42\n2 lines"[/^\d+/]
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_dollar_sign_anchors_to_the_end_of_lines
|
def test_dollar_sign_anchors_to_the_end_of_lines
|
||||||
assert_equal __, "2 lines\nnum 42"[/\d+$/]
|
assert_equal __, "2 lines\nnum 42"[/\d+$/]
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_slash_b_anchors_to_a_word_boundary
|
def test_slash_b_anchors_to_a_word_boundary
|
||||||
assert_equal __, "bovine vines"[/\bvine./]
|
assert_equal __, "bovine vines"[/\bvine./]
|
||||||
end
|
end
|
||||||
|
|
||||||
# ------------------------------------------------------------------
|
# ------------------------------------------------------------------
|
||||||
|
|
||||||
def test_parentheses_group_contents
|
def test_parentheses_group_contents
|
||||||
assert_equal __, "ahahaha"[/(ha)+/]
|
assert_equal __, "ahahaha"[/(ha)+/]
|
||||||
end
|
end
|
||||||
|
|
||||||
# ------------------------------------------------------------------
|
# ------------------------------------------------------------------
|
||||||
|
|
||||||
def test_parentheses_also_capture_matched_content_by_number
|
def test_parentheses_also_capture_matched_content_by_number
|
||||||
assert_equal __, "Gray, James"[/(\w+), (\w+)/, 1]
|
assert_equal __, "Gray, James"[/(\w+), (\w+)/, 1]
|
||||||
assert_equal __, "Gray, James"[/(\w+), (\w+)/, 2]
|
assert_equal __, "Gray, James"[/(\w+), (\w+)/, 2]
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_variables_can_also_be_used_to_access_captures
|
def test_variables_can_also_be_used_to_access_captures
|
||||||
assert_equal __, "Name: Gray, James"[/(\w+), (\w+)/]
|
assert_equal __, "Name: Gray, James"[/(\w+), (\w+)/]
|
||||||
assert_equal __, $1
|
assert_equal __, $1
|
||||||
assert_equal __, $2
|
assert_equal __, $2
|
||||||
end
|
end
|
||||||
|
|
||||||
# ------------------------------------------------------------------
|
# ------------------------------------------------------------------
|
||||||
|
|
||||||
def test_a_vertical_pipe_means_or
|
def test_a_vertical_pipe_means_or
|
||||||
grays = /(James|Dana|Summer) Gray/
|
grays = /(James|Dana|Summer) Gray/
|
||||||
assert_equal __, "James Gray"[grays]
|
assert_equal __, "James Gray"[grays]
|
||||||
@@ -141,17 +142,17 @@ class AboutRegularExpressions < EdgeCase::Koan
|
|||||||
# THINK ABOUT IT:
|
# THINK ABOUT IT:
|
||||||
#
|
#
|
||||||
# Explain the difference between a character class ([…]) and alternation (|).
|
# Explain the difference between a character class ([…]) and alternation (|).
|
||||||
|
|
||||||
# ------------------------------------------------------------------
|
# ------------------------------------------------------------------
|
||||||
|
|
||||||
def test_scan_is_like_find_all
|
def test_scan_is_like_find_all
|
||||||
assert_equal __, "one two-three".scan(/\w+/)
|
assert_equal __, "one two-three".scan(/\w+/)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_sub_is_like_find_and_replace
|
def test_sub_is_like_find_and_replace
|
||||||
assert_equal __, "one two-three".sub(/(t\w*)/) { $1[0, 1] }
|
assert_equal __, "one two-three".sub(/(t\w*)/) { $1[0, 1] }
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_gsub_is_like_find_and_replace_all
|
def test_gsub_is_like_find_and_replace_all
|
||||||
assert_equal __, "one two-three".gsub(/(t\w*)/) { $1[0, 1] }
|
assert_equal __, "one two-three".gsub(/(t\w*)/) { $1[0, 1] }
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user