【问题标题】:How to remove a specific value from an array stored in a ruby hash如何从存储在 ruby​​ 哈希中的数组中删除特定值
【发布时间】:2014-03-18 19:40:15
【问题描述】:

我的错误列表中出现了这个:

@error_messages = {
                   :password=>["can't be blank", "Password is required."], 
                   :"addresses.firstname"=>["can't be blank","Firstname is required."],
                   :"addresses.city"=>["can't be blank", "city is required."]
                  }

在这里,我想从该哈希中删除“不能为空”的值,以便获得我包含的验证错误消息。

是否可以从上面的哈希列表中删除“不能为空”的值,我会在结果中得到这个:

       @error_messages = {
                          :password=>["Password is required."],
                          :"addresses.firstname"=>["Firstname is required."],
                          :"addresses.city"=>["city is required."]
                         }

如何从哈希列表中删除特定值(想要删除特定值而不是完整的键值对)。

【问题讨论】:

    标签: ruby arrays hashset


    【解决方案1】:

    是的,可能。

    @error_messages = {
                       :password=>["can't be blank", "Password is required."], 
                       :"addresses.firstname"=>["can't be blank","Firstname is required."],
                       :"addresses.city"=>["can't be blank", "city is required."]
                      }
    
    @error_messages.each do |_,v|
       v.delete( "can't be blank" )
    end
    
    @error_messages
    # => {:password=>["Password is required."],
    #     :"addresses.firstname"=>["Firstname is required."],
    #     :"addresses.city"=>["city is required."]}
    

    【讨论】:

      猜你喜欢
      • 2019-01-02
      • 2015-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多