From: Stefan Rusterholz Date: 2007-08-12T04:35:29+09:00 Subject: Re: first and last char Felix Windt wrote: >> >> of course, they err :) >> >> wish there were #first and #last in String >> >> just a thought >> kind regards -botp >> > > irb(main):001:0> class String > irb(main):002:1> def first > irb(main):003:2> self.split('').first > irb(main):004:2> end > irb(main):005:1> def last > irb(main):006:2> self.split('').last > irb(main):007:2> end > irb(main):008:1> end > => nil > irb(main):009:0> "testing".first > => "t" > irb(main):010:0> "testing".last > => "g" 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 Regards Stefan -- Posted via http://www.ruby-forum.com/.