Last active
June 14, 2018 03:14
-
-
Save littlelazer/0287fba9de7d4349118d27fbf05d44a9 to your computer and use it in GitHub Desktop.
Immutable complete struct
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
$ Person = Struct.new(:first_name, :last_name) do | |
def initialize(*args) | |
raise ArgumentError, "Incorrect arguments" unless args[0].member?(:first_name) && args[0].member?(:last_name) | |
super(*args) | |
self.freeze | |
end | |
end | |
=> Person | |
$ eryan = Person.new(first_name: "Eryan") | |
=> ArgumentError (Incorrect arguments) | |
$ eryan = Person.new(first_name: "Eryan", bad_argument: "Cobham") | |
=> ArgumentError (Incorrect arguments) | |
$ eryan = Person.new(first_name: "Eryan", last_name: "Cobham") | |
=> #<struct Person first_name="Eryan", last_name="Cobham"> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment