class Array

Public Instance Methods

to_deep_struct() click to toggle source

Returns a new struct with the same key/value pairs

users = [{ name: { first: "Dorian", last: "MariƩ" }].to_struct
users.first.name.first # => "Dorian"
# File lib/dorian/core_ext/array/struct.rb, line 19
def to_deep_struct
  map do |item|
    item.respond_to?(:to_deep_struct) ? item.to_deep_struct : item
  end
end
to_struct() click to toggle source

Returns a new struct with the same key/value pairs

users = [{ first_name: "Dorian", last_name: "MariƩ" }].to_struct
users.first.first_name # => "Dorian"
users.first.birthdate # => NoMethodError: undefined method `birthdate' for <struct...>
# File lib/dorian/core_ext/array/struct.rb, line 10
def to_struct
  map { |item| item.respond_to?(:to_struct) ? item.to_struct : item }
end