From: David Holroyd Date: 2006-10-04T22:19:14+09:00 Subject: Re: How to DRY this? On Wed, Oct 04, 2006 at 10:05:41PM +0900, Peter Szinek wrote: > Hello, > > I have two very similar code snippets in two different methods, and I am > absolutely sure there is some nice way to DRY them in Ruby... I am still > a noob when comes to Ruby idioms so I'd appreciate some help ;-) how about (untested)... def element_and_each_ancestor(element) while element.class != Hpricot::Doc do yield element element = element.parent end end > > ... > while element.class != Hpricot::Doc do > path.push element.name > element = element.parent > end > ... element_and_each_ancestor(element) do |el| path.push el.name end > and > ... > while element.class != Hpricot::Doc do > path.push element > element = element.parent > end > ... element_and_each_ancestor(element) do |el| path.push element end > > i.e. in the first snippet I am pushing element's names, and in the > latter the elements themselves. ta, dave -- http://david.holroyd.me.uk/