Ruby开发:如何从regions数组中排除指定元素DDA
Hey there! Let's break down what's going on here and fix your issue. First, let's look at why your previous attempts didn't work, then go over the correct ways to exclude 'DDA' from your regions array.
Your Current Code
Here's the code you provided for context:
def supported_regions(partition) if @@supported_regions_by_partition_cache[partition].nil? regions = xrp_supported_regions({ignore_build_status: IGNORE_BUILD_STATUS}) .map { |region| rip_helper.get_region(region) } .select { |region| region.arn_partition == partition } .sort_by(&:region_name) .map(&:airport_code) @@supported_regions_by_partition_cache[partition] = regions else regions = @@supported_regions_by_partition_cache[partition] end regions end
Why Your Previous Tries Failed
regions.delete('DDA'): This might not have worked because of caching. If you ran this after the first call tosupported_regions, the cache already stored the full array—modifyingregionsafter fetching it from the cache won't update the stored value. Also,deletemodifies the original array, which could affect other parts of your code that rely on the cache..reject {|s| 'DDA' != s }: You got the condition backwards!rejectremoves elements that match the condition. This line is saying "remove every element that isn't 'DDA'"—leaving you with only 'DDA' instead of excluding it. Oops!
Correct Solutions
1. Fix the reject Condition (One-Time Filter)
If you just need to filter the regions for a single use (without modifying the cache), use reject with the right condition:
# Get the full regions array, then exclude 'DDA' filtered_regions = supported_regions(partition).reject { |code| code == 'DDA' }
This returns a new array without 'DDA' and leaves the original cached array untouched.
2. Filter When Caching (Permanent Exclusion)
If you never want 'DDA' to be included in the cached regions at all, add the reject step directly when building the array. This way, the cache stores the already filtered list, so every call to supported_regions will automatically exclude 'DDA':
def supported_regions(partition) if @@supported_regions_by_partition_cache[partition].nil? regions = xrp_supported_regions({ignore_build_status: IGNORE_BUILD_STATUS}) .map { |region| rip_helper.get_region(region) } .select { |region| region.arn_partition == partition } .sort_by(&:region_name) .map(&:airport_code) .reject { |code| code == 'DDA' } # Add this line to exclude 'DDA' before caching @@supported_regions_by_partition_cache[partition] = regions else regions = @@supported_regions_by_partition_cache[partition] end regions end
Note: If you already ran the original code, you'll need to clear the cache (restart your app or reset @@supported_regions_by_partition_cache) for this change to take effect.
3. Alternative: delete_if (Modify Original Array)
If you do want to modify the original array (not recommended if it's cached, since it affects all users of the cache), you can use delete_if:
regions.delete_if { |code| code == 'DDA' }
This is similar to reject but changes the array in place instead of returning a new one.
Quick Check
Make sure the elements in your regions array are actually strings matching 'DDA' exactly (case-sensitive!). If they're symbols (like :DDA) or have extra whitespace, you'll need to adjust the condition (e.g., code.to_s == 'DDA').
内容的提问来源于stack exchange,提问作者pralxx

