From: "M. Edward (Ed) Borasky" Date: 2006-10-04T22:35:12+09:00 Subject: Re: How to DRY this? 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 > > A couple of obvious (to me, anyhow) questions: 1. You are "pushing" elements and element names. Are you "pulling" or "popping" them somewhere else? 2. If you just pushed the element name, could you retrieve the element from its name? 3. Is there some reason you need two loops up the chain? Could you do while element.class != Hpricot::Doc do path.push [element.name => element] element = element.parent end or something similar?