Ruby: Easiest Way to Filter Hash Keys?

前端 未结 13 1197

I have a hash that looks something like this:

params = { :irrelevant => \"A String\",
           :choice1 => \"Oh look, another one\",
           :choi         


        
13条回答
  •  时光取名叫无心
    2020-11-30 18:38

    As for bonus question:

    1. If you have output from #select method like this (list of 2-element arrays):

      [[:choice1, "Oh look, another one"], [:choice2, "Even more strings"], [:choice3, "But wait"]]
      

      then simply take this result and execute:

      filtered_params.join("\t")
      # or if you want only values instead of pairs key-value
      filtered_params.map(&:last).join("\t")
      
    2. If you have output from #delete_if method like this (hash):

      {:choice1=>"Oh look, another one", :choice2=>"Even more strings", :choice3=>"But wait"}
      

      then:

      filtered_params.to_a.join("\t")
      # or
      filtered_params.values.join("\t")
      

提交回复
热议问题