From: Robert Klemme Date: 2010-01-29T01:16:06+09:00 Subject: Re: Class with Multi Constrcutor ( initialize ) 2010/1/28 Albert Schlef : > Gary Wright wrote: >> On Jan 27, 2010, at 4:14 PM, Albert Schlef wrote: >> >>> Cool. Where is "Array#===" documented? I couldn't find it. >> Unfortunately Array#=== is just the same as Array#==, > > So it isn't cool. "when [Integer, Integer]" won't match any integer > numbers :-( > >> There was a somewhat recent thread about changing Array#=== to >> be an element-wise application of ===, which makes a lot of >> sense to me. > > to me too. Even if that would not happen you could do this and benefit from a more efficient approach because tests are constants: def MultiCheck(*a) def a.===(b) zip(b) {|x,y| return false unless x === y} true end a.freeze end irb(main):023:0> T1 = MultiCheck String => [String] irb(main):024:0> T2 = MultiCheck Fixnum, String => [Fixnum, String] irb(main):025:0> def overload(*a) irb(main):026:1> case a irb(main):027:2> when T1 irb(main):028:2> puts "a single string" irb(main):029:2> when T2 irb(main):030:2> puts a[1] * a[0] irb(main):031:2> else irb(main):032:2* raise ArgumentError, "can't %p" % a irb(main):033:2> end irb(main):034:1> end => nil irb(main):035:0> overload "foo" a single string => nil irb(main):036:0> overload 2, "foo" foofoo => nil irb(main):037:0> overload 2 ArgumentError: can't 2 from (irb):32:in `overload' from (irb):37 from /opt/bin/irb19:12:in `
' irb(main):038:0> :-) Note, the same approach also works with regular expressions as elements in the MultiCheck Array. Cheers robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/