From: Blake Miller Date: 2006-12-02T04:19:47+09:00 Subject: Re: Simple iteration in a function problem Mike Fletcher wrote: > 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. I don't know where "detect" comes from (@ys is an array, and it doesn't seem to be a method of Array)? however, I tried that and the result is null, but I know for certain that the id being searched for does match an element in the @ys array (because I wrote a function to print out the ids in @ys) -- Posted via http://www.ruby-forum.com/.