On Tue, Oct 16, 2012 at 9:38 AM, Hans Mackowiak <lists / ruby-forum.com> wrote:
> or both regexp combined:
> "#hit#here#".gsub(/(^#)|(\#$)/,'')  #=> "hit#here"

Few remarks: you do not need the capturing groups.  And using \A and
\z is better since it will be really the beginning and the end of the
string.  Consider

irb(main):008:0> s = "aaaa\n#bbb#cc#\n"
=> "aaaa\n#bbb#cc#\n"
irb(main):009:0> s.gsub /^#|\#$/, ''
=> "aaaa\nbbb#cc\n"

Note: the backslash before the second # is needed to avoid string
interpolation confusion.

According to the original specification neither the # before "bbb" has
to be removed nor the one after "cc".  On the contrary with \A and \z
the string remains unchanged which is what should happen:

irb(main):010:0> s.gsub /\A#|#\z/, ''
=> "aaaa\n#bbb#cc#\n"
irb(main):011:0> s == s.gsub(/\A#|#\z/, '')
=> true

Kind regards

robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/