in Python, we can e.g.
"[foo ]".strip(" []") # -> "foo"
how do we do this in Ruby? string strip
method is stripping only whitespace ..
UPDATE
stripping is not deleting! : )
"[ foo boo ]".strip(" []") # -> "foo boo"
in Python, we can e.g.
"[foo ]".strip(" []") # -> "foo"
how do we do this in Ruby? string strip
method is stripping only whitespace ..
UPDATE
stripping is not deleting! : )
"[ foo boo ]".strip(" []") # -> "foo boo"
Try the gsub method:
irb(main):001:0> "[foo ]".gsub(/\As+[/,'')
=> "foo ]"
irb(main):001:0> "foo ]".gsub(/s+]\Z/,'')
=> "foo"
etc.
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"
"[[ ] foo [] boo ][ ]".gsub(/\A[ \[\]]+|[ \[\]]+\Z/,'')
=> "foo [] boo"
Can also be shortenend to
"[[ ] foo [] boo ][ ]".gsub(/\A[][ ]+|[][ ]+\Z/,'')
=> "foo [] boo"