Class: String

Inherits:
Object show all
Defined in:
lib/mini_sanity/match.rb,
lib/mini_sanity/change.rb

Instance Method Summary collapse

Instance Method Details

#change(pattern, replacement) ⇒ String #change(pattern, hash) ⇒ String #change(pattern) {|match| ... } ⇒ String

Like String#sub, but raises an exception if no substitution is performed.

Examples:

info = "library: LIB_NAME\nlanguage: LIB_LANG\n"
info.change(/\bLIB_NAME\b/, "mini_sanity")  # == "library: mini_sanity\nlanguage: LIB_LANG\n"
info                                        # == "library: LIB_NAME\nlanguage: LIB_LANG\n"
info.change(/\bLIB_LANGUAGE\b/, "Ruby")     # raises exception

Overloads:

  • #change(pattern, replacement) ⇒ String

    Parameters:

    • pattern (Regexp, String)

      Pattern to search for

    • replacement (String)

      Replacement String (see String#sub documentation for more information)

    Returns:

    Raises:

  • #change(pattern, hash) ⇒ String

    Parameters:

    • pattern (Regexp, String)

      Pattern to search for

    • hash (Hash)

      Substitution Hash (see String#sub documentation for more information)

    Returns:

    Raises:

  • #change(pattern) {|match| ... } ⇒ String

    Parameters:

    Yield Parameters:

    • match (String)

      Matched String

    Yield Returns:

    • (String)

      Replacement String

    Returns:

    Raises:



103
104
105
# File 'lib/mini_sanity/change.rb', line 103

def change(pattern, replacement = nil, &block)
  self.dup.change!(pattern, replacement, &block)
end

#change!(pattern, replacement) ⇒ String #change!(pattern, hash) ⇒ String #change!(pattern) {|match| ... } ⇒ String

Like String#sub!, but raises an exception if no substitution is performed.

Examples:

info = "library: LIB_NAME\nlanguage: LIB_LANG\n"
info.change!(/\bLIB_NAME\b/, "mini_sanity")  # == "library: mini_sanity\nlanguage: LIB_LANG\n"
info                                         # == "library: mini_sanity\nlanguage: LIB_LANG\n"
info.change!(/\bLIB_LANGUAGE\b/, "Ruby")     # raises exception

Overloads:

  • #change!(pattern, replacement) ⇒ String

    Parameters:

    • pattern (Regexp, String)

      Pattern to search for

    • replacement (String)

      Replacement String (see String#sub documentation for more information)

    Returns:

    Raises:

  • #change!(pattern, hash) ⇒ String

    Parameters:

    • pattern (Regexp, String)

      Pattern to search for

    • hash (Hash)

      Substitution Hash (see String#sub documentation for more information)

    Returns:

    Raises:

  • #change!(pattern) {|match| ... } ⇒ String

    Parameters:

    Yield Parameters:

    • match (String)

      Matched String

    Yield Returns:

    • (String)

      Replacement String

    Returns:

    Raises:



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/mini_sanity/change.rb', line 46

def change!(pattern, replacement = nil, &block)
  result = if replacement
    self.sub!(pattern, replacement)
  else
    self.sub!(pattern, &block)
  end

  if !result
    raise MiniSanity::Error.new("String does not match pattern", {
      "String" => self.inspect,
      "Pattern" => pattern.inspect,
    })
  end

  result
end

#match!(pattern, pos = 0) ⇒ MatchData

Like String#match, but raises an exception if the match fails.

Examples:

"[email protected]".match!(/^([^@]+)@(.+)$/)  # === MatchData
"@user".match!(/^([^@]+)@(.+)$/)             # raises exception

Parameters:

  • pattern (Regexp)

    Pattern to search for

  • pos (Integer) (defaults to: 0)

    Position in the String to search from

Returns:

  • (MatchData)

Raises:



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/mini_sanity/match.rb', line 20

def match!(pattern, pos = 0)
  result = self.match(pattern, pos)

  if result.nil?
    raise MiniSanity::Error.new("String does not match pattern", {
      "String" => self.inspect,
      "Relevant portion (from position #{pos})" => (self[pos..].inspect if pos != 0),
      "Pattern" => pattern.inspect,
    })
  end

  result
end