Created
June 14, 2018 03:01
-
-
Save littlelazer/25bf462b242def5a6c6e6aa2c25c7f0e to your computer and use it in GitHub Desktop.
Digging Structs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Companies have Employees, and Employees have Addresses | |
$ Company = Struct.new(:name, :employees, keyword_init: true) | |
=> Company(keyword_init: true) | |
$ Employee = Struct.new(:first_name, :last_name, :address, keyword_init: true) | |
=> Employee(keyword_init: true) | |
$ Address = Struct.new(:street, :city, :state, :zip, keyword_init: true) | |
=> Address(keyword_init: true) | |
$ address = Address.new(street: "2035 W. Wabansia", city: "Chicago", state: "Illinois", zip: 60649) | |
=> #<struct Address street="2035 W. Wabansia", city="Chicago", state="Illinois", zip=60649> | |
$ employee = Employee.new(first_name: "Eryan", last_name: "Cobham", address: address) | |
=> #<struct Employee first_name="Eryan", last_name="Cobham", address=#<struct Address street="2035 W. Wabansia", city="Chicago", state="Illinois", zip=60649>> | |
$ company = Company.new(name: "Devmynd", employees: [employee]) | |
=> #<struct Company name="Devmynd", employees=[#<struct Employee first_name="Eryan", last_name="Cobham", address=#<struct Address street="2035 W. Wabansia", city="Chicago", state="Illinois", zip=60649>>]> | |
$ state = company.dig(:employees, 0, :address, :state) | |
=> "Illinois" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment