循环访问 ruby 哈希并选择正则表达式值



嗨,我遇到了一些意想不到的事情,在任何地方都找不到答案。

我有一个哈希:

hash = {:thiskey => /value/, :anotherkey => /anothervalue/}

当我像这样迭代哈希时:

hash.each do |key, value| 
   puts key
   puts value
end

如果值是正则表达式/日历/....迭代器产生:

>>> thiskey
>>>(?-mix:calendar)

关于为什么会这样的任何想法?

谢谢!

(?-mix:calendar)是使用 ruby 时正则表达式的字符串表示形式。

>> a = /test(er)/
=> /test(er)/
>> print a.source
test(er)=> nil
>> print a
(?-mix:test(er))=> nil
>> 
(?-mix:...)

表示"对于正则表达式的这一部分,Doall 模式、不区分大小写模式和详细模式被关闭"(这是默认值)。表示只是明确这一点。

最新更新