class-variables

Ruby and class variables in inherit class

为君一笑 提交于 2019-12-24 03:07:07
问题 class A def set(v) @@v = v end def put puts @@v end end class B < A end class C < A end B.new.set 'b' B.new.put # => b C.new.set 'c' C.new.put # => c B.new.put # => c Why? And how should I write this to have 'b' in last B.new.put? 回答1: Here is a nice article on the subject - Class and Instance Variables In Ruby. Basically, what you can do is: class A class << self attr_accessor :class_var end def set_class_var(value) self.class.class_var = value end def get_class_var self.class.class_var end

Class variable: public access read-only, but private access r/w

旧巷老猫 提交于 2019-12-23 13:09:03
问题 In my current project I have a class which stores its Instance in a variable. This Instance should be accesible by all other classes in the project, but it may only be altered by its own class. How can I achieve this? 回答1: Write a public getter but no public setter. And the field itself private 回答2: In short that is called immutable object , state of Object cannot change after it is constructed. String is a common example of immutable Class . Make a class immutable by following- ensure the

Confusion about ruby class variable

半世苍凉 提交于 2019-12-23 09:42:52
问题 Assume a simple ruby program using a class variable, class Holder @@var = 99 def Holder.var=(val) @@var = val end def var @@var end end @@var = "top level variable" a = Holder.new puts a.var I guess the result should be 99 , but the output is not 99 . I wonder why. Since class variable's scope is the class, I assume the line @@var = "top level variable" would not affect the variable in the class. 回答1: @@var is a class variable of Holder . And the @@var at the top level is not the same class

PHP performance: $this->variable versus local $variable (manipulating)

北慕城南 提交于 2019-12-23 09:27:20
问题 I had a section in a class that I decided to split into a new one. When I had ported the code section into a new class I noticed it was considerably slower at executing one of the foreach loops. I managed to track down part of the problem to be how I decided to save the final result array. I think it'll be easier to understand if you see a shortened version of my code: The original ported code: http://pastebin.com/2iBuqmgn More optimized ported code: http://pastebin.com/TYU1rHwU You'll see

What is the difference between local, instance, and class variables? [duplicate]

醉酒当歌 提交于 2019-12-23 02:07:22
问题 This question already has answers here : Closed 6 years ago . Possible Duplicate: Difference between class variables and class instance variables? While reading a Ruby book, I seem to have missed the variables chapter. Now I can't seem to understand the following things: What is an instance variable? What is a class instance variable? What is the difference between a variable , @instance_var and @class_instance_var ? I tried to read some posts in different blogs, but I still do not understand

java singleton pattern, should all variables be class variables?

家住魔仙堡 提交于 2019-12-22 18:42:10
问题 If a class implements a singleton pattern, should all the variables be declared static? Is there any reason they shouldn't be declared static? Does it make a difference? 回答1: No. The singleton pattern just means that a single instance is the only instance -- it does not mean "make everything statically accessible". The singleton pattern gives you all the benefits of a "single instance", without sacrificing the ability to test and refactor your code. Edit: The point I'm trying to make is that

java singleton pattern, should all variables be class variables?

℡╲_俬逩灬. 提交于 2019-12-22 18:41:13
问题 If a class implements a singleton pattern, should all the variables be declared static? Is there any reason they shouldn't be declared static? Does it make a difference? 回答1: No. The singleton pattern just means that a single instance is the only instance -- it does not mean "make everything statically accessible". The singleton pattern gives you all the benefits of a "single instance", without sacrificing the ability to test and refactor your code. Edit: The point I'm trying to make is that

how to access a class variable of outer class from inner class in ruby

穿精又带淫゛_ 提交于 2019-12-21 16:52:33
问题 i have some code in Ruby here below: class A @@lock = Monitor.new class B def method @@lock.synchronize puts "xxxxx" end end end end after running it throws an error which said that below: uninitialized class variable @@lock in A::B (NameError) if i want to know how to access the outer class A's class variable @@lock from inner class B's method, how to do it? thank you in advance. 回答1: The only way to access this class variable is via an accessor method class A def self.lock @@lock ||=

Example of something that is not thread-safe in Rails

本秂侑毒 提交于 2019-12-21 04:34:09
问题 I've seen threads like this on thread-safety in Rails and various web pages on the topic, and I'm sure everyone's great at reciting what it is and giving 'tips' on what isn't thread-safe ("class variables!"), but I can never seem to find a clear, simple, complete example of something that is actually not thread-safe in Rails , to the point where I wonder if anyone actually understands it at all. I would be grateful if someone could prove me wrong and give: a clear, simple, complete example of

instance variable, class variable and the difference between them in ruby

点点圈 提交于 2019-12-19 09:23:43
问题 I am having a hard time understanding instance variable, class variable and the difference between them in ruby... can someone explain them to me? I have done tons of Google searches, just can't understand them fully. Thank you! 回答1: Let's say you define a class. A class can have zero or more instances. class Post end p1 = Post.new p2 = Post.new Instance variables are scoped within a specific instance. It means if you have an instance variable title , each post will have its own title. class