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.76.1 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_tools-0.76.0 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_tools-0.75.2 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_ruby_utils-0.119.2 lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_tools-0.75.1 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_tools-0.75.0 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_ruby_utils-0.119.1 lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_tools-0.74.1 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_tools-0.74.0 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_tools-0.73.0 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_ruby_utils-0.119.0 lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_tools-0.72.0 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_ruby_utils-0.118.1 lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_ruby_utils-0.118.0 lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_tools-0.70.1 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_tools-0.70.0 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_tools-0.69.1 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_ruby_utils-0.117.1 lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_tools-0.69.0 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_tools-0.68.0 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/regexp/if_match.rb