Skip to content

Instantly share code, notes, and snippets.

@alchimere
Created November 14, 2015 17:19
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save alchimere/a53fca0641d446c3d7a3 to your computer and use it in GitHub Desktop.
Save alchimere/a53fca0641d446c3d7a3 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)
@cokara
Copy link

cokara commented Nov 19, 2015

I love this.

@gma
Copy link

gma commented Nov 19, 2015

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