Created
June 14, 2018 02:55
-
-
Save littlelazer/ecadfc56b19096b83bda9b399c407809 to your computer and use it in GitHub Desktop.
Ruby Struct with kwargs
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
# Ruby 2.5 | |
# Create a struct the new way | |
$ Coordinate = Struct.new(:latitude, :longitude, keyword_init: true) | |
=> Coordinate(keyword_init: true) | |
# keyword arguments, can define any property | |
$ chicago = Coordinate.new(longitude: -87.67897) | |
=> #<struct Coordinate latitude=nil, longitude=-87.67897> | |
# once you use the keyword_init option for the struct, you must use keyword arguments | |
$ devmynd = Coordinate.new(41.912047, -87,67897) | |
ArgumentError (wrong number of arguments (given 2, expected 0)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment