From: Mike Fletcher Date: 2006-12-02T04:09:37+09:00 Subject: Re: Simple iteration in a function problem Blake Miller wrote: > # this is the problem function. all I want is the specific (ONE) > element > # from the @ys array that matches the id, but when you iterate over the > # array, i can't find a way to only have one element returned > def getXById(id) > @ys.each_with_index do |comp, index| > if( @ys[index].getUniqueId == id ) > @ys.fetch(index) > end > end > end Perhaps def getXById( id ) @ys.detect { |y| y.getUniqueId == id } end See the docs for Enumerable#detect about handling what's returned if nothing is found. -- Posted via http://www.ruby-forum.com/.