不知道如何根据条件从数组中删除哈希



我试图从数组中删除一些哈希,如果哈希中的特定键包含或包括一些特定的单词。

BANNED_WORDS = ['Hacked', 'hack', 'fraud', 'hacked']
data = [
{
"news_url": "https://www.benzinga.com/markets/cryptocurrency/21/10/23391043/north-vancouver-to-heat-buildings-with-bitcoin-mining",
"image_url": "https://crypto.snapi.dev/images/v1/m/v/fw-69939.jpeg",
"title": "North Vancouver To Heat Buildings With Bitcoin Mining",
"text": "Canadian hack Bitcoin (CRYPTO: BTC) mining firm MintGreen has partnered with state-owned Lonsdale Energy Corporation (LEC) to heat 100 residential and commercial buildings in North Vancouver with recovered energy from crypto mining.",
"source_name": "Benzinga",
"date": "Fri, 15 Oct 2021 12:16:19 -0400",
"topics": [
"mining"
],
"sentiment": "Neutral",
"type": "Article",
"tickers": [
"BTC"
]
},
{
"news_url": "https://u.today/ethereum-20-next-steps-to-mainnet-shared-by-ethereum-foundation",
"image_url": "https://crypto.snapi.dev/images/v1/b/t/10169-69937.jpg",
"title": "Ethereum 2.0 Next Steps to Mainnet Shared by Ethereum Foundation",
"text": "Ethereum (ETH) developers have entered final phase of testing before hotly anticipated ETH1-ETH2 transition",
"source_name": "UToday",
"date": "Fri, 15 Oct 2021 12:11:00 -0400",
"topics": [],
"sentiment": "Neutral",
"type": "Article",
"tickers": [
"ETH"
]
}
]

我试图删除文本或标题包含/包括上面BANNED_WORDS数组中的任何单词的任何散列。

我已经尝试了以下和其他一些变化,但似乎没有工作。我是新的ruby,有人可以指出我做错了什么,谢谢。

data.select{|coin| coin[:text].split(" ").select{ |word| !BANNED_WORDS.include?(word) || coin[:title].split(" ").select{ |word| !BANNED_WORDS.include?(word)}}

所以结果应该是:

filtered_result = [
{
"news_url": "https://u.today/ethereum-20-next-steps-to-mainnet-shared-by-ethereum-foundation",
"image_url": "https://crypto.snapi.dev/images/v1/b/t/10169-69937.jpg",
"title": "Ethereum 2.0 Next Steps to Mainnet Shared by Ethereum Foundation",
"text": "Ethereum (ETH) developers have entered final phase of testing before hotly anticipated ETH1-ETH2 transition",
"source_name": "UToday",
"date": "Fri, 15 Oct 2021 12:11:00 -0400",
"topics": [],
"sentiment": "Neutral",
"type": "Article",
"tickers": [
"ETH"
]
}
]

这是一个正则表达式的作业。

R = /b(?:#{BANNED_WORDS.join('|')})b/
#=> /b(?:Hacked|hack|fraud|hacked)b/
data.reject { |h| h[:title].match?(R) || h[:text].match?(R) }
#=> [{:news_url=>"https://u.today/ethereum-20-next-steps...,
#     ...
#     :tickers=>["ETH"]}]

看到Regexp #比赛吗?。

正则表达式中的b是一个字边界。它们的存在是为了防止'haskintosh''defraud'的匹配。

最新更新