From: Ken Bloom Date: 2006-10-04T22:20:10+09:00 Subject: Re: How to DRY this? On Wed, 04 Oct 2006 22:05:41 +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 ;-) > > ... > 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. Option 1: don't. It's a very small snippet of code. Option 2: def dopush while element.class!= Hpricot::Doc do path.push(yield(element)) element = element.parent end end The first snippet becomes dopush {|e| e.name} The second snippet becomes dopush {|e| e} -- Ken Bloom. PhD candidate. Linguistic Cognition Laboratory. Department of Computer Science. Illinois Institute of Technology. http://www.iit.edu/~kbloom1/ I've added a signing subkey to my GPG key. Please update your keyring.