在Ruby中,如何拒绝一个包含哈希值的数组中的元素?

问题描述 投票:0回答:1

例如,在Ruby中,如何拒绝一个包含哈希值的数组中的元素?

hash_var = ["1", "2", {"a" => ["aa", "bb", "cc"]}]
reject(hash_var, "1")             # should return ["2", {"a" => ["aa", "bb", "cc"]}]
reject(hash_var, "a")             # should return ["1", "2"]
reject(hash_var, {"a" => ["aa"]}) # should return ["2", {"a" => ["bb", "cc"]}]

基本上,我应该能够拒绝数组中的元素,无论是哈希还是数组元素。

arrays ruby-on-rails ruby hash
1个回答
0
投票

你可以在hash上使用删除方法,如

hash_var = ["1", "2", {"a" => ["aa", "bb", "cc"]}]
hash_var.delete("1") //this will delete 1 from hash then new value of hash will be ["2", {"a"=>["aa", "bb", "cc"]}]
hash_var[1]["a"].delete("aa") //this will give output ["2", {"a"=>["bb", "cc"]}]

诸如此类


0
投票

应该可以了

hash_var.drop_while { |e| e == '1' }
hash_var.reject { |e| e == '1' }
hash_var.find_all { |e| e != '1' }
hash_var.select { |e| e != '1' }

0
投票

你可以定义实例方法 Enumerable#dig_drop:

arr = ["1", "2", {"a" => ["aa", "bb", "cc"]}, {"b" => ["aa", "bb"]}]

module Enumerable
  def dig_drop(*elm)
    self.each_with_index {|_, idx|
      unless self[idx].respond_to? :dig
        self.delete(elm[0])
        next
      end
      last=elm.pop
      if elm.empty?
        self[idx].delete(last)
      else
        self[idx].dig(*elm).nil? ? nil : self[idx].dig(*elm).delete(last)
      end
    }
    self.reject!(&:empty?)
  end
end

arr.dig_drop "1"         # arr equals ["2", {"a" => ["aa", "bb", "cc"]}, {"b" => ["aa", "bb"]}]
arr.dig_drop('a')        # arr equals ["2", {"b" => ["aa", "bb"]}]  
arr.dig_drop('b', 'bb')  # arr equals ["2", {"b" => ["aa"]}

0
投票

我想这个方法应该可行。

def reject(hash_var, ele)
  # Find the index of the ele
  # If it is found, just remove it and return hash_var
  idx = hash_var.index(ele)
  if idx
    hash_var.delete_at(idx)
    return hash_var
  end

  # Convert the hashes to arrays - 2D arrays
  # { "b" => "2" } becomes [["b", "2"]]
  (0...hash_var.length).each do |n|
    if hash_var[n].class == Hash
      hash_var[n] = hash_var[n].to_a
    end
  end

  (0...hash_var.length).each_with_index do |n, i|
    # Only operate if hash_var[n] is an array
    # this means that this is 2D array i.e. hash
    if hash_var[n].class == Array
      # If this is just as element, delete the whole hash_key
      # i.e. reject(hash_var, "a")
      # here ele = "a", so since this is not hash
      # we need to delete full element i.e. ["aa", "bb", "cc"]
      if hash_var[n][0][0] == ele
        hash_var.delete_at(i)
      else
        next if ele.class == String
        ele_a = ele.to_a
        if hash_var[n][0][0] == ele_a[0][0]
          diff = hash_var[n][0][1] - ele_a[0][1]
          hash_var[n][0][1] = diff
        end
      end
    end
  end

  ## Convert the 2D arrays back to hashes
  (0...hash_var.length).each do |n|
    if hash_var[n].class == Array
      hash_var[n] = hash_var[n].to_h
    end
  end

  # return final
  return hash_var
end

测试:

hash_var = ["1", "2", {"a" => ["aa", "bb", "cc"]}]
p reject(hash_var, "1")
# => ["2", {"a"=>["aa", "bb", "cc"]}]

p reject(hash_var, "a")
# => ["1", "2"]

p reject(hash_var, {"a" => ["aa"]})
# => ["1", "2", {"a"=>["bb", "cc"]}]
© www.soinside.com 2019 - 2024. All rights reserved.