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
3
votes
4 answers

Is there any way to get class instance attributes without creating class instance?

Here is the link to my first question: Creating class instance from dictionary? So I am trying to create class instance from dictionary which holds keys that class has not. For example: class MyClass(object): def __init__(self, value1,…
Demyanov
  • 901
  • 2
  • 10
  • 15
3
votes
4 answers

How deny creating instance variables in Ruby

I would like to deny creating instance variables in Ruby,to prevent unattended variables being created 'by mistake'. My class: class Test def initialize @a = 'Var A' end def make_new @b = 'Var B' <-- I would like to deny creation of…
3
votes
1 answer

python how to save new objects into a list without duplication

High-level picture of my program purpose: parse an XML file and save text into similar python objects problem: Every time I create a new python object and append it to a list, instead of creating a new object it seems to append a reference to the…
2
votes
2 answers

Objective-C pattern for class instance variables?

What would be a nice pattern in Objective-C for class variables that can be "overridden" by subclasses? Regular Class variables are usually simulated in Objective-C using a file-local static variables together with exposed accessors defined as Class…
2
votes
4 answers

Should I use class variables or class-instance variables for class static variables in Ruby?

class Something @@variable = 'Class variable' def give_me @@variable end end class OtherThing @variable = 'Instance variable with an interface' class << self attr_accessor :variable end def give_me …
Zequez
  • 3,399
  • 2
  • 31
  • 42
2
votes
4 answers

Making a class instance variable unwritable from outside world

class Event @event_list = {} attr_reader :name, :value def initialize(name, value) @name = name @value = value end def to_s "#{@value}" end class << self def event_list …
Deepak
  • 341
  • 1
  • 10
2
votes
3 answers

Eclipse. Run two different mains when the second reads a static field of the previous

I have a Java project in eclipse which is divided in two parts; two different main classes that run two different Threads basically. One contains loading, initialization and debug-showing procedures that are quite slow. While, the other manipulates…
1
vote
2 answers

Class instance variables in ActiveRecord (Ruby On Rails)

I am trying to store a value in a model class, for example, values of couple of checkboxes. I figured I could store them in a class instance variable, however, these values are cleared when I a user clicks elsewhere on the screen. How can I…
Karan
  • 14,824
  • 24
  • 91
  • 157
1
vote
1 answer

Can I use a class instance as an argument for a generic in java?

I am passing a class as a parameter to a method and I need to create an instance of that class, something like public void setPiece(int i0, int j0, Class pieceClass) { Piece p = null; try { Constructor
1
vote
3 answers

How can I statistic instance numbers of each class and the memory they consumed in the peak time in native C++ project

My compiler project has a serious memory-consuming. So I want to find a method that can find out which class is the worst one. It should give me something like bellow: -------------------------------------------------------------------- Class name,…
Jerrfey
  • 65
  • 5
1
vote
1 answer

instantiate typescript class instance with constructor parameters

I'm trying to create a function that triggers the class's init method and returns the instantiated class, but I'm not able to preserve the constructor and class types. I tried: class Test { constructor(a: number, b: string, c?: number[]) {} …
Slinidy
  • 367
  • 1
  • 4
  • 12
1
vote
1 answer

Why the list variable named nodes in the class is taking the output of the previous instance of the class?

class Node: def __init__(self, value): self.data = value self.next = None class SinglyLinkedList: nodes = [] def __init__(self): self.head = None self.tail = None def…
1
vote
1 answer

Does Ruby's define_method have special access to instance variables?

I am learning Ruby and have stumbled upon some code similar to the one below, which shows the difference between instance variables and class instance variables. I've tested it in my console and it works like described (outputs "John"). What I don't…
wawa
  • 11
  • 4
1
vote
1 answer

Why equality check for instance of Struct/Class are different?

I don't understand the difference between struct and class equality check. Since both Struct and Class gets their #hash from Kernel but they seem to behave differently. I know that instance.hash will produce a different result for each class…
MorboRe'
  • 151
  • 10
1
vote
1 answer

Ruby class variable scope issue

Ok, so I've read up on class variables, class instance variables and instance variables in Ruby. I found this explanation by Phrogz very illuminating. Now I'm trying to add a variable to my class that is accessible by multiple methods within my…
megahra
  • 295
  • 2
  • 19