Class: Hash
- Inherits:
-
Object
- Object
- Hash
- Defined in:
- documented/common/class_exts/hash.rb
Overview
Extension to the Hash class This class adds additional methods to the built-in Hash class.
Class Method Summary collapse
-
.put(target, path, val) ⇒ Hash
Puts a value into a nested hash structure at the specified path.
Instance Method Summary collapse
-
#to_struct ⇒ OpenStruct
Converts the hash to an OpenStruct object.
Class Method Details
.put(target, path, val) ⇒ Hash
Puts a value into a nested hash structure at the specified path.
19 20 21 22 23 24 25 26 |
# File 'documented/common/class_exts/hash.rb', line 19 def self.put(target, path, val) path = [path] unless path.is_a?(Array) fail ArgumentError, "path cannot be empty" if path.empty? root = target path.slice(0..-2).each { |key| target = target[key] ||= {} } target[path.last] = val root end |
Instance Method Details
#to_struct ⇒ OpenStruct
Converts the hash to an OpenStruct object.
34 35 36 |
# File 'documented/common/class_exts/hash.rb', line 34 def to_struct OpenStruct.new self end |