Ruby Definition of Self

Ruby and other languages (such as Smalltalk and Objective-C) prefer the term “message passing”, whereas Java and C++ prefer “method invocation”. That is, the “Java way” is to call a method on an object — running code in the context of an object — whereas the “Ruby way” is to send an object a message, … Read more

Is there a generic way for a function to reference itself?

There is no generic way for a function to refer to itself. Consider using a decorator instead. If all you want as you indicated was to print information about the function that can be done easily with a decorator: from functools import wraps def showinfo(f): @wraps(f) def wrapper(*args, **kwds): print(f.__name__, f.__hash__) return f(*args, **kwds) return … Read more

Why isn’t self always needed in ruby / rails / activerecord?

This is because attributes/associations are actually methods(getters/setters) and not local variables. When you state “parent = value” Ruby assumes you want to assign the value to the local variable parent. Somewhere up the stack there’s a setter method “def parent=” and to call that you must use “self.parent = ” to tell ruby that you … Read more

What does new self(); mean in PHP?

self points to the class in which it is written. So, if your getInstance method is in a class name MyClass, the following line : self::$_instance = new self(); Will do the same as : self::$_instance = new MyClass(); Edit : a bit more information, after the comments. If you have two classes that extend … Read more

self.delegate = self; what’s wrong in doing that?

See this thread http://www.cocoabuilder.com/archive/cocoa/241465-iphone-why-can-a-uitextfield-be-its-own-delegate.html#241505 Basically, the reason for the “freeze” when you click on your UITextField with itself as a delegate is that respondsToSelector is calling itself -> infinite recursion. UITextField is unique AFAIK. You can usually use a class as its own delegate with no particular problems. For UITextField you must create an actual … Read more

“TypeError: method() takes 1 positional argument but 2 were given” but I only passed one

In Python, this: my_object.method(“foo”) … is syntactic sugar, which the interpreter translates behind the scenes into: MyClass.method(my_object, “foo”) … which, as you can see, does indeed have two arguments – it’s just that the first one is implicit, from the point of view of the caller. This is because most methods do some work with … Read more

“TypeError: method() takes 1 positional argument but 2 were given” but I did only pass one

In Python, this: my_object.method(“foo”) …is syntactic sugar, which the interpreter translates behind the scenes into: MyClass.method(my_object, “foo”) …which, as you can see, does indeed have two arguments – it’s just that the first one is implicit, from the point of view of the caller. This is because most methods do some work with the object … Read more