Ruby regex key search

前端 未结 4 2083
情书的邮戳
情书的邮戳 2021-02-04 13:28

http://ruby-doc.org/core-1.9.3/Hash.html#method-i-include-3F

It is possible to convert hash.has_key?(String) to have a regex search capabilities?

4条回答
  •  星月不相逢
    2021-02-04 13:55

    If you are only interested in a yes/no answer, then any? might be a good option:

    hash.keys.any? { |key| key.to_s.match(regexp)}
    

    where regexp is a regular expression.

提交回复
热议问题