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.67.1 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_ruby_utils-0.117.0 lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_tools-0.67.0 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_tools-0.66.0 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_tools-0.65.1 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_ruby_utils-0.116.0 lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_ruby_utils-0.115.0 lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_tools-0.65.0 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_ruby_utils-0.114.0 lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_tools-0.64.0 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_ruby_utils-0.113.0 lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_tools-0.63.0 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_tools-0.62.1 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_tools-0.62.0 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_tools-0.61.1 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_tools-0.61.0 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_tools-0.60.3 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_tools-0.60.2 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_tools-0.60.1 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_tools-0.60.0 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/regexp/if_match.rb