Skip to content

Instantly share code, notes, and snippets.

@littlelazer
Created June 14, 2018 02:55
Show Gist options
  • Save littlelazer/ecadfc56b19096b83bda9b399c407809 to your computer and use it in GitHub Desktop.
Save littlelazer/ecadfc56b19096b83bda9b399c407809 to your computer and use it in GitHub Desktop.
Ruby Struct with kwargs
# 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