T
Thomas Sawyer
Robert said:Actually I am totally against it, in core that is. It would be nice to
have a standard library extension for this, e.g. meta-ruby.
I was really impressed about by David's keynote at Rubyconf. Just
thought to share this thoughts in case I am not a minority.
You are against having a method altogether, you mean?
I take it because of the very nature singleton_methods / eigenmethods.
And, I'm guessing, you prescribe to the idea that the only good way to
the singleton "space" is to include modules into it, rather than
defining methods directly. There was a blog post around somewhere to
that effect. It was a good article. I wish I could dig it up now, but
I'm not finding it. Perhaps someone else has the link? In any case, if
that's what you mean, then I generally agree. That article was right,
there rarely is a good reason to define methods directly into the
singleton, one should be including a module with the methods defined in
it instead. The only exception may be class-level methods, but even then
using #extend deserves more merit.
Taking that idea to heart perhaps a reorientation on the matter is
warranted.
Consider this form in place of the usual 'class << self':
class X
extend Module.new {
...
}
end
Not quite as elegant since we can't use do...end. But if #extend could
take a block:
class X
extend do
...
end
end
Then the issue becomes transparent. Even for regular objects:
obj.extend do
...
end
This is not to say that a singleton_class/eigenclass method would never
be of use, but it would certainly mitigate the need for it a great deal.
(One might also take away from this that a better name for it, if it
were given a method name, would be #extension, but I mention that only
as an aside.)
T.