Sha256: e9a9f32d3fd7b5394f8c497c6214eafb96badb5c93c93749d7aa42f95698bce4

Contents?: true

Size: 563 Bytes

Versions: 5

Compression:

Stored size: 563 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) # rubocop:disable Style/OptionalBooleanParameter
    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

5 entries across 5 versions & 2 rubygems

Version Path
eac_ruby_utils-0.124.0 lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_tools-0.97.2 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_ruby_utils-0.123.0 lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_ruby_utils-0.121.0 lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_ruby_utils-0.120.0 lib/eac_ruby_utils/patches/regexp/if_match.rb