instance-variables

What's the Difference Between These Two Ruby Class Initialization Definitions?

若如初见. 提交于 2019-12-10 14:19:26
问题 I'm working through a book on Ruby, and the author used a slightly different form for writing a class initialization definition than he has in previous sections of the book. It looks like this: class Ticket attr_accessor :venue, :date def initialize(venue, date) self.venue = venue self.date = date end end In previous sections of the book, it would've been defined like this: class Ticket attr_accessor :venue, :date def initialize(venue, date) @venue = venue @date = date end end Is there any

difference between class method , instance method , instance variable , class variable?

隐身守侯 提交于 2019-12-10 12:17:59
问题 I recently started learning ruby. I am confused between class methods, instance methods, instance variables, and class variables. I googled a lot, but I didn't get any clarification on those. Any help along with examples would be appreciated. 回答1: First take a look at this diagram: You can rightly say that “ obj has a method called my_method( ),” meaning that you’re able to call obj.my_method(). By contrast, you shouldn’t say that “MyClass has a method named my_method().” That would be

Constant instance variables?

馋奶兔 提交于 2019-12-10 03:26:33
问题 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 changed. What's the most Pythonic way to handle someone attempting to modify that instance variable? Simply trust the user not to try and change something they

Marking instance variables @private

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-09 19:35:01
问题 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? 回答1: Private instance variables are used to separate interface from implementation. In Objective-C, since the class declaration must show all of the instance variables, there needs to be a way to prevent subclasses from accessing the ones that are part of the internal implementation. Otherwise, other programmers could

instance variables in @interface; header vs implementation

℡╲_俬逩灬. 提交于 2019-12-09 14:45:59
问题 Is there any difference between declaring a private instance variable in the header vs declaring it in the implementation? in TestObj.h @interface TestObj : NSObject { int test; } @end vs in TestObj.m @interface TestObj() { int test; } @end Both seem equivalent to me, is there any actual difference between declaring an instance variable in the header vs in the implementation, if not which is preferred? The @interface within the implementation file just seems like a way to declare private

Why don't I need an ivar for this Core Data property?

泪湿孤枕 提交于 2019-12-08 14:08:28
What is the sourcery going on here that makes it so I don't need to declare managedObjectContext as an ivar?? Where does __managedObjectContext exist? What is with the double-underscore prefix? Header @interface CAHistoryController : NSObject {} @property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext; @end Implementation @implementation EBHistoryController @synthesize managedObjectContext=__managedObjectContext; - (NSManagedObjectContext *)managedObjectContext { if (__managedObjectContext != nil) { return __managedObjectContext; } // ...etc } @synthesize grew the

Instance variables, preventing changes once its value has been set

一曲冷凌霜 提交于 2019-12-08 09:07:33
问题 There is a class Section (as part of MyModule mixin). Its instance variable @mark is initialized to nil , and can be changed by an instance method change_mark when its current value is nil , but not when it's been set to something else. I'm trying to build some protections to prevent the instance variable from being set in such case. Here's what I have: module MyModule class Section attr_accessor :id, :mark def initialize(id, mark) @id = id @mark = mark end def change_mark(mark) if @mark !=

Instance Variables in class not being saved

霸气de小男生 提交于 2019-12-08 08:45:14
问题 I'm filtering all my instance values of my PersonalInfo class via before_validation like this: before_validation :strip_tabs def strip_tabs self.instance_variables.map do |attr| value = self.instance_variable_get(attr) if value.present? && value.kind_of?(String) encoding_options = { :invalid => :replace, # Replace invalid byte sequences :undef => :replace, # Replace anything not defined in ASCII :replace => '', # Use a blank for those replacements :universal_newline => true # Always break

Instance variable in Stripes

有些话、适合烂在心里 提交于 2019-12-08 08:14:29
I'm trying to find a way to create an instance variable within the Stripes application context. Something that i would do in the init() method of a Servlet while using hand-coded servlets. The problem is that since an instance of the ActionBean is created each time the application is accessed, the variable in the actionBean is created multiple time. I have tried to get some reasonable place withing Stripes trying to call the ServletContext via ActionBeanContext.getServletContext() , but from there there is no way to access the init() method and write some code in it. Do you have any

Why don't I need an ivar for this Core Data property?

眉间皱痕 提交于 2019-12-08 07:45:02
问题 What is the sourcery going on here that makes it so I don't need to declare managedObjectContext as an ivar?? Where does __managedObjectContext exist? What is with the double-underscore prefix? Header @interface CAHistoryController : NSObject {} @property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext; @end Implementation @implementation EBHistoryController @synthesize managedObjectContext=__managedObjectContext; - (NSManagedObjectContext *)managedObjectContext {