From: Ken Coar Date: 2010-02-22T18:38:34+09:00 Subject: Re: Complex type-checking Robert Klemme wrote: > >> So please >> let's leave the 'why do you want to do that' bit alone for now. :-) > > Sorry, I can't. Why do you want to do that? :-) :-) >> Which is actually turning out to be a right bugger to code, actually. > > Is it? Well, it seems to be.. The thing is, the Point class match works all right -- but when specified as individual coordinates, they can be integers (potentially bignums) or floats -- so checking the actual coordinates is not as simple as matching the class. I *could* do a (Fixnum|Float|...) alternation, I suppose, but that doesn't extend to cases in which someone might be using a custom class extending one of those. Which is actually probably a bit unlikely, so maybe I'll just rule that possibility out. :-) > def pattern_check(args, pattern) > m = pattern.match(args.map{|x|x.class}.join(' ')) > > if m > m.to_a.each_with_index do |m,i| > return i if i > 0 && m > end > end > > raise ArgumentError, args.inspect > end > > def m(*a) > case pattern_check(a, %r{\A(?:(String String)|(Fixnum))\z}) > when 1 > printf "Two strings: %p\n", a > when 2 > printf "A number: %p\n", a > else > raise ArgumentError, a.inspect > end > end > > Method m does look ugly though. Consider also that the argument list may include more than one tuple.. This has given me some ideas, though. Thanks! #ken B-)} -- Posted via http://www.ruby-forum.com/.