Questions tagged [class-instance-variables]

in ruby, where classes are objects, this means the instance variables of the class object itself

Here is an example:

class Test
  @ins1 = "gah"

  def initialize()
    @ins2 = "wtf?"
  end
end

@ins2 is an instance variable and @ins1 is a class instance variable. This might hopefully shed some more light over it: ruby: class instance variables vs instance variables

73 questions
217
votes
8 answers

Ruby class instance variable vs. class variable

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…
Elmor
  • 4,775
  • 6
  • 38
  • 70
96
votes
3 answers

Difference between class variables and class instance variables?

Can anyone tell me about the difference between class variables and class instance variables?
cmd
  • 1,013
  • 1
  • 8
  • 8
29
votes
2 answers

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

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…
14
votes
2 answers

Why should @@class_variables be avoided in Ruby?

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…
ab217
  • 16,900
  • 25
  • 74
  • 92
11
votes
3 answers

In Ruby are there any related applications of the syntax: class << self ... end

class << self attr_accessor :n, :totalX, :totalY end The syntax above is used for defining class instance variables. But when I think about what syntax implies, it doesn't make any sense to me, so I'm wondering if this type of syntax is used for…
10
votes
2 answers

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

According to Reek, creating a class variable is considered a 'code smell'. What is the explanation behind this?
Douglas
  • 381
  • 4
  • 12
6
votes
1 answer

Assign module method to a Class variable or Instance variable

In module a.py def task(): print "task called" a = task class A: func = task # this show error unbound method #func = task.__call__ # if i replace with this work def __init__(self): self.func_1 = task …
Kallz
  • 3,244
  • 1
  • 20
  • 38
6
votes
4 answers

In Ruby, in the context of a class method, what are instance and class variables?

If I have the following piece of Ruby code: class Blah def self.bleh @blih = "Hello" @@bloh = "World" end end What exactly are @blih and @@bloh? @blih is an instance variable in the class Blah, and @@bloh is a class variable in the…
Pablo Fernandez
  • 279,434
  • 135
  • 377
  • 622
5
votes
2 answers

Can a class in ruby store the number of instantiated objects using a @class_instance_variable and not a @@class_variable?

I am trying to keep count of instances of objects of a given class, inside the class that defines those objects. First of all I am aware of code reflection and ObjectSpace.each_object, but I'd prefer not to use reflection and let the class itself be…
Redoman
  • 3,059
  • 3
  • 34
  • 62
4
votes
4 answers

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

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 =…
Logan Bibby
  • 1,167
  • 1
  • 10
  • 31
4
votes
3 answers

How do I use dictionary key,value pair to set class instance attributes "pythonic"ly?

I have created some Python classes to use as multivariate data structures, which are then used for various tasks. In some instances, I like to populate the classes with various value sets. The default parameter filename "ho2.defaults" would look…
Corey Petty
  • 63
  • 2
  • 3
4
votes
3 answers

ruby: class instance variables vs instance variables

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…
akostadinov
  • 17,364
  • 6
  • 77
  • 85
4
votes
3 answers

Initialization of "final" instance variables

I would like to understand initialization of class instances in various cases.. In JLS-7 Section 12.5, there was no mention of how and when final instance variables were initialized? Can some one point me reference to understand the behaviour in…
Pushparaj
  • 1,039
  • 1
  • 6
  • 26
3
votes
4 answers

Auto-incrementing IDs for Class Instances

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…
inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241
3
votes
2 answers

Adding a class instance variable and attr_reader to Ruby class at runtime?

How do I add a class instance variable, the data for it and a attr_reader at runtime? class Module def additional_data member, data self.class.send(:define_method, member) { p "Added method #{member} to #{name}" } end end For…
Zabba
  • 64,285
  • 47
  • 179
  • 207
1
2 3 4 5