何时以及为什么应该使用类和全局变量?

时间:2022-09-20 10:34:53

I am using Ruby on Rails 3.0.7 and I would like to know when and why class (@@) and global ($) variables should be used and if in this case those are properly used.

我正在使用Ruby on Rails 3.0.7,我想知道何时以及为什么应该使用class(@@)和global($)变量,如果在这种情况下正确使用它们。

P.S.: I am asking this question because I am having the mentioned case-problem and in an answer is proposed the use of a class variable. I am grateful you can explain me if in that case it is good to use that.

P.S。:我问的是这个问题,因为我遇到了上述案例问题,并在答案中建议使用类变量。我很感激你可以解释我,如果在那种情况下使用它是好的。

2 个解决方案

#1


3  

The short answer is: never.

简短的回答是:从不。

I'm new to ruby, but I do know this much from other languages: global variables are never thread-safe.

我是ruby的新手,但我从其他语言中知道这一点:全局变量从不是线程安全的。

Along the same lines, avoid the singleton pattern like the plague unless you only ever deal with a unique thread.

沿着同样的路线,除非你只处理一个独特的线程,否则避免像瘟疫这样的单身模式。


Edit:

As an aside, googling for ruby dependency injection suggests that Ruby doesn't need any of it. Well, tell you what. It does.

另外,谷歌搜索ruby依赖注入表明Ruby不需要任何东西。好吧,告诉你什么。确实如此。

Because it was always assumed it doesn't, there are mountains of gems and libraries and what not out there. They basically assume you only ever have a single thread and IO blocking. Had they not done so from the start, they might have been thread safe and non-blocking instead. But currently, they just aren't.

因为它总是假设没有,所以有大量的宝石和图书馆以及那里没有的东西。他们基本上假设你只有一个线程和IO阻塞。如果他们从一开始就没有这样做,那么他们可能是线程安全的而且非阻塞的。但目前,他们并非如此。

And had they done so, they would have been playing better with event-driven servers too.

如果他们这样做了,那么他们在使用事件驱动的服务器时也会玩得更好。

As things stand, it's a bloody mess.

事实上,这是一个血腥的混乱。

Event Machine is not thread-safe. Thin and Goliath aren't by the same token. Rack-async is basically monkey patching the whole thing. Passenger uses fork and is only smart if you install REE/1.8.7 with rails. Mongrel is thread safe but IO-blocking. Webrick is single-threaded and IO blocking. The list goes on. It's just messy.

事件机器不是线程安全的。瘦和歌利亚不是出于同样的原因。 Rack-async基本上是猴子修补整个事情。如果您使用rails安装REE / 1.8.7,Passenger使用fork并且只是智能的。 Mongrel是线程安全的但是IO阻塞。 Webrick是单线程和IO阻塞。名单还在继续。这太乱了。

#2


0  

Class variables can be used for a quick-and-dirty form of caching.

类变量可用于快速和脏的缓存形式。

class Thing
  def expensiveCall
    unless @@expensiveResult
      @@expensiveResult = ['foo','bar','baz'] # or whatever
    end
    @@expensiveResult
  end
end

#1


3  

The short answer is: never.

简短的回答是:从不。

I'm new to ruby, but I do know this much from other languages: global variables are never thread-safe.

我是ruby的新手,但我从其他语言中知道这一点:全局变量从不是线程安全的。

Along the same lines, avoid the singleton pattern like the plague unless you only ever deal with a unique thread.

沿着同样的路线,除非你只处理一个独特的线程,否则避免像瘟疫这样的单身模式。


Edit:

As an aside, googling for ruby dependency injection suggests that Ruby doesn't need any of it. Well, tell you what. It does.

另外,谷歌搜索ruby依赖注入表明Ruby不需要任何东西。好吧,告诉你什么。确实如此。

Because it was always assumed it doesn't, there are mountains of gems and libraries and what not out there. They basically assume you only ever have a single thread and IO blocking. Had they not done so from the start, they might have been thread safe and non-blocking instead. But currently, they just aren't.

因为它总是假设没有,所以有大量的宝石和图书馆以及那里没有的东西。他们基本上假设你只有一个线程和IO阻塞。如果他们从一开始就没有这样做,那么他们可能是线程安全的而且非阻塞的。但目前,他们并非如此。

And had they done so, they would have been playing better with event-driven servers too.

如果他们这样做了,那么他们在使用事件驱动的服务器时也会玩得更好。

As things stand, it's a bloody mess.

事实上,这是一个血腥的混乱。

Event Machine is not thread-safe. Thin and Goliath aren't by the same token. Rack-async is basically monkey patching the whole thing. Passenger uses fork and is only smart if you install REE/1.8.7 with rails. Mongrel is thread safe but IO-blocking. Webrick is single-threaded and IO blocking. The list goes on. It's just messy.

事件机器不是线程安全的。瘦和歌利亚不是出于同样的原因。 Rack-async基本上是猴子修补整个事情。如果您使用rails安装REE / 1.8.7,Passenger使用fork并且只是智能的。 Mongrel是线程安全的但是IO阻塞。 Webrick是单线程和IO阻塞。名单还在继续。这太乱了。

#2


0  

Class variables can be used for a quick-and-dirty form of caching.

类变量可用于快速和脏的缓存形式。

class Thing
  def expensiveCall
    unless @@expensiveResult
      @@expensiveResult = ['foo','bar','baz'] # or whatever
    end
    @@expensiveResult
  end
end