Skip to content

Instantly share code, notes, and snippets.

@lukemorton
Last active October 20, 2015 13:10
Show Gist options
  • Save lukemorton/ff207f96c71ccd56027e to your computer and use it in GitHub Desktop.
Save lukemorton/ff207f96c71ccd56027e to your computer and use it in GitHub Desktop.
class Person < TypedStruct[first_name: String, last_name: String]
def full_name
"#{first_name} #{last_name}"
end
end
class Person < TypedStruct[first_name: String,
last_name: String,
age: [Integer, :optional]]
def full_name
"#{first_name} #{last_name}"
end
end
class Person < TypedStruct
field :first_name, String
field :last_name, String
field :age, Integer, :optional
end
Person.new('Luke', 'Morton')
Person.new(first_name: 'Luke', last_name: 'Morton')
Person.new(first_name: 12, last_name: 'Morton') # => raises TypedStruct::TypeError
Person.new(first_name: 'Luke') # => raises TypedStruct::FieldMissingError
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment