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.
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 |