How can i remove a repeating string keywo开发者_运维百科rd from all elements in an array ?
I think you mean you have an array of strings and they all contain some substring that you want to remove. Non-destructively:
array.map {|s| s.gsub(keyword, '')}
Use destructive variants as desired to do it in-place.
Are you referring to string in the array, or non-unique elements. For the first, use the uniq method:
p ["foo", "bar", "foo", "baz"].uniq
["foo", "bar", "baz"]
For the latter, try something like:
p ["foo", "bar", "foo", "baz"].map { |x| x.gsub('oo', '') }
["f", "bar", "f", "baz"]
精彩评论