From: Joel VanderWerf Date: 2007-06-11T07:29:17+09:00 Subject: Re: fun with "case" Robert Klemme wrote: > > A funny (and readable) way to test collection sizes just occurred to me: ... Some more Sunday afternoon fun... class Cond < Proc alias :=== :call end module Kernel def cond Cond.new end end class Integer def elements cond {|enum| self == enum.size} end end class Range def elements cond {|enum| self === enum.size} end def includes_it cond {|enum| enum.all?{|elt| self.include?(elt)}} end end class Object def is_included cond {|enum| enum.include? self} end end require 'enumerator' INCREASING = cond {|enum| enum.enum_for(:each_cons, 2).all?{|x,y| x < y}} case [1,2,3] when (3..5).elements puts "at least three, but no more than five, elements" end case [1,2,3] when (1..4).includes_it puts "included in 1..4" end case [1,2,3] when 2.is_included puts "includes 2" end case [1,2,3] when INCREASING puts "increasing!" end case [1,2,3] when 3.elements puts "three elements!" when 5.elements puts "too much!" else puts "else" end __END__ Output: at least three, but no more than five, elements included in 1..4 includes 2 increasing! three elements! -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407