Method: String#change

Defined in:
lib/mini_sanity/change.rb

#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