1

How can i remove a repeating string keyword from all elements in an array ?

0

2 Answers 2

6

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.

Sign up to request clarification or add additional context in comments.

3 Comments

here's a destructive (in-place) example: array.each {|word| word.delete!('aeiou')}
here's another destructive (in-place) example: array.map! {|word| word.gsub(keyword,'')}
we are the destructoglenns
1

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"]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.