Sha256: 9c441bf98b426632ee16fee841a64c60975f158972d39cb989c10ed4eab29052
Contents?: true
Size: 1.14 KB
Versions: 3
Compression:
Stored size: 1.14 KB
Contents
=to_regexp Basically a safe way to convert strings to regexps (with options). str = "/finalis(é)/im" old_way = eval(str) # not safe new_way = str.to_regexp # provided by this gem old_way == new_way # true You can also treat strings as literal regexps. These two are equivalent: '/foo/'.to_regexp #=> /foo/ 'foo'.to_regexp(:literal => true) #=> /foo/ If you need case insensitivity and you're using <tt>:literal</tt>, pass options like <tt>:ignore_case</tt>. These two are equivalent: '/foo/i'.to_regexp #=> /foo/i 'foo'.to_regexp(:literal => true, :ignore_case => true) #=> /foo/i You can get the options passed to <tt>Regexp.new</tt> with <tt>#as_regexp</tt>: '/foo/'.to_regexp == Regexp.new('/foo/'.as_regexp) # true Finally, you can be more lazy using <tt>:detect</tt>: 'foo'.to_regexp(detect: true) #=> /foo/ 'foo\b'.to_regexp(detect: true) #=> %r{foo\\b} '/foo\b/'.to_regexp(detect: true) #=> %r{foo\b} 'foo\b/'.to_regexp(detect: true) #=> %r{foo\\b/} Copyright 2012 Seamus Abshere
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
to_regexp-0.2.1 | README.rdoc |
to_regexp-0.2.0 | README.rdoc |
to_regexp-0.1.2 | README.rdoc |