Skip to content

Instantly share code, notes, and snippets.

@cheeyeo
Forked from alchimere/hash_with_default.rb
Created January 27, 2016 21:25
Show Gist options
  • Save cheeyeo/8caedab53f2b69cc0787 to your computer and use it in GitHub Desktop.
Save cheeyeo/8caedab53f2b69cc0787 to your computer and use it in GitHub Desktop.
# 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment