From: Mikael Brockman Date: 2004-10-11T05:03:25+09:00 Subject: Re: Putting some code out to DRY "trans. (T. Onoma)" writes: > Notice the three lines of duplication. Any ideas on DRYing this up? > > def each > seed = @start > if @step > @step.times do > yield(seed) > @skip.times { seed = seed.succ } > break if @halt.call(seed) if @halt > end > else > loop do > yield(seed) > @skip.times { seed = seed.succ } > break if @halt.call(seed) if @halt > end > end > end You could let @step default to $infinity, where $infinity is something like this: | $infinity = Object.new | class << $infinity | def times; loop { yield }; end | # other infinity methods omitted because YAGNI | end That would let you define #each as simply | def each | seed = @start | @step.times do | yield(seed) | @skip.times { seed = seed.succ } | break if @halt.call(seed) if @halt | end | end