Created
November 14, 2015 17:19
-
-
Save alchimere/a53fca0641d446c3d7a3 to your computer and use it in GitHub Desktop.
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
# Other syntax without patch | |
STUDENT_LEVELS = { | |
freshman: Student::Underclassman, | |
sophomore: Student::Underclassman, | |
junior: Student::Upperclassman, | |
senior: Student::Upperclassman, | |
graduate: Student::Graduate | |
}.tap { |h| h.default = Student::Unregistered } | |
# Other syntax with patch on Hash | |
class Hash | |
def with_default(value) | |
self.default = value | |
self | |
end | |
end | |
STUDENT_LEVELS = { | |
freshman: Student::Underclassman, | |
sophomore: Student::Underclassman, | |
junior: Student::Upperclassman, | |
senior: Student::Upperclassman, | |
graduate: Student::Graduate | |
}.with_default(Student::Unregistered) |
I like the first approach, but I'd be much happier encountering code that didn't patch Hash
to just to re-invent API that's provided in the standard library. It's bad for (other people's) productivity.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I love this.