instance-variables

Local variables set to nil? (Objective-C)

拜拜、爱过 提交于 2019-11-27 12:08:51
I'm reading a book on Objective-C and the author said that if local variables aren't assigned a value they will be set to nil, but static variables will be set to zero. So, I set up int a and didn't assign it a value. Then NSLog(@"%i", a) to display it and a was displayed as zero. I was a little confused on that and I was wondering if someone could clarify it for me? With ARC enabled, your Objective-C object pointer variables will be set to nil regardless of where you create them. Without ARC, and for built in C types, your variables will not be initialized. Instance variables of Objective-C

python: What happens when class attribute, instance attribute, and method all have the same name?

巧了我就是萌 提交于 2019-11-27 11:36:29
问题 How does python differentiate a class attribute, instance attribute, and method when the names are the same? class Exam(object): test = "class var" def __init__(self, n): self.test = n def test(self): print "method : ",self.test test_o = Exam("Fine") print dir(test_o) print Exam.test print test_o.test test_o.test() Output : ['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '_

Python - should all member variables be initialized in __init__

戏子无情 提交于 2019-11-27 11:28:38
问题 Maybe this is more of a style question than a technical one but I have a python class with several member variables and I want to have it work so that some of the member variables are initialized when the user first creates an instance of the class (i.e. in the __init__ function) and I want the other member variables to be defined from arguments of member functions that will be called later on. So my question is should I initialize all member variables in the __init__ function (and set the

Properties and Instance Variables in Objective-C

夙愿已清 提交于 2019-11-27 10:23:37
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 *something; IBOutlet NSTextField *myTextField; } @property (nonatomic, retain) NSString *name; @property (nonatomic, retain) NSArray *items; Since other objects need to manipulate our name and items instance variables, we use @property / @synthesize to generate accessors/mutators

When do Ruby instance variables get set?

你说的曾经没有我的故事 提交于 2019-11-27 10:11:25
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? sris Instance variables in ruby may be a bit confusing when first learning Ruby, especially if you are accustomed to another OO language like Java. You cannot simply declare an instance variable. One of the most important things to know about instance variables in ruby, apart from the

Access instance variable from outside the class

╄→гoц情女王★ 提交于 2019-11-27 09:43:30
问题 If an instance variable belongs to a class, can I access the instance variable (e.g. @hello ) directly using the class instance? class Hello def method1 @hello = "pavan" end end h = Hello.new puts h.method1 回答1: Yes, you can use instance_variable_get like this: class Hello def method1 @hello = "pavan" end end h = Hello.new p h.instance_variable_get(:@hello) #nil p h.method1 #"pavan" - initialization of @hello p h.instance_variable_get(:@hello) #"pavan" If the variable is undefined (first call

Ruby Metaprogramming: dynamic instance variable names

耗尽温柔 提交于 2019-11-27 09:20:27
问题 Let's say I have the following hash: { :foo => 'bar', :baz => 'qux' } How could I dynamically set the keys and values to become instance variables in an object... class Example def initialize( hash ) ... magic happens here... end end ... so that I end up with the following inside the model... @foo = 'bar' @baz = 'qux' ? 回答1: The method you are looking for is instance_variable_set. So: hash.each { |name, value| instance_variable_set(name, value) } Or, more briefly, hash.each &method(:instance

Directly accessing an instance variable vs. Using an accessor method

独自空忆成欢 提交于 2019-11-27 09:15:25
问题 Can anyone explain the difference between accessing an instance attribute via self.attribute and by @attribute ? 回答1: self.attribute calls the method attribute . self.attribute = value calls the method attribute= with the argument value . @attribute and @attribute = value get/set the value of the instance variable @attribute . So basically they're two entirely different things. However if you call attr_accessor :attribute it defines the method attribute to return @attribute and the method

Ruby convert Object to Hash

雨燕双飞 提交于 2019-11-27 09:05:07
问题 Let's say I have a Gift object with @name = "book" & @price = 15.95 . What's the best way to convert that to the Hash {name: "book", price: 15.95} in Ruby, not Rails (although feel free to give the Rails answer too)? 回答1: class Gift def initialize @name = "book" @price = 15.95 end end gift = Gift.new hash = {} gift.instance_variables.each {|var| hash[var.to_s.delete("@")] = gift.instance_variable_get(var) } p hash # => {"name"=>"book", "price"=>15.95} Alternatively with each_with_object :

Total newbie: Instance variables in ruby?

杀马特。学长 韩版系。学妹 提交于 2019-11-27 08:06:13
Pardon the total newbiew question but why is @game_score always nil? #bowling.rb class Bowling @game_score = 0 def hit(pins) @game_score = @game_score + pins end def score @game_score end end Let's walk through the code, shall we? #bowling.rb class Bowling @game_score = 0 # (1) At this point (1), we are still inside the class Bowling . Remember: classes are just objects like any other. So, at this point you are assigning 0 to the instance variable @game_score of the class object Bowling . def hit(pins) @game_score = @game_score + pins # (2) Now (2), we are inside an instance method of the