In Python, we can use the .strip()
method of a string to remove leading or trailing occurrences of chosen characters:
>>> print " (Removes (only) leading & trailing brackets & ws ) ".strip(" ()")
'Removes (only) leading & trailing brackets & ws'
How do we do this in Ruby? Ruby's strip
method takes no arguments and strips only whitespace.
There is no such method in ruby, but you can easily define it like:
def my_strip(string, chars)
chars = Regexp.escape(chars)
string.gsub(/\A[#{chars}]+|[#{chars}]+\z/, "")
end
my_strip " [la[]la] ", " []"
#=> "la[]la"