« Return to Thread: Proposal: Array#walker

Proposal: Array#walker

by Wolfgang Nádasi-donner-2 :: Rate this Message:

Reply to Author | View in Thread

Good morning all together!

I know it is very late for Ruby 1.9.1 proposals, but I have a very simple and
easy suggestion. While writing a program to generate the Ruby program for some
guessUTF test (border cases which should not success), I recognized, that it can
be very helpful for several use cases to have a method Array#walker with the
following Ruby implementation...

class Array
   def walker
     l = self.length
     l.times do |i|
       yield self[0, i], self[i], self[i+1, l-i-1]
     end
   end
end

A simple usage example is...

[1,2,3,4].walker{|pre,el,post|puts "#{pre.inspect}-#{el} - #{post.inspect}"}

Output:
[]-1 - [2, 3, 4]
[1]-2 - [3, 4]
[1, 2]-3 - [4]
[1, 2, 3]-4 - []

What do you think about this (hopefully) minor change proposal?

Wolfgang Nádasi-Donner

 « Return to Thread: Proposal: Array#walker