class-instance-variables

ruby: class instance variables vs instance variables

不问归期 提交于 2019-12-12 04:37:00
问题 my idea is to create a community wiki for people that come from a java background because reading a lot of explanations, I couldn't comprehend anything until I actually tried a couple of things and the pieces of the puzzle started to find their places. But I first need to make sure I'm getting it right. Coming from such background it was very confusing for me to find out that @variable may mean 2 very different things. Here is an example: class Test @ins = "gah" def self.ins puts @ins end def

Why is using a class variable in Ruby considered a 'code smell'?

[亡魂溺海] 提交于 2019-12-10 02:46:50
问题 According to Reek, creating a class variable is considered a 'code smell'. What is the explanation behind this? 回答1: As you can find in their documentation on Class Variables: Class variables form part of the global runtime state, and as such make it easy for one part of the system to accidentally or inadvertently depend on another part of the system. So the system becomes more prone to problems where changing something over here breaks something over there. In particular, class variables can

Why should @@class_variables be avoided in Ruby?

天涯浪子 提交于 2019-12-04 16:48:47
问题 I know that some say that class variables (e.g. @@class_var ) should be avoid in Ruby and should use the an instance variable (e.g. @instance_var ) in the class scope instead: def MyClass @@foo = 'bar' # Should not do this. @foo = 'bar' # Should do this. end Why is the use of class variables frowned upon in Ruby? 回答1: Class variables are often maligned because of their sometimes confusing behavior regarding inheritance: class Foo @@foo = 42 def self.foo @@foo end end class Bar < Foo @@foo =

Auto-incrementing IDs for Class Instances

孤街浪徒 提交于 2019-12-01 12:12:22
Disclaimer : This is for a semester project that I am currently working on. My question is regarding an implementation level detail and is not part of the grading scheme. I am only writing this code as a way to test the theory that I am proposing for the paper that I will write. Also, I have considered the answers for this question with little luck, so please do not consider this as a duplicate of that question The Problem : I have a graph (G=(V,E)). At some point in my algorithm, I need to turn this into a hypergraph (in a sense) by "collapsing" multiple nodes (say, v_1, v_2, ..., v_n ) into

New instance of class with a non-None class attribute?

时光总嘲笑我的痴心妄想 提交于 2019-11-30 17:56:36
问题 I have a Python class that has a class attribute set to something other than None . When creating a new instance, the changes made to that attribute perpetuates through all instances. Here's some code to make sense of this: class Foo(object): a = [] b = 2 foo = Foo() foo.a.append('item') foo.b = 5 Using foo.a returns ['item'] and foo.b returns 5 , as one would expect. When I create a new instance (we'll call it bar ), using bar.a returns ['item'] and bar.b return 5 , too! However, when I

How can Ruby's attr_accessor produce class variables or class instance variables instead of instance variables?

偶尔善良 提交于 2019-11-28 04:09:08
If I have a class with an attr_accessor , it defaults to creating an instance variable along with the corresponding getters and setters. But instead of creating an instance variable, is there a way to get it to create a class variable or a class instance variable instead? Like this: class TYourClass class << self attr_accessor :class_instance_variable end end You can look at this as opening the metaclass of the class (of which the class itself is an instance) and adding an attribute to it. attr_accessor is a method of class Class , it adds two methods to the class, one which reads the instance

ruby: class instance variables vs instance variables

半世苍凉 提交于 2019-11-27 02:06:55
my idea is to create a community wiki for people that come from a java background because reading a lot of explanations, I couldn't comprehend anything until I actually tried a couple of things and the pieces of the puzzle started to find their places. But I first need to make sure I'm getting it right. Coming from such background it was very confusing for me to find out that @variable may mean 2 very different things. Here is an example: class Test @ins = "gah" def self.ins puts @ins end def initialize() @ins = "wtf?" end def ins2 puts @ins end end As far as I understand, the first @ins is an

Ruby class instance variable vs. class variable

孤街醉人 提交于 2019-11-26 00:50:29
问题 I read \"When do Ruby instance variables get set?\" but I\'m of two minds when to use class instance variables. Class variables are shared by all objects of a class, Instance variables belong to one object. There\'s not much room left to use class instance variables if we have class variables. Could someone explain the difference between these two and when to use them? Here\'s a code example: class S @@k = 23 @s = 15 def self.s @s end def self.k @@k end end p S.s #15 p S.k #23 I understand

Difference between class variables and class instance variables?

倾然丶 夕夏残阳落幕 提交于 2019-11-26 00:45:19
Can anyone tell me about the difference between class variables and class instance variables? Wayne Conrad A class variable ( @@ ) is shared among the class and all of its descendants. A class instance variable ( @ ) is not shared by the class's descendants. Class variable ( @@ ) Let's have a class Foo with a class variable @@i , and accessors for reading and writing @@i : class Foo @@i = 1 def self.i @@i end def self.i=(value) @@i = value end end And a derived class: class Bar < Foo end We see that Foo and Bar have the same value for @@i : p Foo.i # => 1 p Bar.i # => 1 And changing @@i in one

Ruby class instance variable vs. class variable

强颜欢笑 提交于 2019-11-25 23:43:52
I read " When do Ruby instance variables get set? " but I'm of two minds when to use class instance variables. Class variables are shared by all objects of a class, Instance variables belong to one object. There's not much room left to use class instance variables if we have class variables. Could someone explain the difference between these two and when to use them? Here's a code example: class S @@k = 23 @s = 15 def self.s @s end def self.k @@k end end p S.s #15 p S.k #23 I understand now, Class Instance Variables are not passed along the inheritance chain! Instance variable on a class: