Jim Jewett
Posts: 11
Nickname: jimj
Registered: Apr, 2005
|
|
Re: Ruby, PHP and a Conference
|
Posted: Jan 31, 2006 4:31 PM
|
|
Thank you; this was the first quick-intro I've seen to Ruby that didn't boil down to either "You can do things in a clumsy manner" or "You can write line noise". I could actually see myself using the language based on this, if python weren't available.
Speaking of which...
python also has open classes; the catch is that a class can choose to close itself, and the standard builtins do. (I will agree that adding functions to a specific instance rather than to the whole class can be confusing; it stays a function instead of becoming an instancemethod with the special access to self. But adding to a class isn't that hard.)
>>> int.f=f
Traceback (most recent call last): File "<pyshell#33>", line 1, in -toplevel- int.f=f TypeError: can't set attributes of built-in/extension type 'int' >>> class C(int): pass
>>> c=C() >>> c.f()
Traceback (most recent call last): File "<pyshell#38>", line 1, in -toplevel- c.f() AttributeError: 'C' object has no attribute 'f' >>> def f(self, arg): print arg
>>> C.f=f >>> c.f("and the method appears") and the method appears >>> c.g=f >>> c.g("but self magic doesn't happen", "so not an instancemethod") so not an instancemethod
Even before @classmethod , you could always write self.__class__ , which doesn't seem much worse than Ruby's x.class prefix for calling classmethods.
One way to emulate Ruby's module is just to use a regular class, but throw an exception in __init__ or __new__ (preferably only if the instance is not also an instance of a subclass).
|
|