From: Robert Mela Date: 2006-06-08T23:03:26+09:00 Subject: Re: Easy question --------------060001090703020302080307 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit 1) @points is an array of point objects class Point :attr_reader :x, :y def initialize( x, y ) @x, @y=x,y end end 2) With inject, aren't you iterating over the entire array every time you add a new point, such that series.add(p) becomes O n^2? Daniel Schierbeck wrote: > Robert Mela wrote: >> def add_point( p ) >> @points.push(p) >> if !@minx || @minx > p.x >> @minx=p.x >> end >> if .... > > @points is an array of arrays, right? > > def add_point(p) > @points.push(p) > @max_x = @points.inject{|max, cur| cur[0] > max[0] ? cur : max}[0] > @min_x = @points.inject{|min, cur| cur[0] < min[0] ? cur : max}[0] > @max_y = @points.inject{|max, cur| cur[1] > max[1] ? cur : max}[1] > @min_y = @points.inject{|min, cur| cur[1] < min[1] ? cur : max}[1] > end > > Depending on how often you update the array, and how often you want > the min/max values, you ought to consider putting each #inject in its > own method instead: > > def max_x > @points.inject{|max, cur| cur[0] > max[0] ? cur : max}[0] > end > > > Cheers, > Daniel > --------------060001090703020302080307 Content-Type: text/x-vcard; charset=utf-8; name="rmela.vcf" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="rmela.vcf" begin:vcard fn:Robert Mela n:Mela;Robert adr;dom:;;;Lexington;MA email;internet:rmela@rcn.com tel;cell:781-363-0982 x-mozilla-html:FALSE version:2.1 end:vcard --------------060001090703020302080307--