From: "Stephan Kämper" Date: 2007-08-13T15:15:54+09:00 Subject: Re: first and last char Hi, Am 13.08.2007 um 08:04 schrieb Bertram Scharpf: > Hi, > > Am Sonntag, 12. Aug 2007, 02:43:00 +0900 schrieb Felix Windt: >>> -----Original Message----- >>> From: botp [mailto:botpena@gmail.com] >>> Sent: Saturday, August 11, 2007 10:30 AM >>> >>> wish there were #first and #last in String >> >> 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" > > Sometimes I wish every young programmer was forced to do a > month in Assembler and another one in C just to see what > cost in time and space some constructions cause. > > Sorry, Felix! Well, here's something that should be a little bit less cycle intensive (depending on how String#[](arg) is implemented): class String def last self[-1].chr end end Cheers Stephan -- Stephan K�mper/IT-Beratung http://www.stephankaemper.de Softwaretest / Datenanalyse / Entwicklung