Ruby: Easiest Way to Filter Hash Keys?

前端 未结 13 1205

I have a hash that looks something like this:

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


        
13条回答
  •  鱼传尺愫
    2020-11-30 18:45

    If you want the remaining hash:

    params.delete_if {|k, v| ! k.match(/choice[0-9]+/)}
    

    or if you just want the keys:

    params.keys.delete_if {|k| ! k.match(/choice[0-9]+/)}
    

提交回复
热议问题