Hi --

On Mon, 26 May 2003, Simon Strandgaard wrote:

> On Sun, 25 May 2003 23:47:54 +0000, Phlip wrote:
> > What's the equivalent in the (vastly superior) language Ruby? How does one
> > unflatten?
>
>
> How about ???
>
> > ruby flat.rb
> [[1, 1, 1], [2, 2], [3, 3, 3, 3]]
> >
>
> > cat flat.rb
> class Array
>         def shape(*n)
>                 data = self
>                 res = []
>                 n.each do |count|
>                         res << data.slice!(0, count)
>                 end
>                 res
>         end
> end
> p [1, 1, 1, 2, 2, 3, 3, 3, 3].shape(3, 2, 4)

That modifies the original array, though, which probably isn't what's
wanted.

Less impact-ful way:

  class Array
    def shape(*n)
      i = 0
      n.map {|e| s=i; i+=e; slice(s,e)}
    end
  end


David

-- 
David Alan Black
home: dblack / superlink.net
work: blackdav / shu.edu
Web:  http://pirate.shu.edu/~blackdav