Ruby regex key search

前端 未结 4 1914
情书的邮戳
情书的邮戳 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:54

    I am not aware of an absolute answer for this question but if I were to write a hacky method for this, I would do this

    !!hash.keys.detect{ |k| k.to_s =~ /Your_Regex_here/ }
    

    This will return true when any key matches regex and false otherwise.

提交回复
热议问题