[...]> In Ruby, methods are not first
> class objects. (Though to go with Ruby's 100% OOP mantra you would
> think they would be, but in any case...)
[...]
I used to think and wish for this too, and complain about it often.
Then, while implementing my own OOP version of Lua, I had an epiphany:
How can a method be a first-class object *and* have #super work?
You mean to unify UnboundMethod and Method. I suppose UnboundMethod
could keep a table of the classes it was in.
For #super to work, you need a method to know what class it is
associated with, so that it can search the ancestor chain. But as soon
as you associate it with a class, it's no longer a first class object.
(If you try to use the class of the receiver, it works for one level
up, but beyond that it fails.)
For more details, see my original post on this topic. [1] Please poke
holes in it. I'd love to see how Ruby could treat all methods as first-
class objects interchangeable with blocks/procs/lambdas, unifying the
madness (which, of course, would need some way to control the
different arity handling), but I'm not seeing it as possible right
now.
[1]http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/179372
Feel free to poke holes in my argument
I'm not so sure about the #initialize thing. I think maybe your
troubles came from lua and how you went about it there. Ruby creates
an object basically like this:
class Class
def new(*a, &b)
o = allocate
o.initialize(*a, &b)
o
end
end
In fact you can use that as a basis for some fun meta-coding.
class Class
alias_method :postinitialize_new, :new
def new(*args, &blk)
o = allocate
a = ancestors
until a.empty?
m = a.pop
if m.method_defined?('preinitialize') or
m.private_method_defined?('preinitialize')
im = instance_method('preinitialize')
im.arity == 0 ? im.bind(o).call : im.bind(o).call(*args,
&blk)
end
end
o.__send__(:initialize, *args, &blk) if
o.object_class.private_method_defined?(:initialize)
o
end
end
So i don't see why it can't be:
class Class
def new(*a, &b)
o = allocate
o.new(*a, &b)
o
end
end
also, from your post, I wish ruby did this always!:
"(For those not familiar with Lua, the colon used when defining a
function means "Hey, please create an implicit first parameter named
'self', because I'm too lazy to type it each time." This is why I can
pass the 'self' from the subclasses to the parent method, and have the
initializer operate on it instead.)"
T.
···
On Nov 12, 10:10 am, Phrogz <phr...@mac.com> wrote:
On Nov 12, 5:00 am, Trans <transf...@gmail.com> wrote: