Class Variables - Static.

21 views
Skip to first unread message

Rupesh Jayaprakash

unread,
May 26, 2014, 3:16:26 AM5/26/14
to rubyonra...@googlegroups.com
Hello,

When setting the class static variables, These are shared across all the
instances created.

Class A
@@bool_var = true

def get_bool_var
@@bool_var
end
end

All the instances will have this static variable,
When through a controller, a user requests, Based on some logic, I set
the value to false.

Is this class static variable be set to false in all the other requests
from other users on a web application ?

Thank You.

--
Posted via http://www.ruby-forum.com/.

Matt Jones

unread,
May 26, 2014, 7:29:02 AM5/26/14
to rubyonra...@googlegroups.com


On Monday, 26 May 2014 03:16:26 UTC-4, Ruby-Forum.com User wrote:
Hello,

When setting the class static variables, These are shared across all the
instances created.

Class A
  @@bool_var = true

  def get_bool_var
    @@bool_var
  end
end

All the instances will have this static variable,
When through a controller, a user requests, Based on some logic, I set
the value to false.

Is this class static variable be set to false in all the other requests
from other users on a web application ?


The class variable will lose its value between requests in development mode due to class reloading. You SHOULD NOT use class variables to do this in production, as the value is only available in the process where it was set, making multi-process deployments behave in ways you don't expect.

--Matt Jones

Rupesh Jayaprakash

unread,
May 26, 2014, 2:44:35 PM5/26/14
to rubyonra...@googlegroups.com
Thank You Jones for your answer,

I was suspecting that my class variable will not be the same at the same
request. I understand that on a multi-process deployment things would be
different.
Reply all
Reply to author
Forward
0 new messages