From: Simon Strandgaard Date: 2004-05-07T18:19:30+09:00 Subject: Re: [ann] iterator-0.7 "Robert Klemme" wrote: > "Simon Strandgaard" schrieb im Newsbeitrag > news:20040506182920.02fd89f5.neoneye@adslhome.dk... > > "Robert Klemme" wrote: > > > Make Iterator::Concat#initialize to be able to do > > > iterator = Iterator::Concat.new(i1, i2, i3) # brackets removed > > > iterator = Iterator::Concat.new(i1, ary2, i3) > > > > > > > Major dillema, I don't know how to do it. > > My current Concat#initialize looks like "def initialize(iterators, > position=nil)" > > Ah, I see. > > > If I do "def initialize(*iterators, position=nil)".. that won't work ? > > > > irb(main):001:0> def m(*args, x=nil) > > irb(main):002:1> p args > > irb(main):003:1> p x > > irb(main):004:1> end > > SyntaxError: compile error > > (irb):1: syntax error > > def m(*args, x=nil) > > ^ > > (irb):1: syntax error > > from (irb):1 > > irb(main):005:0> > > > > Any hints on how to pass 'position' to initialize ? > > def init(*args) > if Numeric === args[-1] > pos = args[-1] > args.slice!(-1, 1) > else > pos = nil > end > > puts "iterators: #{args.inspect}" > puts "position : #{pos.inspect}" > end > Hmmm.. maybe we are trying to put too much logic into the initialize method. We could make a factory method for that. class Concat def self.mk(*iterators); end end > irb(main):066:0> init "a","s",1 > iterators: ["a", "s"] > position : 1 > => nil > irb(main):067:0> init "a","s" > iterators: ["a", "s"] > position : nil > => nil > irb(main):068:0> init > iterators: [] > position : nil > => nil > > Or even > > def init2(*args) > pos = nil > iters = [] > > args.each do |arg| > case arg > when Numeric > raise ArgumentError, "duplicate start positions" if pos > pos = arg > when Iterator # place appropriate type(s) here > iters << arg > when Enumerable > iters << Iterator::Continuation.new(arg, :each) > else > raise ArgumentError, "Don't know what to do with #{arg.inspect}" > end > end > > puts "iterators: #{iters.inspect}" > puts "position : #{pos.inspect}" > end > Bigger is better.. the factory method approach is visually better. Interesting dillema though :-) -- Simon Strandgaard