<commit_before># Supports access to nested hashes with a string instead of multiple []'s.
# Inspired by <NAME>'s dot_notation gem at https://github.com/joshsz/dot_notation,

module TrickBag
module CollectionAccess

  module_function


  # Accesses a collection with a single string that represents n levels of depth.
  # See the spec file for examples, but here's one:
  #
  # h = { 'h' => ['a', 'b'] }
  # CollectionAccess.access(h, 'h.1') => 'b'
  #
  # If an error is raised while trying to access the value with a given key,
  # an error will be raised showing that error plus the context of which
  # key failed, as in:
  #
  # Error occurred processing key [x.1] in [x.1.2]: undefined method `[]' for nil:NilClass
  #
  # @param collection the collection to access
  # @param key_string_or_array the string or array representing the keys to use
  # @param separator the string to use to separate the keys, defaults to '.'
  def access(collection, key_string_or_array, separator = '.')

    to_integer = ->(object) do
      begin
        Integer(object)
      rescue
        raise "Key is not a number string: #{object}"
      end
    end

    get_key_array = -> do
      case key_string_or_array
        when Array
          key_string_or_array
        when String
          key_string_or_array.split(separator)
        else
          raise "Invalid data type: #{key_string_or_array.class}"
      end
    end<commit_msg>Clarified error message.<commit_after># Supports access to nested hashes with a string instead of multiple []'s.
# Inspired by <NAME>'s dot_notation gem at https://github.com/joshsz/dot_notation,

module TrickBag
module CollectionAccess

  module_function


  # Accesses a collection with a single string that represents n levels of depth.
  # See the spec file for examples, but here's one:
  #
  # h = { 'h' => ['a', 'b'] }
  # CollectionAccess.access(h, 'h.1') => 'b'
  #
  # If an error is raised while trying to access the value with a given key,
  # an error will be raised showing that error plus the context of which
  # key failed, as in:
  #
  # Error occurred processing key [x.1] in [x.1.2]: undefined method `[]' for nil:NilClass
  #
  # @param collection the collection to access
  # @param key_string_or_array the string or array representing the keys to use
  # @param separator the string to use to separate the keys, defaults to '.'
  def access(collection, key_string_or_array, separator = '.')

    to_integer = ->(object) do
      begin
        Integer(object)
      rescue
        raise "Key cannot be converted to an Integer: #{object}"
      end
    end

    get_key_array = -> do
      case key_string_or_array
        when Array
          key_string_or_array
        when String
          key_string_or_array.split(separator)
        else
          raise "Invalid data type: #{key_string_or_array.class}"
      end
    end
{'fraction_non_alphanumeric': 0.0838552540013918, 'fraction_numerical': 0.0027835768963117608, 'mean_word_length': 2.9115646258503403, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3392346', 'n_tokens_mistral': 854, 'n_tokens_neox': 839, 'n_words': 331}