Sha256: c53986d0c8f9b2d00c911fbe196545784e926630f4025e6920c10ee71de48285

Contents?: true

Size: 802 Bytes

Versions: 5

Compression:

Stored size: 802 Bytes

Contents

# -*- encoding: utf-8 -*-

class String

  # Public: Transform itself in a Regexp.
  #
  # Returns a Regexp if successful, nil otherwise.
  # ---
  # Credit to Josh Cheek: http://www.ruby-forum.com/topic/193809
  #
  # Note: there is also a gem:
  #   https://github.com/seamusabshere/to_regexp 
  #   Could be useful if I later want to parse things like
  #     $ ackr %r{regexp}
  #   or if bugs are discovered in the following code.
  def to_regexp
    return nil unless self.strip.match(/\A\/(.*)\/(.*)\Z/mx)
    regexp , flags = $1 , $2
    return nil if !regexp || flags =~ /[^xim]/m

    x = /x/.match(flags) && Regexp::EXTENDED
    i = /i/.match(flags) && Regexp::IGNORECASE
    m = /m/.match(flags) && Regexp::MULTILINE

    Regexp.new regexp , [x,i,m].inject(0){|a,f| f ? a+f : a }
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ackr-0.3.0 lib/ackr/extensions/string.rb
ackr-0.2.4 lib/ackr/extensions/string.rb
ackr-0.2.3 lib/ackr/extensions/string.rb
ackr-0.2.2 lib/ackr/extensions/string.rb
ackr-0.2 lib/ackr/extensions/string.rb