Skip to content

Instantly share code, notes, and snippets.

@kakra
Created September 22, 2008 12:15
Show Gist options
  • Save kakra/11974 to your computer and use it in GitHub Desktop.
Save kakra/11974 to your computer and use it in GitHub Desktop.
How to apply case-when on lambdas
#!/usr/bin/env ruby
class Proc
def ===(*parameters)
self.call(*parameters)
end
end
sunday = Proc.new { |time| time.wday == 0 }
monday = Proc.new { |time| time.wday == 1 }
tuesday = Proc.new { |time| time.wday == 2 }
wednesday = Proc.new { |time| time.wday == 3 }
thursday = Proc.new { |time| time.wday == 4 }
friday = Proc.new { |time| time.wday == 5 }
saturday = Proc.new { |time| time.wday == 6 }
case Time.now
when sunday
puts "Faulenzen!"
when monday, tuesday
puts "Örks, arbeiten. :-("
when wednesday
puts "Hälfte geschafft."
when thursday
puts "Ein Tag noch!"
when friday
puts "Juhu, bald Wochenende!"
when saturday
puts "Partytime!"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment