Project

General

Profile

Feature #20885

Updated by Dan0042 (Daniel DeLorme) 9 days ago

I would like a variation of sub/gsub that returns a new string if there is a match, or nil otherwise. 

 This can be currently done with `str.dup.gsub!(...)` at the mere cost of 2 unnecessary extra allocations due to String#dup 

 TBH I'm not sure we need yet another gsub method, but what gets me here is that `gsub?` is the primitive operation from which `gsub` and `gsub!` are made. Internally, these two methods work like this: 

 ```ruby 
 def gsub!(...) 
   modified = gsub?(...) and replace(modified) 
 end 

 def gsub(...) 
   gsub?(...) or dup 
 end 
 ``` 

 We can efficiently derive these two methods from `gsub?`, but we cannot efficiently implement `gsub?` in ruby, so it feels regrettable to me that `gsub?` is not available as a built-in. 

 Use cases include: 

 ```ruby 
 # ensure #ensure gsub has modified the string 
 newstr = str.gsub?(rx,repl) or raise "str did not contain expected value" 

 # take #take an action if the string is modified 
 if str = obj.title.gsub?(rx,repl) 
   obj.title = str 
   obj.save 
 end 

 # avoid allocating a new string if there is no change 
 str = str.gsub?(rx,repl) || str 
 ```

Back