self

When to use self in module's methods

这一生的挚爱 提交于 2019-12-09 09:28:38
问题 My module definition looks like this: module RG::Stats def self.sum(a, args = {}) a.inject(0){ |accum, i| accum + i } end end To use this method I simply require the file containing this definition so that I can do: RG::Stats.sum(array) and also RG::Stats.method(:sum) However, if I need to know the list of methods using RG::Stats.instance_methods I get an empty array. This is because I have used self . If I omit self then RG::Stats.instance_methods gives the list of methods, but I cannot

need self to set all constants of a swift class in init

て烟熏妆下的殇ゞ 提交于 2019-12-09 07:34:55
问题 I have a Swift class that has a constant ivar (are they called instance constants now?). To set the value to this constant, I need to call an initializer of the desired object and pass itself. However, I am not allowed to as I need to initialize all values first, then call super.init() and after that I am allowed to access self . So what to do in this case? class Broadcaster: NSObject, CBPeripheralManagerDelegate { let broadcastID: NSUUID let bluetoothManager: CBPeripheralManager init

“self” object for UIViewcontroller has @“0 objects” in debug window in xcode

≯℡__Kan透↙ 提交于 2019-12-08 15:55:21
问题 I segue to a Viewcontroller that immediately has 0 objects for "self" and although I can access "self." objects, I cannot see them in the debug window. Without pasting the code, is there a known reason for this to be happening? Thanks! to clarify: In the debug window under "self", i can usually see my variables and classes and what their values are but for this one, I see none and it says @"0 Objects" 回答1: It seems you did copy&paste some controls from other viewcontroller into the

static:: vs. self:: - are there any downsides?

三世轮回 提交于 2019-12-07 13:18:48
问题 In this StackOverflow question I learned that self:: was not inheritance-aware where static:: was (in PHP). When it comes to defining a bunch of constants within a class, if you want to override those constants in a subclass to change default "behaviours", it becomes necessary to use static:: so that a method on the parent class that references the constant, honours the "override". In the 2 years since I asked that original question, I have started using static:: extensively, to the point

Java “self” (static) reference

非 Y 不嫁゛ 提交于 2019-12-06 21:05:14
问题 I am looking for a "self" reference to the current class in JAVA in a static context manner like in PHP Scope Resolution Operator? Solution: Break out of scope? BEWARE, this is compared to a static definition really slow (by factor 300): static Logger LOG = LoggerFactory.getLogger(new RuntimeException().getStackTrace()[0].getClassName()); The old-fashioned way would be: static Logger LOG = LoggerFactory.getLogger(<Classname>.class.getName()); Are there any alternatives? I'm looking for a way

Will self retain within block?

╄→гoц情女王★ 提交于 2019-12-06 09:14:53
Before/After call the block, the retaincount is always 1. From apple block doc we know that the self should retain. Can anyone know why? NSLog(@"Before block retain count: %d", [self retainCount]); void (^block)(void) = ^(void){ UIImage* img = [UIImage imageNamed:@"hometown.png"]; [self setImage:img]; NSLog(@"After block retain count: %d", [self retainCount]); }; block(); First, retainCount is useless. Don't call it. . Blocks only retain captured objects when the block is copied. Thus, self won't be retained by the block in that example. OK I did some research, now things became more clear.

Tree view using SQL Query

浪子不回头ぞ 提交于 2019-12-06 08:28:54
问题 I have a regions table of which I want a tree view (table simple ordered as tree) is it possible using sql queries help is appreciated, I tried to do it using self joins but i did not get the desired result. tree view is something like this Indiv Div1 Zon1 div2 zon2 div3 zon3 EDIT: as per Charles Bretana suggetion I tried CTE in below query and it did not give me desired result. WITH Emp_CTE (id, ParentID, name) AS ( SELECT id, ParentID, name FROM eQPortal_Region WHERE ParentID=0 UNION ALL

Explicit passing of Self when calling super class's __init__ in python

烈酒焚心 提交于 2019-12-06 04:12:04
This question is in relation to posts at What does 'super' do in Python? , How do I initialize the base (super) class? , and Python: How do I make a subclass from a superclass? which describes two ways to initialize a SuperClass from within a SubClass as class SuperClass: def __init__(self): return def superMethod(self): return ## One version of Initiation class SubClass(SuperClass): def __init__(self): SuperClass.__init__(self) def subMethod(self): return or class SuperClass: def __init__(self): return def superMethod(self): return ## Another version of Initiation class SubClass(SuperClass):

What is the value of self in a Rails model and why aren't obvious instance methods available?

拟墨画扇 提交于 2019-12-06 03:19:20
问题 I have a custom accessor method in my rails 3.1.6 app that assigns a value to an attribute, even if the value is not present.The my_attr attribute is a serialized Hash which should be merged with the given value unless a blank value is specified, in which case it will set the current value to a blank value. (There are added checks to make sure the values are what they should be, but are removed for brevity, as they are not part of my question.) My setter is defined as: def my_attr=(new_val)

Understanding the difference between `self`and `cls` and whether they refer to the same attributes

旧巷老猫 提交于 2019-12-06 00:46:26
问题 I'm trying to understand if there are differences between self and cls but I'm struggling, even though a lot of discussion on this topic exists. For instance: class maclass(): A = "class method" def __init__(self): self.B = "instance method" def getA_s(self): print(self.A) def getA_c(cls): print(cls.A) def getB_s(self): print(self.B) def getB_c(cls): print(cls.B) C = maclass() C.getA_s() C.getA_c() C.getB_s() C.getB_c() which give me: class method class method instance method instance method