Class: String
- Defined in:
- lib/mini_sanity/match.rb,
lib/mini_sanity/change.rb
Instance Method Summary collapse
-
#change(pattern, replacement = nil, &block) ⇒ Object
Like String#sub, but raises an exception if no substitution is performed.
-
#change!(pattern, replacement = nil, &block) ⇒ Object
Like String#sub!, but raises an exception if no substitution is performed.
-
#match!(pattern, pos = 0) ⇒ MatchData
Like String#match, but raises an exception if the match fails.
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.
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.
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.
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 |