Sha256: d0cbb5724d9eb71c99dfe59b8afd99e8c878f92c34ebcdbe9f13f9ed5d7d9b22

Contents?: true

Size: 514 Bytes

Versions: 373

Compression:

Stored size: 514 Bytes

Contents

# frozen_string_literal: true

class Regexp
  # If +self+ matches +string+ returns +block.call(Match result) or only Match result if block is
  # not provided.
  # If +self+ does not match +string+ raises a +ArgumentError+ if +required+ is truthy or return
  # +nil+ otherwise.
  def if_match(string, required = true, &block)
    m = match(string)
    if m
      block ? block.call(m) : m
    elsif required
      raise(::ArgumentError, "Pattern \"#{self}\" does not match string \"#{string}\"")
    end
  end
end

Version data entries

373 entries across 373 versions & 4 rubygems

Version Path
eac_tools-0.41.0 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_tools-0.40.0 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_tools-0.39.0 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_ruby_utils-0.106.1 lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_ruby_utils-0.106.0 lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_tools-0.38.0 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_tools-0.37.2 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_ruby_utils-0.105.0 lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_tools-0.37.1 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_tools-0.37.0 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_tools-0.36.1 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_tools-0.36.0 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_tools-0.35.0 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_tools-0.34.0 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_tools-0.33.0 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_tools-0.32.0 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_ruby_utils-0.104.0 lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_tools-0.31.1 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_tools-0.31.0 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_tools-0.30.0 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/regexp/if_match.rb