Regexp.last_match 线程安全吗? [英] Is Regexp.last_match thread safe?

查看:36
本文介绍了Regexp.last_match 线程安全吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我正在查看的代码:

This is the code that I'm looking at:

def method_missing(id, *args)
    return self.find(Regexp.last_match(1),  args[0]) if id.id2name =~ /find_by_(.+)/
    raise NoMethodError
end

如果我有多个线程调用 Regexp.last_match 会怎样?

What happens if I have multiple threads calling Regexp.last_match?

如果我有多个线程使用 method_missing 方法调用对象会怎样?

What happens if I have multiple threads calling the object with the method_missing method?

推荐答案

Ruby 1.9.2 平台文档声明调用 Regexp.last_match 等效于读取特殊的 $~ 全局变量.

The Ruby 1.9.2 platform docs state that calling Regexp.last_match is equivalent to reading the special $~ global variable.

来自The Ruby Programming Language",第 318 页:重要的是要记住 $~ 和从它派生的变量都是线程局部和方法局部的."

From "The Ruby Programming Language", pg 318: "it is important to remember that $~ and the variables derived from it are all thread-local and method-local."

所以 Regexp.last_match 线程安全的.至于您在 method_missing 中使用的其他方法,我相信它们也是线程安全的.(如果有人知道不同,请编辑此帖子.)

So Regexp.last_match is thread-safe. As for the other methods you are using in method_missing, I believe they are thread-safe as well. (If anybody knows differently, please edit this post.)

这篇关于Regexp.last_match 线程安全吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆