73 questions
0
votes
2
answers
121
views
How to define a class instance variable in Ruby Struct subclass?
How can I define a variable that has shared access by instance methods of a Struct?
I can use a global variable $tmp like this:
Triple = Struct.new :x, :y, :z do
def mul s
$tmp.x = x * s
$...
0
votes
1
answer
26
views
Get the variable associated with a JPanel component from a component array
Say I have a JPanel with buttons (and other things) as components and I make an array of those components as such:
JPanel panel = new JPanel();
JButton btn1 = new JButton();
JButton btn2 = new ...
0
votes
1
answer
99
views
Class Inheritance python require fill child class name
it might be a silly question since I'm new to Python.
However, I hope someone can explain this because I try to find lots of resource but still hardly understand the mechanism behind.
So I create a ...
0
votes
1
answer
28
views
aiohttp instance variable values shared by different users?
I am testing an aiohttp app with web.run. I instantiate an imported class just after the import declaration and the a value of this instance is changed by data channel( for instance.changevalue() ...
0
votes
3
answers
1k
views
How to get the maximum value among the arguments of class objects in Python? [duplicate]
I have sample class and instances:
class A:
def __init__(self, num)
#instances
a = A(2)
b = A(4)
c = A(5)
d = A(7)
In another class (class B) within a method, I have to get the highest value ...
1
vote
1
answer
378
views
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<Piece> pieceClass) {
Piece p = ...
-1
votes
3
answers
3k
views
getting field value from class instance in python
I don't understand how to get the value of a field instance from python class. I added what I need this code to do in get_person method. The output should be 3 or None.
class Digit:
def __init__(...
1
vote
1
answer
1k
views
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 {
...
0
votes
1
answer
308
views
Can't figure out how to create a "list" instance variable using class in Python
This is my code:
class Category():
def __init__(self,category,balance=0,ledger=[]):
self.category = category
self.balance = balance
self.ledger = ledger
def ...
1
vote
1
answer
57
views
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
...
0
votes
3
answers
3k
views
Java how to call method in another class
Sorry for the basic question but I'm learning, new to programming. I am trying to call a method that is in another class but I'm unable to without passing in the required values for the constructor of ...
0
votes
2
answers
27
views
@coffee_hour = Plan.find_by_event(:coffee_hour) ...Is it possible to link using a variable like this?
My Links table has five different events and I use partials because they are scattered around the page. As an examples, "coffee_hour", and "wine_time." Each event has a url and a time. In my ...
1
vote
1
answer
533
views
Does Ruby's define_method have special access to instance variables? [duplicate]
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 ...
2
votes
1
answer
2k
views
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 ...
0
votes
2
answers
303
views
Create a new instance of parent's class variable for each child class
I have this parent class:
class ListBase
@@list = []
def self.list
@@list
end
end
I'm trying to create two child classes like so:
class Child1 < ListBase
end
class Child2 < ...
0
votes
1
answer
249
views
Django Field Instances overriding each others arguments
I am testing and preparing a new Django package for using bleach with Text and Char fields in the Django ORM and with DRF. I've hit a bit of a roadblock with it however and it has made me take pause ...
0
votes
0
answers
117
views
Hierarchical abstraction in c++
I have following abstraction in my code:
class A{};
class B: public A{};
class C: public B{};
Now when I declare an instance of class C every thing is OK. The problem is when I declare class C in a ...
0
votes
0
answers
72
views
how classes can inherit tk.frames when being called as a module in python
I am a tk newbie and am having trouble importing classes and having them inherit frames. If I stack all the code in one file it works. Something like (I found this on Github sorry for no attribution),
...
0
votes
1
answer
196
views
Why are statically bound methods not involved in the CIR ( Class instance Record ) of the class while dynamically bound methods are?
I read in Sebesta that static bound methods do not need to be stored in the CIR but i cant figure out why. If it is not stored in the CIR, how does the compiler know which statically bound method is ...
0
votes
1
answer
386
views
instance_variable_set is not setting the right variable in an instance
I have an API endpoint I'm testing using Ruby. I have 8 tests that look like the following:
it "must rate limit on this api" do
body = {
..
}
# Current limits assigned to this api ...
0
votes
2
answers
213
views
Choosing a controller for a layout class instance variable in Rails
I have a DateTime class instance variable @current_year = DateTime.now.year, which I have set into a footer partial. This partial is rendered across several of my clientside pages (and across multiple ...
0
votes
0
answers
30
views
Can python class variable be used as both class and instance variable? [duplicate]
I am very confused after working with Django Framework and the model of Djano Framework
class User(models.Model):
first_name = models.CharField(max_length=20)
last_name = models.CharField(...
1
vote
1
answer
64
views
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 ...
6
votes
1
answer
2k
views
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
...
0
votes
1
answer
40
views
Python: Can't search through list of class instance variables
I'm trying to create a chemistry GUI that shows various information about each element. I'm using a list of class instances to print out the information, but I continue to get a 'list' object has no ...
0
votes
1
answer
95
views
Ruby: Nested Array behaving strangley as an Instance Variable
For our assignment, we were meant to create tic-tac-toe on a board of any size and accept an array as the input. I still am not fully grasping the attr_accessor module, and I'm pretty sure I used it ...
0
votes
1
answer
154
views
Class instance variable not available to child class?
I want to leverage class methods on child classes in ruby, but those that rely on child instance variables are not working. I was told "don't use class variables! (@@)", so I'm not. How can I make ...
0
votes
1
answer
808
views
Set Subclass Variables/Attributes from Within Base Class
Is it possible to dynamically create/set variables within a subclass within the base class across multiple subclasses without affecting the other subclasses?
For example, take this code here:
class ...
0
votes
2
answers
4k
views
Accessing Class variables from another class
How do you access an instance in an object and pass it to another 'main' object? I'm working with a parser for a file that parses different tags, INDI(individual), BIRT(event), FAMS(spouse), FAMC(...
10
votes
2
answers
3k
views
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?
0
votes
1
answer
701
views
Python- how to query database by class
I have a Python project (I'm quite new to Python), and on one of the webpages, there is a drop-down box which should display a list of all of the projects whose 'status' fields are set to 'live'.
It ...
1
vote
1
answer
81
views
Why can't a class instance variable be accessed in a singleton-class definition in Ruby?
class MyClass
@my_class_instance_variable = "here"
p @my_class_instance_variable # => "here"
class << self
p @my_class_instance_variable # => nil
end
end
class MyClass
p @...
1
vote
2
answers
1k
views
C# display image in WPF from a custom class other than MainWindow : Window
Hello I have a simple WPF window and I want to load and display a logo image on specific "Image" item. The following code works perfectly and I can display the logo.
namespace WPFexample
{
public ...
1
vote
1
answer
92
views
How to declare Class Instance Variable in inherited class automatically
I am using Class Inheritance Variables in Ruby to keep track of the number of instances I've created so far. To keep my code DRY, I implemented most of the logic in a base class that all my other ...
-1
votes
1
answer
617
views
Class instance variable is coming as `nil` in instance method
I assigned a class instance variable as an array.
class Red
@items = ["brinjal", "banana"]
puts @items.inspect
def test
puts @items.inspect
end
end
p = Red.new # => prints ["brinjal",...
5
votes
3
answers
4k
views
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 ...
4
votes
4
answers
4k
views
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 ...
2
votes
4
answers
498
views
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 &...
0
votes
2
answers
85
views
What is the difference between instance variables in a class or using them as parameters in a constructor in Java?
How is using studentName and studentAverage different in the class or in the constructor?
public class StackOverFlowQ {
String studentName;
int studentAverage;
public void ...
1
vote
1
answer
56
views
class instance variables proper usage
class variable works like this:
class Hello
@@x = 0
def self.counter
@@x
end
def initialize
@@x += 1
end
end
Hello.new
Hello.new
Hello.new
p Hello.counter
#=> 3
but class ...
5
votes
3
answers
4k
views
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 ...
4
votes
3
answers
203
views
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 ...
1
vote
1
answer
194
views
How to declare a class instance variable in Ruby?
I need a class variable which does not get inherited, so I decided to use a class instance variable. Currently I have this code:
class A
def self.symbols
history_symbols
end
private
def ...
1
vote
4
answers
914
views
Relation between class instance and static methods
Is there any particular relation between class instance (e.g variables that declared by static/final) and static methods(e.g class methods) ?
I hop you understand what i mean.
3
votes
4
answers
662
views
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
@...
1
vote
2
answers
443
views
How can I pass an object instance in a method call in C#?
I have a class Club which has a list _list as an argument, while 'Player' is another class ... I have this method for adding the club members, thas is implemented this way:
public void AddPlayer(...
0
votes
1
answer
73
views
anyway to access parent's properties who ware overwritten by child?
is there any work around to access the parents values that ware overwritten by child?
parent::$prop: expect to be static. and the same with: self::$prop
class base {
public $name = 'base';
...
1
vote
0
answers
151
views
Ruby/Rails: trying to invent an inherited resource gem. Class instance variable in mixin
I want to DRY my views and controllers from a lot of similar code. I want to do it by myself in educational purpose, I know about the InheritedResourse gem.
So far I wrote:
class Admin::...
1
vote
4
answers
292
views
Why are my instance variables not existing across methods of the same class in ruby?
I am doing the ruby koans and I am on the DiceSet project. I've made the DiceSet class but my instance variables don't seem to persist with the instance like I thought they would. My code is
class ...
3
votes
1
answer
252
views
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 ...