instance-variables

Initializing Instance Variable as an Array - Ruby

徘徊边缘 提交于 2020-01-01 06:27:08
问题 I'm trying to initialize and instance variable as an array as follows: class Arch < ActiveRecord::Base attr_accessor :name1 def initialize @name1 = [] end def add_name1(t) @name1 << t end end When I try Arch.new in a console session I get (Object doesn't support #inspect). What's up? How do I make an instance array variable? I tried to follow this like so: class Arch < ActiveRecord::Base attr_accessor :name1 def after_initialize @name1 = [] end def add_name1(t) @name1 << t end end and my

What is the python attribute get and set order?

穿精又带淫゛_ 提交于 2019-12-30 04:37:12
问题 Python provides us many possibilities on instance/class attribute, for example: class A(object): def __init__(self): self.foo = "hello" a = A() There are many ways to access/change the value of self.foo : direct access a.foo inner dict a.__dict__['foo'] get and set a.__get__ and a.__set__ ,of course there two are pre-defined methods. getattribute a.__getattribute__ __getattr__ and __setattr__ maybe more. While reading source code, I always get lost of what's their ultimate access order? When

Must all Python instance variables be declared in def __init__?

半城伤御伤魂 提交于 2019-12-30 04:33:07
问题 Or can they be declared otherwise? The code below does not work: class BinaryNode(): self.parent = None self.left_child = None Do they need to be declared in __init__ ? 回答1: They do not have to be declared in __init__ , but in order to set an instance variable using self , there needs to be a reference to self , and the place you are defining the variables does not. However, class BinaryNode(): parent = None left_child = None def run(self): self.parent = "Foo" print self.parent print self

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

天涯浪子 提交于 2019-12-28 08:11:41
问题 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? 回答1: 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

ruby: class instance variables vs instance variables

我的梦境 提交于 2019-12-28 01:28:11
问题 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

How to pass a default argument value of an instance member to a method?

徘徊边缘 提交于 2019-12-27 10:21:08
问题 I want to pass a default argument to an instance method using the value of an attribute of the instance: class C: def __init__(self, format): self.format = format def process(self, formatting=self.format): print(formatting) When trying that, I get the following error message: NameError: name 'self' is not defined I want the method to behave like this: C("abc").process() # prints "abc" C("abc").process("xyz") # prints "xyz" What is the problem here, why does this not work? And how could I make

How to pass a default argument value of an instance member to a method?

时光总嘲笑我的痴心妄想 提交于 2019-12-27 10:19:29
问题 I want to pass a default argument to an instance method using the value of an attribute of the instance: class C: def __init__(self, format): self.format = format def process(self, formatting=self.format): print(formatting) When trying that, I get the following error message: NameError: name 'self' is not defined I want the method to behave like this: C("abc").process() # prints "abc" C("abc").process("xyz") # prints "xyz" What is the problem here, why does this not work? And how could I make

how to i call a variable from the form.cs file to the program.cs file

拈花ヽ惹草 提交于 2019-12-25 08:49:49
问题 I am having trouble accessing two of my variables. I have looked on the internet and found that I need to use something like form.dlg.selectedpath to call it but I get three errors. One says form.dlg is inaccessible the next says an object reference is required. I also try to access another one and that says form does not contain definition for dlg2. This is the code I want the variables in. var di = new DirectoryInfo(Form1.dlg.SelectedPath); di.CopyTo(Form1.dlg2.SelectedPath, true); This is

Check if an instance variable has one or more objects?

痴心易碎 提交于 2019-12-25 07:28:43
问题 This is an example code, just to illustrate the problem. When I click one of the buttons I will load some instance variable into the view and render it. I don't know if the variable will have one or more objects inside. I therefore have to check if the variable has one or more objects inside, when it is loaded into the view (else the .each method will fail if there is only one object inside). Or is there a way to store just one object inside the variable as an array? aa.html.erb <div class=

Is it possible to define an integer-like object in Python that can also store instance variables?

為{幸葍}努か 提交于 2019-12-25 00:38:12
问题 Is it possible to define a data object in python that behaves like a normal integer when used in mathematical operations or comparisons, but is also able to store instance variables? In other words, it should be possible to do the following things: pseudo_integer = PseudoInteger(5, hidden_object="Hello World!") print(5 + pseudo_integer) # Prints "10" print(pseudo_integer == 5) # Prints "True" print(pseudo_integer.hidden_object) # Prints "Hello World!" 回答1: Yes, it is. You can create your own