Sha256: fa6e1bd2e53b0a87253df1a0559d54e46add422912df33cfe5deaf527fb84e6f

Contents?: true

Size: 850 Bytes

Versions: 2

Compression:

Stored size: 850 Bytes

Contents

# frozen_string_literal: true

# file: lib/base_process.rb

class BaseProcess
  attr_reader :key

  def initialize(key)
    @key = key
  end

  def deep_match(input, predicate)
    matches = []

    # If the current input is a Hash, iterate over each key-value pair
    if input.is_a?(Hash)

      input.each do |key, value|

        # If the value matches the predicate, add it to matches
        if predicate.call(key, value)
          matches << value
        end

        # Continue searching deeper within the value
        matches.concat(deep_match(value, predicate))
      end

    # If the input is an Array, iterate over each item
    elsif input.is_a?(Array)

      input.each do |item|

        # Continue searching within each item of the array
        matches.concat(deep_match(item, predicate))
      end
    end

    matches
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
klue-langcraft-0.3.0 lib/base_process.rb
klue-langcraft-0.2.0 lib/base_process.rb