Class: Hash
- Defined in:
- lib/golly-utils/ruby_ext/options.rb,
lib/golly-utils/ruby_ext/deep_dup.rb,
lib/golly-utils/ruby_ext/hash_combinations.rb
Instance Method Summary collapse
-
#deep_dup ⇒ Object
Creates a copy of the hash with deep copies of each key and value.
-
#validate_keys(valid_keys, error_msg = "Invalid hash keys") ⇒ self
Checks that all keys in this hash are part of a specific set; fails if not.
-
#validate_option_keys(*valid_keys) ⇒ self
Checks that all keys in this hash are part of a specific set; fails if not.
-
#value_combinations ⇒ Array<Hash>
Given a hash where each value can be a single value or an array of potential values, this generate a hash for each combination of values across all keys.
Instance Method Details
#deep_dup ⇒ Object
Creates a copy of the hash with deep copies of each key and value.
22 23 24 25 26 27 28 |
# File 'lib/golly-utils/ruby_ext/deep_dup.rb', line 22 def deep_dup duplicate = {} each_pair do |k,v| duplicate[k.deep_dup]= v.deep_dup end duplicate end |
#validate_keys(valid_keys, error_msg = "Invalid hash keys") ⇒ self
Checks that all keys in this hash are part of a specific set; fails if not.
9 10 11 12 13 14 15 |
# File 'lib/golly-utils/ruby_ext/options.rb', line 9 def validate_keys(valid_keys, error_msg = "Invalid hash keys") invalid= self.keys - [valid_keys].flatten unless invalid.empty? raise "#{error_msg}: #{invalid.sort_by(&:to_s).map(&:inspect).join ', '}" end self end |
#validate_option_keys(*valid_keys) ⇒ self
Checks that all keys in this hash are part of a specific set; fails if not.
Differs from #validate_keys by providing an option-related error message.
24 25 26 |
# File 'lib/golly-utils/ruby_ext/options.rb', line 24 def validate_option_keys(*valid_keys) validate_keys valid_keys, "Invalid options provided" end |
#value_combinations ⇒ Array<Hash>
Given a hash where each value can be a single value or an array of potential values, this generate a hash for each combination of values across all keys.
23 24 25 |
# File 'lib/golly-utils/ruby_ext/hash_combinations.rb', line 23 def value_combinations collect_value_combinations [], {}, keys end |