Skip to content

Instantly share code, notes, and snippets.

@littlelazer
Created June 14, 2018 02:59
Show Gist options
  • Save littlelazer/7ef7889671b1fcb5a67ff3cc39410f66 to your computer and use it in GitHub Desktop.
Save littlelazer/7ef7889671b1fcb5a67ff3cc39410f66 to your computer and use it in GitHub Desktop.
Different ways of accessing struct members
$ 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