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

Thanks (to everyone) for the great answers. Once again Ruby excels at this
stuff (so far every example in the book turns out to be easier in Ruby
than in Perl-- I hope this is a trend {grin}).

(on Mon, 28 Jan 2002 14:16:57 +0900) Mathieu Bouchard <matju / sympatico.ca>
wrote:
> Or like this:
> 
> 	class Array
> 	  def splice(i,n,v)
> 	    r = slice!(i,n)
> 	    self[i,0]=v
> 	    r
> 	  end
> 	end

I did have to modify this to:

class Array
  def splice!( pos, len = self.length, args = [] ) 
    # set up an index for original position of insert in case of negative
pos    # otherwise we're counting from the end of a shorter array
    orig = pos
    if orig < 0 then
       orig = self.length + orig
    end
    return if orig < 0   #still out of range

    r = slice!( pos, len )
    self[ orig, 0 ] = args
    r 
  end
end

Without that pre-conversion of a negative index to a positive index we end
up off from where we probably wanted to be because the slice! resizes the
array.

I tried reversing the slice! and the self[] = args but I couldn't find any
way to avoid the temporary variable that seemed simpler (you still have to
account for negative indexes differently than positive because now the
array is larger than expected).

Thanks again!

 ______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_|

--=.po_ZMUKCRE6rkh
Content-Type: application/pgp-signature

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

iD8DBQE8VfI44ClW9KMwqnMRAqzNAJ9Rx9pl73r/tXTtQWVhZJbWmwuydwCcDRvh
DNbNq8lxwuG0/h/IkGQ1SO0=
=rxYF
-----END PGP SIGNATURE-----

--=.po_ZMUKCRE6rkh--