Created
June 14, 2018 02:59
-
-
Save littlelazer/7ef7889671b1fcb5a67ff3cc39410f66 to your computer and use it in GitHub Desktop.
Different ways of accessing struct members
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
$ Client = Struct.new(:first_name, :last_name, keyword_init: true) | |
=> Client(keyword_init: true) | |
$ client_1 = Client.new(first_name: "Jamal", last_name: "Windsor") | |
=> #<struct Client first_name="Jamal", last_name="Windsor"> | |
# using a getter | |
$ client_1.first_name | |
=> "Jamal" | |
# using a string as a key | |
$ client_1["first_name"] | |
=> "Jamal" | |
# using a symbol as a key | |
$ client_1[:first_name] | |
=> "Jamal" | |
# using an array index | |
$ client_1[0] | |
=> "Jamal" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment