Ruby: filter array removing blacklisted names

Related searches

Say I have an array of sentences:

sentences = ["Tom is a good person", "Jack spent some time", "Kat did something wrong"]

and I have a blacklist of names:

blacklist = ["Jack", "Kat"]

Now I need to filter sentences into an array that removes all the sentences that contain blacklisted names, so:

["Tom is a good person"]

How would I do it in Ruby?

Thanks!

sentences = ["Tom is a good person", "Jack spent some time", "Kat did something wrong",
             "Kathy knows her stuff"]

blacklist = ["Jack", "Kat"]

r = /\b#{Regexp.union(blacklist)}\b/
  #=> /\b(?-mix:Jack|Kat)\b/
sentences.reject { |s| s.match?(r) }
  #=> ["Tom is a good person", "Kathy knows her stuff"]

Word breaks (\b) are needed in the regular expression so that "Kat" does not match the first three letters of "Kathy". One could instead write:

r = /\b#{blacklist.join('|')}\b/
  #=> /\bJack|Kat\b/ 

How to remove nil elements from an array in Rails, Here i am demonstrating the frequently used methods for removing nil elements from an array. 1> using present method with reject. copytext. >> [� This allows you to filter using the index, instead of the object (in this case a string) itself. In-Place Array Filtering. Using select on an array always creates a new array. If instead you want to change the original array, you can use the select! method. Here's how:

You just need to reject the records

sentences.reject!{|sentence| sentence.match(blacklist.join('|'))}

You will get the required output -

["Tom is a good person"]

Docs for reject! - https://ruby-doc.org/core-2.2.0/Array.html#method-i-reject-21

reject! will update the same array, if you don't want that, you can use reject and store in a new array.

Go back to =~ :)

sentences.reject!{|sentence| !((Regexp.new(blacklist.join('|')) =~ sentence).nil?) }

Reject the sentence, if `=~' finds a match ( Code used - !nil? )

This is going to do essentially the same thing. Although ruby says, match is faster

How to Filter Arrays of Data in Ruby Quick Tips, Often when writing Ruby programs, either in Rails or say as part of a script, its useful to be able to select or filter da The PHP Unit Testing framework. Contribute to sebastianbergmann/phpunit development by creating an account on GitHub.

sentences = ["Tom is a good person", "Jack spent some time", "Kat did something wrong"]

blacklist = ["Jack", "Kat"]

Program

p sentences.filter{|string|!(blacklist.map{|x|string.match?(x)}.any?)}

If you are using < Ruby 2.6 then

p sentences.select{|string|!(blacklist.map{|x|string.include?(x)}.any?)}

Result

["Tom is a good person"]

encoding: utf-8 require "logstash/filters/base" require "logstash , prune filter is for removing fields from events based on # whitelists or blacklist of field names or This can e.g. be useful if you have a <<plugins-filters-json, json>> # or [source,ruby] # filter { # %PLUGIN% { # whitelist_names => [ " method", :array, :default => [] # Exclude fields whose names match specified regexps,� I've been trying to filter this array to only show the first unique 0 index of an array within an array. My array is already sorted by the 1 index.

chrisvfritz/language_filter: LanguageFilter is a Ruby gem to , Name. Latest commit message. Commit time. config � removed violet blue from Symbol signifying a pre-packaged list; An array of words and phrases to is a Ruby gem to detect and optionally filter multiple categories of language. For example, LanguageFilter uses matchlist and exceptionlist instead of blacklist and � Browse other questions tagged ruby-on-rails arrays ruby or ask your own question. The Overflow Blog Tales from documentation: Write for your clueless users

Prune filter plugin | Logstash Reference [7.9], The prune filter is for removing fields from events based on whitelists or blacklist of field names or their values (names and values can also be regular expressions ). In case field values are arrays, each array item is matched against memcached � metricize � metrics � mutate � prune � range � ruby � sleep� Use filter() find rows/cases where conditions are true. Unlike base subsetting with [ , rows where the condition evaluates to NA > are dropped.</p>

How To Work with Arrays in Ruby. An array is a data structure that represents a list of values, called elements. Arrays let you store multiple values in a single variable. This can condense and organize your code, making it more readable and maintainable.

Comments
  • Thanks let me try.
  • I might be doing something wrong, but it complains match isn't defined.
  • Sorry I might be using an old version of ruby (2.3.7) and I got: undefined method filter' for #<Array:0x00007f9bf4e796f8>`
  • @hzxu Okay, Now updated another method for you which works for you.
  • I have to change filter to select and change match to include.
  • @hzxu Perfectly okay then.