Feature #13048

Better way to do"some string"))

Added by justcolin (Colin Fulton) over 2 years ago. Updated over 2 years ago.

Target version:


It is relatively common to call"some string")) or"some string")) in Ruby (a quick search turns up over half a million matches in Github). However, these seems rather cumbersome. It would be nice to have a class instance method on Regexp and/or an instance method on String to go directly to an escaped regular expression.

Personally I'd rather see #to_regexp added to String. This would not overlap with the functionality that"string") provides since that doesn't quote the String.

It would also be good to see 9846 addressed along with this request so one could more easily duck type a method to accept both Regexp and String arguments.



Updated by nobu (Nobuyoshi Nakada) over 2 years ago

A known hack, Regexp.union("some string").

Updated by justcolin (Colin Fulton) over 2 years ago

It would be nice to have a real/more-idiomatic way instead of a hack, and that hack doesn't support duck typing, while adding #to_regexp to String does.

Updated by shyouhei (Shyouhei Urabe) over 2 years ago

We looked at this issue at yesterday's developer meeting.

While looking at " Regexp.escape" search result of GitHub, we felt in common that some (if not most) of those usage are questionable. For instance multiple copies of String#to_regexp implementations specify Regexp::IGNORECASE along with the escaped text; which does not immediately make sense to me. There are other use cases in tests. Because they are written in their own DSLs, why not have custom matcher that escape strings appropriately.

Nobody was against adding this functionality but I think more closer look at use cases would give us better insight of the needs.

Also available in: Atom PDF