dideler
11/14/2015 - 5:19 PM

Related to : http://weblog.jamisbuck.org/2015/11/14/little-things-refactoring-with-hashes.html

# 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)