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.29.0 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_ruby_utils-0.103.0 lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_tools-0.28.0 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_tools-0.27.2 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_ruby_utils-0.102.1 lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_tools-0.27.1 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_tools-0.27.0 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_tools-0.26.0 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_ruby_utils-0.102.0 lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_tools-0.25.1 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_ruby_utils-0.101.0 lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_tools-0.25.0 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_tools-0.24.1 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_ruby_utils-0.100.0 lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_tools-0.24.0 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_ruby_utils-0.99.0 lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_tools-0.23.0 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_tools-0.22.0 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_tools-0.21.0 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/regexp/if_match.rb
eac_tools-0.20.0 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/regexp/if_match.rb