Last active
June 21, 2018 18:49
-
-
Save littlelazer/73bc85f83a1f0e5623b272373295f18a to your computer and use it in GitHub Desktop.
Using enumerable/hash/array methods for 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
$ 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