instance-variables

Why do my controller's instance variables not work in views (Rails)

I would like to add a couple of instance variables to my controller, since the variables in question are required from within more than one action's view. However, the below example does not work as I would expect. class ExampleController < ApplicationController @var1 = "Cheese" @var2 = "Tomato" def show_pizza_topping # What ...

Outputting iVars from description method?

I am pretty sure I am just missing the point here and getting confused. Can anyone tell me how I might write a simple description for an object that will print out the value of its instance variables to the console. Also: is there anyway to present the information as a block (i.e. if you had 10 iVars its going to be a pain getting them ...

Objective C - access BOOL ivar of void pointer to self

I have a thing that uses a SystemSoundID to play a sound, and then repeat with a C function being used as the sound completion callback, and the function is passed (void *)self (since it has to be a void pointer), and then I want to play the sound again if the alarmPlaying BOOL ivar of self is true, otherwise remove the sound completion ...

Constant instance variables?

I use 'property' to ensure that changes to an objects instance variables are wrapped by methods where I need to. What about when an instance has an variable that logically should not be changed? Eg, if I'm making a class for a Process, each Process instance should have a pid attribute that will frequently be accessed but should not be ...

Properties and Instance Variables in Objective-C

I'm rather confused about properties and instance variables in Objective-C. I'm about half-way through Aaron Hillegass's "Cocoa Programming for Mac OS X" and everything is logical. You would declare a class something like this: @class Something; @interface MyClass : NSObject { NSString *name; NSArray *items; Something *so...

[Ruby] Declaring instance variables iterating over a hash!!

Hi, i want to do the following: I want to declare the instance variables of a class iterating over a dictionary. Let's assume that i have this hash hash = {"key1" => "value1","key2" => "value2","key3" => "value3"} and i want to have each key as instance variable of a class. I want to know if i could declare the variables iterating o...

Declaring an object at class level, problems. iPhone Objective-C

Objective C on iPhone: I am attempting to declare the following object at class level so that i do not have to re-connect this socket every time I write something to it. (Writing things multiple times a second) whenever i am writing a steady stream of live data, about every 2 seconds it freezes. I don't know why, but it does. Code: Soc...

Instance variable: self vs @

I saw a code class Person def initialize(age) @age = age end def age @age end def age_difference_with(other_person) (self.age - other_person.age).abs end protected :age end What I want to know is how difference between using @age and self.age in age_difference_with method. ...

Object-Oriented Perl constructor syntax

I'm a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I don't get what this line does at all, but I always see it. Do I need it? my $type = shift; #I'm turning the array of inputs into a has...

How to get instance from string in C#?

Is it possible to get the property of a class from string and then set a value? Example: string s = "label1.text"; string value = "new value"; label1.text = value; <--and some code that makes this How to do this? ...

Java Instance Variables vs Local Variables

Hi, I'm in my first programming class in high school. We're doing our end of the first semester project. This project only involves one class, but many methods. My question is best practice with instance variables and local variables. It seems that it would be much easier for me to code using almost only instance variables. But I'm not ...

Property Refuses to Synthesize

I've been going through the screencasts here to learn how to write a table-based iPhone application, and it's been going pretty smoothly so far. Presently I'm halfway through the third episode, and it's starting to hit a snag. In order to remove temporary hard-coding for the top layer of the table, the tutorial creates an NSMutableDici...

Should I access Ivars directly within a class implementation?

I've been sort of on the fence on this one for a while, but I'd like to know what people think about accessing instance variables directly from within an Objective-C class implementation? Using accessors and mutators makes a lot of things easy, but for simple things, is it bad to access the instance variable directly? Is the best practi...

Marking instance variables @private

I've noticed that a lot of Apple's interfaces use @private before their instance variable declarations. Is there a good design reason for this, and is it something I should do? ...

Delphi: constant class instance

If I define a record, I can define an instance of that record as a constant record e.g. Tunit = record _name: string; _scale: real; end; const metre: Tunit = (_name: 'metre'; _scale: 1.0; ) I need to do something similar where Tunit is a Class and metre is an instance of the class, but is a (particular) instance whose asso...

Instance variable initialization in java

Hi I have doubt in instance variable initialization in java.I have seen different developers initializing varibles differently.In the below sample codes what is better way.Do we have any advantage of initializing variables inside the constructor? .. Example 1 code is easy to read.Can anyone clarify this..... Example 1: class A { B b...

SELF keyword in Objective-C

In a project I'm creating, I have various classes. One of my classes has an instance of NSMutableArray that holds objects of another one of my classes. I thought I had a firm understanding on this topic, but somehow it got jumbled up in my mind again. When initializing an instance of this class, I have this initilize method: - (MMShowM...

How to maintain data across object instances in Objective-C?

I've had some experience developing for the iPhone but am entirely self taught and am trying to improve my practices. This is a question that is probably pretty introductory to programming. What is the best way (or is it possible) to maintain instance variables with values that are common to all instances of an object? Is it possible to...

Hide instance variable from header file in Objective C

I came across a library written in Objective C (I only have the header file and the .a binary). In the header file, it is like this: @interface MyClass : MySuperClass { //nothing here } @property (nonatomic, retain) MyObject anObject; - (void)someMethod; How can I achieve the same thing? If I try to declare a property without i...

How do you use an instance variable with mailer in Ruby on Rails?

I created an instance variable (@user) in the model of a mailer and want to access it in the view? But it's giving me an an error (@user = nil). What's the best way to pass a variable to view (the email body)? Thanks, Chirag ...