From: Rajinder Yadav Date: 2009-12-27T13:02:12+09:00 Subject: Re: How to truncate the spaces in the front of a line Phillip Gawlowski wrote: > On 27.12.2009 04:22, Milo Luo wrote: >> Hi, guys >> I am new to Ruby. Do you have any idea to truncate or remove the space >> in the front of a line? > irb(main):001:0> " spaces in front".gsub /^ +/,"" > => "spaces in front" > irb(main):002:0> RUBY_VERSION > => "1.8.6" > irb(main):003:0> > > Translation: > String#gsub applies the regexp given as first argument to a given > String, and replaces the matches with the second argument. > > The regexp "^ +" (the // notation is one of the many ways to tell Ruby > we are using a regexp) means "At the beginning of the line ("^"), find > any spaces(" "), and then stop ("+")". More or less, anyway (I'm not a > Regexp guru). ;) > > -- > Phillip Gawlowski > > a more convenient way is to use one of the strip methods irb(main):004:0> " spaces in front".lstrip => "spaces in front" irb(main):005:0> "spaces in back ".rstrip => "spaces in back" irb(main):006:0> " spaces on both side ".strip => "spaces on both side" -- Kind Regards, Rajinder Yadav http://DevMentor.org Do Good! - Share Freely