From: Markus Date: 2004-09-24T00:13:31+09:00 Subject: Re: Newbie: Case statement On Thu, 2004-09-23 at 05:04, Florian Gross wrote: > Yup, you can use this as a case that checks all options: > > def greedy_case(obj, cases) > cases.find_all do |condition, action| > condition === obj > end.inject(Hash.new) do |hash, (condition, action)| > hash[condition] = action.call; hash > end > end > > def test(x) > greedy_case(x, > 1 .. 2 => lambda { puts "In A" }, > 1 .. 3 => lambda { puts "In B" }, > 2 .. 4 => lambda { puts "In C" }) > end > > test(2) Nice. Thats the best examples of writing your own control structure I've seen in a quite while. *smile* And it even has the non-deterministic aspect that Dijkstra was so fond of. -- Markus