Skip to content

Instantly share code, notes, and snippets.

@littlelazer
Last active June 21, 2018 18:49
Show Gist options
  • Save littlelazer/73bc85f83a1f0e5623b272373295f18a to your computer and use it in GitHub Desktop.
Save littlelazer/73bc85f83a1f0e5623b272373295f18a to your computer and use it in GitHub Desktop.
Using enumerable/hash/array methods for structs
$ Employee = Struct.new(:first_name, :last_name, :email, keyword_init: true)
=> Employee(keyword_init: true)
$ employee = Employee.new(first_name: "Eryan", last_name: "Cobham", email: "eryan@devmynd.com")
=> #<struct Employee first_name="Eryan", last_name="Cobham", email="eryan@devmynd.com">
# output all properties of the struct, even uninitialized ones
$ employee.members
=> [:first_name, :last_name, :email]
# output all of the set values
$ employee.values
=> ["Eryan", "Cobham", "eryan@devmynd.com"]
$ employee.select{ |value| value.start_with? "e" }
=> ["eryan@devmynd.com"]
$ employee.each{ |value| puts value }
Eryan
Cobham
eryan@devmynd.com
=> #<struct Employee first_name="Eryan", last_name="Cobham", email="eryan@devmynd.com">
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment