Class: Hash
- Inherits:
-
Object
- Object
- Hash
- Defined in:
- lib/everythingrb/core/hash.rb
Overview
Extensions to Ruby’s core Hash class
These additions make working with hashes more convenient by adding conversion methods to different data structures and string formatting helpers.
Constant Summary collapse
- EMPTY_STRUCT =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
A minimal empty struct for Ruby 3.2+ compatibility
Ruby 3.2 enforces stricter argument handling for Struct. This means trying to create a Struct from an empty Hash will result in an ArgumentError being raised. This is trying to keep a consistent experience with that version and newer versions.
Struct.new(:_).new(nil)
Instance Method Summary collapse
-
#deep_freeze ⇒ self
Recursively freezes self and all of its values.
-
#join_map(join_with = "") {|key, value| ... } ⇒ String
Combines filter_map and join operations.
-
#to_istruct ⇒ Data
Converts hash to an immutable Data structure.
-
#to_ostruct ⇒ OpenStruct
Converts hash to an OpenStruct recursively.
-
#to_struct ⇒ Struct
Converts hash to a Struct recursively.
Instance Method Details
#deep_freeze ⇒ self
Recursively freezes self and all of its values
146 147 148 149 |
# File 'lib/everythingrb/core/hash.rb', line 146 def deep_freeze each_value { |v| v.respond_to?(:deep_freeze) ? v.deep_freeze : v.freeze } freeze end |
#join_map(join_with = "") {|key, value| ... } ⇒ String
Combines filter_map and join operations
51 52 53 54 55 |
# File 'lib/everythingrb/core/hash.rb', line 51 def join_map(join_with = "", &block) block = ->(kv_pair) { kv_pair.compact } if block.nil? filter_map(&block).join(join_with) end |
#to_istruct ⇒ Data
Converts hash to an immutable Data structure
68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/everythingrb/core/hash.rb', line 68 def to_istruct recurse = lambda do |input| case input when Hash input.to_istruct when Array input.map(&recurse) else input end end Data.define(*keys.map(&:to_sym)).new(*values.map { |value| recurse.call(value) }) end |
#to_ostruct ⇒ OpenStruct
Converts hash to an OpenStruct recursively
122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/everythingrb/core/hash.rb', line 122 def to_ostruct recurse = lambda do |value| case value when Hash value.to_ostruct when Array value.map(&recurse) else value end end OpenStruct.new(**transform_values { |value| recurse.call(value) }) end |
#to_struct ⇒ Struct
Converts hash to a Struct recursively
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/everythingrb/core/hash.rb', line 94 def to_struct # For Ruby 3.2, it raises if you attempt to create a Struct with no keys return EMPTY_STRUCT if RUBY_VERSION.start_with?("3.2") && empty? recurse = lambda do |value| case value when Hash value.to_struct when Array value.map(&recurse) else value end end Struct.new(*keys.map(&:to_sym)).new(*values.map { |value| recurse.call(value) }) end |