From: ara.t.howard@... Date: 2006-08-31T02:52:58+09:00 Subject: Re: [RCR] Array.step On Thu, 31 Aug 2006, Logan Capaldo wrote: > require 'enumerator' > class Array > def self.step(i, *a, &block) > b = block || lambda { |x| x } > i.to_enum(:step, *a).map(&b) > end > end you'll have to write almost as many lines as i did to make all of it work though, still - i had forgotten about that: harp:~ > cat a.rb require 'enumerator' class Array def self.step(i, *a, &block) b = block || lambda { |x| x } i.to_enum(:step, *a).map(&b) 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) ==>[] # <- this one's wrong Array.step(1,5,2) ==>[1, 3, 5] Array.step(-1,-5,-2) ==>[-1, -3, -5] Array.step 2 ArgumentError: wrong number of arguments from a.rb:17:in `step' from a.rb:17:in `step' from (irb):1 Array.step -2 ArgumentError: wrong number of arguments from a.rb:17:in `step' from a.rb:17:in `step' from (irb):1 Array.step(0,7){|i| 2 ** i} ==>[1, 2, 4, 8, 16, 32, 64, 128] even = Array.step(9){|i| i.modulo(2).zero?} ArgumentError: wrong number of arguments from a.rb:17:in `step' from a.rb:17:in `step' from (irb):1 regards. -a -- what science finds to be nonexistent, we must accept as nonexistent; but what science merely does not find is a completely different matter... it is quite clear that there are many, many mysterious things. - h.h. the 14th dalai lama