Class: Struct

Inherits:
Object
  • Object
show all
Defined in:
lib/everythingrb/struct.rb

Overview

Extensions to Ruby’s core Struct class

Provides:

  • #to_deep_h: Recursively convert to hash with all nested objects

Examples:

require "everythingrb/struct"

Person = Struct.new(:name, :profile)
person = Person.new("Alice", {roles: ["admin"]})
person.to_deep_h  # => {name: "Alice", profile: {roles: ["admin"]}}

Instance Method Summary collapse

Instance Method Details

#to_deep_hHash

Recursively converts the Struct and all nested objects to hashes

Examples:

Address = Struct.new(:city, :country)
Person = Struct.new(:name, :address)
person = Person.new("Alice", Address.new("New York", "USA"))
person.to_deep_h  # => {name: "Alice", address: {city: "New York", country: "USA"}}

Returns:

  • (Hash)

    A deeply converted hash of the Struct



28
29
30
# File 'lib/everythingrb/struct.rb', line 28

def to_deep_h
  to_h.to_deep_h
end