Ruby value of a hash key?

后端 未结 6 2242
暖寄归人
暖寄归人 2021-01-31 08:43

I\'ve got a list of values that are in a Ruby hash. Is there a way to check the value of the key and if it equals \"X\", then do \"Y\"?

I can test to see if the hash ha

6条回答
  •  無奈伤痛
    2021-01-31 09:08

    I didn't understand your problem clearly but I think this is what you're looking for(Based on my understanding)

      person =   {"name"=>"BillGates", "company_name"=>"Microsoft", "position"=>"Chairman"}
    
      person.delete_if {|key, value| key == "name"} #doing something if the key == "something"
    
      Output: {"company_name"=>"Microsoft", "position"=>"Chairman"}
    

提交回复
热议问题