Trans
8/12/2007 2:31:00 PM
On Aug 11, 6:52 pm, Daniel DeLorme <dan...@dan42.com> wrote:
> Stefan Rusterholz wrote:
> > Ew, that's awfully complex. You create n new objects from which you
> > throw n-1 away again...
> > Think of the memory! ;-)
> > def first; self[0,1]; end; def last; self[-1,1]; end
>
> wrong, that's the first and last *bytes*, not characters.
For Ruby 1.9+ it will just be:
def first; self[0]; end; def last; self[-1]; end
my own version is (basically):
def first(pattern=//)
split(pattern).at(0)
end
which is a little more versatile. but I see the point about the
memory, and I'll add an optimization clause come 1.9.
T.