From: Eric Mahurin Date: 2006-08-31T02:45:34+09:00 Subject: Re: [RCR] Array.step ------=_Part_20571_14977908.1156959908553 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline On 8/30/06, ara.t.howard@noaa.gov wrote: > > > > > harp:~ > cat a.rb > class Array > def self.step i, *a, &b > j, s, ignored = *a > i, j = 0, i if j.nil? > s ||= (j < i ? -1 : 1) > list = new > i.step(j,s){|k| list << k} > list.map! &b if b > list > end > end > > require 'irb/xmp' > > xmp 'Array.step(0,4)' > xmp 'Array.step(0,-4)' > > xmp 'Array.step(1,5,2)' > xmp 'Array.step(-1,-5,-2)' > > xmp 'Array.step 2' > xmp 'Array.step -2' > > xmp 'Array.step(0,7){|i| 2 ** i}' > xmp 'even = Array.step(9){|i| i.modulo(2).zero?}' > > > > harp:~ > ruby a.rb > Array.step(0,4) > ==>[0, 1, 2, 3, 4] > Array.step(0,-4) > ==>[0, -1, -2, -3, -4] > Array.step(1,5,2) > ==>[1, 3, 5] > Array.step(-1,-5,-2) > ==>[-1, -3, -5] > Array.step 2 > ==>[0, 1, 2] > Array.step -2 > ==>[0, -1, -2] > Array.step(0,7){|i| 2 ** i} > ==>[1, 2, 4, 8, 16, 32, 64, 128] > even = Array.step(9){|i| i.modulo(2).zero?} > ==>[true, false, true, false, true, false, true, false, true, false] > What's wrong with the block form of Array.new? Array.new(5) { |i| i } Array.new(5) { |i| -i } Array.new(3) { |i| 1+2*i } Array.new(3) { |i| -(1+2*i) } Array.new(3) { |i| i } Array.new(3) { |i| -i } Array.new(8){ |i| 2 ** i } Array.new(10){ |i| i.modulo(2).zero? } ------=_Part_20571_14977908.1156959908553--