class-variables

Is it thread safe to set Active Resource HTTP authentication on a per-user basis?

只愿长相守 提交于 2019-11-27 15:08:50
Active Resource can make use of HTTP authentication set at the class level. For instance: class Resource < ActiveResource::Base end Resource.user = 'user' Resource.password = 'password' or Resource.site = "http://user:password@site.com/" But what if I use different HTTP authentication based on which user is logged in? If I change Resource.user and Resource.password, is that going to cause a race condition where requests from one thread suddenly start using the authentication of a user whose requests are running simultaneously in a different thread? Or is this a non-issue (as long as I reset

Ruby class variables

自闭症网瘾萝莉.ら 提交于 2019-11-27 13:57:43
The ruby class-instance stuff is giving me a headache. I understand given this... class Foo @var = 'bar' end ...that @var is a variable on the created class's instance. But how do I create a sub-class overridable class variable? Here is an example of what I would do in Python: class Fish: var = 'fish' def v(self): return self.var class Trout(Fish): var = 'trout' class Salmon(Fish): var = 'salmon' print Trout().v() print Salmon().v() Which outputs: trout salmon How do I do the same thing in ruby? To contrast @khelll's answer, this uses instance variables on the Class objects: class Fish # an

Class variables in Ruby

安稳与你 提交于 2019-11-27 07:37:08
问题 I've come across the following example from this tutorial: class Song @@plays = 0 def initialize(name, artist, duration) @name = name @artist = artist @duration = duration @plays = 0 end def play @plays += 1 @@plays += 1 "This song: #@plays plays. Total #@@plays plays." end end s1 = Song.new("Song1", "Artist1", 234) # test songs s2 = Song.new("Song2", "Artist2", 345) puts s1.play puts s2.play puts s1.play puts s1.play Is @@plays politely accessible only inside the class Song? This commentary

python subclass access to class variable of parent

北城余情 提交于 2019-11-27 07:13:52
I was surprised to to learn that a class variable of a subclass can't access a class variable of the parent without specifically indicating the class name of the parent: >>> class A(object): ... x = 0 ... >>> class B(A): ... y = x+1 ... Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 2, in B NameError: name 'x' is not defined >>> class B(A): ... y = A.x + 1 ... >>> B.x 0 >>> B.y 1 Why is it that in defining B.y I have to refer to A.x and not just x? This is counter to my intuition from instance variables, and since I can refer to B.x after B is

why are java constants declared static?

限于喜欢 提交于 2019-11-27 02:30:35
问题 Why are java constants declared static ? class Foo{ static final int FII = 2 ; } In this I understand the use of final? Buy why does it have to be static? Why should it be a class variable, and not an instance variable? 回答1: If it could vary by the instance of a class, then it's clearly not a constant . What would it mean to get a different value of pi for each instance of Math (not that Math even allows instances to be constructed)? Or a different case insensitive ordering for each instance

What is the difference between an instance and a class (static) variable in Java [closed]

情到浓时终转凉″ 提交于 2019-11-27 01:42:00
问题 The title of this question is actually a previous examination question and I am looking for clarification / an answer to it. Please note that I am learning Java and am becoming familiar with its syntax. I understand that this question may have been asked before and if so can someone please show me where I may access the question if possible? Also please accept my apologies if this is the case. To show that I have been researching this area, my own understanding is that instance variables

Create module variables in Ruby

帅比萌擦擦* 提交于 2019-11-27 00:01:03
问题 Is there any way to create a variable in a module in Ruby that would behave similar to a class variable? What I mean by this is that it would be able to be accessed without initializing an instance of the module, but it can be changed (unlike constants in modules). 回答1: Ruby natively supports class variables in modules, so you can use class variables directly, and not some proxy or pseudo-class-variables: module Site @@name = "StackOverflow" def self.setName(value) @@name = value end def self

What does @@variable mean in Ruby?

佐手、 提交于 2019-11-26 18:05:54
What are Ruby variables preceded with double at signs ( @@ )? My understanding of a variable preceded with an at sign is that it is an instance variable, like this in PHP: PHP version class Person { public $name; public function setName($name) { $this->name = $name; } public function getName() { return $this->name; } } Ruby equivalent class Person def set_name(name) @name = name end def get_name() @name end end What does the double at sign @@ mean, and how does it differ from a single at sign? A variable prefixed with @ is an instance variable , while one prefixed with @@ is a class variable .

Is it thread safe to set Active Resource HTTP authentication on a per-user basis?

北城以北 提交于 2019-11-26 17:03:49
问题 Active Resource can make use of HTTP authentication set at the class level. For instance: class Resource < ActiveResource::Base end Resource.user = 'user' Resource.password = 'password' or Resource.site = "http://user:password@site.com/" But what if I use different HTTP authentication based on which user is logged in? If I change Resource.user and Resource.password, is that going to cause a race condition where requests from one thread suddenly start using the authentication of a user whose

Ruby class variables

眉间皱痕 提交于 2019-11-26 16:33:37
问题 The ruby class-instance stuff is giving me a headache. I understand given this... class Foo @var = 'bar' end ...that @var is a variable on the created class's instance. But how do I create a sub-class overridable class variable? Here is an example of what I would do in Python: class Fish: var = 'fish' def v(self): return self.var class Trout(Fish): var = 'trout' class Salmon(Fish): var = 'salmon' print Trout().v() print Salmon().v() Which outputs: trout salmon How do I do the same thing in