Strip with some other character

strip function removes the trailing whitespace. i want to remove
trailing comma's. is there a additional parameter for strip or some
other function to do the same.

string.gsub(/^,+/, '').gsub(/,+$/, '')

martin

···

On Thu, Aug 7, 2008 at 5:03 PM, Junkone <junkone1@gmail.com> wrote:

strip function removes the trailing whitespace. i want to remove
trailing comma's. is there a additional parameter for strip or some
other function to do the same.

a = "Ruby,"
a.chomp(',')
# a.chomp!(',')
# but this will remove only the last (one) ','

···

On Aug 8, 5:04 am, Junkone <junko...@gmail.com> wrote:

strip function removes the trailing whitespace. i want to remove
trailing comma's. is there a additional parameter for strip or some
other function to do the same.

This can be done with a single gsub:

irb(main):001:0> s=",a,"
=> ",a,"
irb(main):002:0> s.gsub %r{^,+|,+$}, ''
=> "a"

However, both strip trailing and leading commas. So for trailing
commas this should be sufficient

irb(main):003:0> s.gsub %r{,+$}, ''
=> ",a"

OP: And of course, use gsub! for inplace modification if you want that.

Kind regards

robert

···

2008/8/8 Martin DeMello <martindemello@gmail.com>:

On Thu, Aug 7, 2008 at 5:03 PM, Junkone <junkone1@gmail.com> wrote:

strip function removes the trailing whitespace. i want to remove
trailing comma's. is there a additional parameter for strip or some
other function to do the same.

string.gsub(/^,+/, '').gsub(/,+$/, '')

--
use.inject do |as, often| as.you_can - without end