--TL2702s=.SiNZ5Ja
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit

I'm rewriting the examples in "Mastering Algorithms with Perl" (the
O'Reilly book) in Ruby. I was a bit surprised to find no splice command in
Ruby, but I needed one for the sandwich example at the end of chapter two,
so I wrote one. I checked past ruby-talk for others, but found only sparse
references to splice.

So being fairly new at Ruby (and not even sure this works 100% like the
Perl splice), I'd love comments and suggestions-- especially since I've
only got a few unit tests (mostly the aforementioned sandwich example).
Anything I missed that will break easily? Anything that could be written
better another way? Any Ruby idioms I've totally flubbed?

Thanks in advance for any advice. --michael

#!/usr/bin/ruby -w

class Array

  def splice( pos, len = self.length, argarray = [] )
    # pos = position to start insert/delete from
    # len = length of slice to pull/replace (non-negative)
    # argarray = a Array of arguments we want to put into the array
    #            (actually any object that has #each will be work)

    if( pos < 0 ) then
      # starting from the end of the array
      # +1 because you can't have -0 to indicate last elemeent
      pos = self.length + pos
    end

    # we're going to have problems if pos is bigger than the
    # array, or is still negative (and if it's still negative, 
    # should it just default to 0?)
    if( pos < 0 or pos > self.length ) then
      #should probably raise error, for now we'll just bail out
      return
    end

    # if len > 0 then we need to return an array of len elements
    retarray = []
    # if we don't remove the whole end of the array,
    # we'll want to store the second half somewhere
    halfarray = []

    if( len >= 0 ) then	
      # set the end point of the slice to remove
      term = pos + len - 1 #to account for single elements
      term = self.length if term > self.length

      # remove from the end of the array to the element after that
      if term < self.length then
	halfarray = self.slice!(( term + 1)  .. self.length )
      end

      # remove the spliced part
      if( pos <= term ) then
	retarray = self.slice!( pos .. term )
      end
    else
      return    # should raise error?
    end

    # now slap the temporary array elements (if any) 
    # onto what's left of the original array.
    argarray.each{ |x| self.push(x) }
    halfarray.each{ |x| self.push(x) }

    return retarray
  end # splice

end

# make a basic sandwich
sandwich = %w( bread bologna bread )
puts sandwich.inspect

# change our mind, go for club sandwich instead.
puts sandwich.splice( 1, 1, %w( chicken lettuce bread bacon mayo ) )
puts sandwich.inspect

# hmmm. hold the mayo and butter the bread, please
puts sandwich.splice( 1, 0, 'butter' )
puts sandwich.inspect
puts sandwich.splice( -2, 1, 'butter' )
puts sandwich.inspect

# time for lunch
puts mouth = sandwich.splice( 0 )
puts mouth.inspect
puts sandwich.inspect

# try to cause indigestion
puts gullet = mouth.splice( -99999, 256, ['vinegar', 'baking soda'])
puts mouth.inspect
puts gullet = mouth.splice( 256, -9, ['zotz'] )
puts mouth.inspect
puts gullet = mouth.splice( 1, 256, ['pickles', 'ice cream'] )
puts mouth.inspect


 ______Michael_C_Libby__{_x_(at)_ichimunki_(dot)_com_}______
| "even monkeys fall from trees" : "saru mo ki kara ochiru" |
|     public key at http://www.ichimunki.com/public.key     |
|_Fingerprint: D946FE20 79EE2109 161BFAFB E02956F4 A330AA73_|

--TL2702s=.SiNZ5Ja
Content-Type: application/pgp-signature

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)

iD8DBQE8VMvL4ClW9KMwqnMRAmTpAJ9dpZem/t3mWXoIjWXkWq9j0oLzwwCeOoEE
8hqUqDXnXQad+dwr+pUZdbA=
=xRXM
-----END PGP SIGNATURE-----

--TL2702s=.SiNZ5Ja--