"Martin DeMello" <martindemello / yahoo.com> schrieb im Newsbeitrag news:J%m6a.316187$Yo4.11888543 / news1.calgary.shaw.ca... > Robert Klemme <bob.news / gmx.net> wrote: > > "Martin DeMello" <martindemello / yahoo.com> schrieb im Newsbeitrag > >> Tangentially, is the following code safe: > >> > >> def initialize(*factors) > >> @factors = factors > >> @filters = [] > >> @factors.select {|i| i.is_a?(Proc)}.each {|fn| > >> (@filters[(fn.arity.abs) -1] ||= []) << fn > >> # ( a kludge to get around the fact that {|x|}.arity = -1 ) > >> } > >> @factors.reject!{|i| i.is_a?(Proc)} > >> @dim = @factors.length > >> end > >> > >> assuming the blocks in @factors are guaranteed not to have *d arguments? > >> Or is there some insidious corner case I'm missing? > > > > I'm not sure whether I understand where you're up to. I wonder why you > > substract 1 from the abs(arity). > > To have the array start indexing from 0 :) The bane of array programmers > for generations now. Ah, I see. As long as you can make sure that nobody sends you a block with arity = 0... > The abs() is the kludge I was referring to - it's > there solely to take care of the arity=-1 case. Yes, of course. > > And another tip aside: move the reject! above then @factors.select. Then > > you can omit the select and gain a bit performance. :-) > > Sadly, reject! returns the array, not the rejected elements. I'm trying > to separate the array into procs and non-procs (what I ought to do is to > use partition, now that it's in 1.8). Oops, yes I overlooked that there is no "!": I thought you wanted to remove all non procs but in effect you do a partition. sorry for the confusion. Regards robert