From: Jean Helou Date: 2006-10-04T22:18:09+09:00 Subject: Re: How to DRY this? On 10/4/06, 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 ;-) > > ... > while element.class != Hpricot::Doc do > path.push element.name > element = element.parent > end > ... > > and > ... > while element.class != Hpricot::Doc do > path.push element > element = element.parent > end > ... > > > i.e. in the first snippet I am pushing element's names, and in the > latter the elements themselves. > > Thanks, > Peter > http://www.rubyrailways.com > > interesting question ... def element_pusher(root_element, attribute=nil) while element.class!=Hpricot::Doc do pushie = attribute.nil? ? element : element.send(attribute.to_sym) path.push pushie element = element.parent end path end good ? I don't have time to mockup something to test it but path.push(attribute.nil? ? element : element.send(attribute.to_sym)) might even work ... jean jean