instance-variables

Python - Get Instance Variables

Is there a built-in method in Python to get an array of all a class' instance variables? For example, if I have this code: class hi: def __init__(self): self.ii = "foo" self.kk = "bar" Is there a way for me to do this: >>> mystery_method(hi) ["ii", "kk"] Thanks guys! Edit: I originally had asked for class variables erron...

smalltalk singleton pattern: how do I initialize the instance variables?

I'm having trouble in getting the singleton pattern to initialize a instance variable in smalltalk. (here is a link to another implementation for clarification) this is what I have: new ^UniqueInstance ifNil: [UniqueInstance := self basicNew. UniqueInstance: instanceVar := Object new. ]. that last line (UniqueInsta...

Initialising an instance variable with a method from the class (Java)

Hello, can I initialize an instance variable in Java, when I initialise it when I declare it, and initialise it with the return value of a method, which I define later in the class. Something like this: public class MyClass { integers[] myArray = new integers[length()]; int length() { .... } } length() gives me...

How to access instance variables from one class while inside another class

I'm really new to Ruby. And by new - less than 16 hours, but my boss gave me some Ruby code to add to. However, I found it was one giant file and not modular at all, so I decided to clean it up. Now that I've broken it up into several files/classes (generally speaking, 1 class per file,) I'm having problems piecing it together for it to ...

Ruby headscratcher - instance variable isn't outputted

I'm probably doing something stupid but I can't figure out what it is. The output I'm seeing from this program is foo test What I'm expecting to see is foo abc test Does anyone see anything obviously wrong here? class Foo def initialize(l) @label = l end def label @label end def abc @abc en...

Assigning values to Instance variables in Objective C

The function I'm looking at: -(void)viewDidLoad { NSBundle *bundle = [NSBundle mainBundle]; NSString *plistPath = [bundle pathForResource:@"statedictionary" ofType:@"plist"]; NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:plistPath]; self.statesZips = dictionary; [dictionary release]; N...

How can I initialize a module's instance variables in Ruby?

I have some modules where I would like to use instance variables in. I'm currently initializing them like this : module MyModule def self.method_a(param) @var ||= 0 # other logic goes here end end I also could call a init method to initialize them : def init @var = 0 end but this would mean I have to remember to alw...

Rails Active Record Instance Variables

Hello My questions is in regards to this AR and its instance variable @saved class PhoneNumber < ActiveRecord::Base has_one :user validates_presence_of :number def self.create_phone_number( user, phone_hash ) @new_phone = PhoneNumber.new(phone_hash) @user = user PhoneNumber.transaction do @user.phone_numbers << @new_phone @new...

What is the exact definition of instance variable?

For me instance variables are simple data types like int or double. Everything that is created automatically when the object is created. If an object creates additional objects - like everything that is it done with the NEW keyword - these are not instance variables. Am I right or wrong? ...

What is the correct (or best) way to subclass the Python set class, adding a new instance variable?

I'm implementing an object that is almost identical to a set, but requires an extra instance variable, so I am subclassing the built-in set object. What is the best way to make sure that the value of this variable is copied when one of my objects is copied? Using the old sets module, the following code worked perfectly: import sets cla...

What is the difference between a property and an instance variable?

I think I've been using these terms interchangably / wrongly! ...

PHP show names of all declared classes?

Lets say I have this code: <?php class hello { var $greeting = "hello"; function hello(){ echo $this->greeting; return; } } $hello1 = new hello; $hello2 = new hello; $hello4 = new hello; ?> How do I get it to echo all the names of instantiated objects (and if possible their respective class), so that it ec...

When do Ruby instance variables get set?

class Hello @hello = "hello" def display puts @hello end end h = Hello.new h.display I created the class above. It doesn't print anything out. I thought the instance variable @hello was set during the class declaration. But when I call the display method the output is 'nil'. What's the correct way to do this? ...

When to use @ in a Rails View and when to use a symbol?

<% form_tag(:action=>'update', :id=>@album.id) do %> Title: <%= text_field(:album, :title) %><br> Artist: <%= text_field(:album, :artist) %><br> Genre: <%= text_field(:album, :genre) %><br> Release Date: <%= datetime_select(:album, :release_date, :start_year=>1960) %><br> <%= submit_tag("Update") %> <% end %> In the...

Have you ever used a "class instance variable" in any of your Ruby code?

I can understand why you would need a class variable to keep track of things like the total number of objects that have been instantiated in that class. And I can understand why you would need an instance variable to store attributes of a particular object in that class. But class instance variables I just can't seem to justify. As I ...

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 instance variable instead? ...

Should you only use local variables in a partial?

Using local variables seems advisable in a partial that could be used application-wide to avoid dependencies across the application. But within a single controller it seems acceptable to reference instance variables that you know will be available in all of the actions that use the partial. If you do this, there seems to be a risk, h...

Objective-C Basic Class question

So I'm a bit rusty getting back into programming and I can't seem to find a good reference for understanding the structure for what I am trying to achieve. So, without further ado I am looking at creating and Class object such as. #import Assets.h @interface MainRecord: NSObject { Assets* assets; ... } ... @end Having a class...

Why are my ActiveRecord class instance variables disappearing after the first request in development mode?

I have a class instance variable on one of my AR classes. I set its value at boot with an initializer and, after that, never touch it again except to read from it. In development mode, this value disappears after the first request to the web server. However, when running tests, using the console or running the production server this d...

How to make a real private instance variable?

I want to make an instance variable that can't be accessed from outside. Is something like that possible in objective-c? I remember Apple has private variables and stuff like that, but if people know about them, they can use them. Apple calls that "private API", but obviously others can access that stuff if they find out what's in there....