简体   繁体   中英

How to remove a specific value from an array stored in a ruby hash

I am getting this in my error list :

@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."]
                  }

Here I want to remove the value "can't be blank" value from this hash so that I will get the validation error messages which was included by me.

is it possible to remove "can't be blank" value from above hash list and I will get this in result :

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

How to remove a specific value from a hash list(want to remove a specific value not a complete key,value pair).

Yes, possible.

@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."]}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM