class Hash
Public Instance Methods
to_deep_struct()
click to toggle source
Returns a new struct with the same key/value pairs
user = { name: "Dorian", events: [{ name: "Party" }] }.to_deep_struct user.events.first.name # => "Party"
# File lib/dorian/core_ext/hash/struct.rb, line 19 def to_deep_struct Struct .new(*keys.map(&:to_sym)) .new( *values.map do |value| value.respond_to?(:to_deep_struct) ? value.to_deep_struct : value end ) end
to_struct()
click to toggle source
Returns a new struct with the same key/value pairs
user = { first_name: "Dorian", last_name: "MariƩ" }.to_struct user.first_name # => "Dorian" user.birthdate # => NoMethodError: undefined method `birthdate' for <struct...>
# File lib/dorian/core_ext/hash/struct.rb, line 10 def to_struct Struct.new(*keys.map(&:to_sym)).new(*values) end