[rspec-users] class << self considered harmful... really?

Aslak Hellesøy aslak.hellesoy at gmail.com
Thu Nov 27 00:28:07 EST 2008


When (if) this thread ends, let's start a discussion about indentation  
conventions!

kthxbye

> Avdi Grimm wrote:
>> On Wed, Nov 26, 2008 at 12:17 PM, Matt Wynne <matt at mattwynne.net>  
>> wrote:
>>
>>>> And class variables are problematic in Rails in development mode  
>>>> because
>>>> of class reloading.
>>>>
>>> And more generally dangerous for threading reasons, right?
>>>
>>
>>
>> All of the above.  My rule of thumb is: don't use them unless I  
>> have a
>> very, very good reason.  I can't think of the last time I had that
>> good a reason.
>>
>>
>
> So.. you are saying that this is preferred:
>
> class State < AR:Base
>
> def self.names
>    @names ||= State.all.map{|s| s.name}
> end
>
> end
>
> to this:
>
> class State < AR:Base
>
> def self.names
>    @@names ||= State.all.map{|s| s.name}
> end
>
> end
>
> Is that correct?
>
> How is using a class instance variable safer from a threading point  
> of view?  Do threads not share class instance variables the same way  
> class variables would be shared?
>
> -Ben
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users



More information about the rspec-users mailing list