Cannot subclass Class?

irb(main):006:0> class MC < Class
irb(main):007:1> end
TypeError: can't make subclass of Class

Why is this? Is it a language restriction or a 1.8.2 limitation?

Are there other ways to define new meta-classes?

itsme213 wrote:

TypeError: can't make subclass of Class
Are there other ways to define new meta-classes?

You can subclass Module. Perhaps that is powerful enough?

I was trying to figure out exactly what metaclasses allow you to do,
and I came across the following quote:

Metaclasses are deeper magic than 99% of users should ever worry
about. If you wonder whether you need them, you don't (the people who
actually need them know with certainty that they need them, and don't
need an explanation about why).
-- Python Guru Tim Peters

If you've got an example of something you want to do with metaclasses
in Ruby, post it here. I'll bet you get a lot of suggestions that are
less "brain-melting," as the Python folks like to say.

Remember, you can do a lot by including Mixin modules.

···

On 10/2/05, itsme213 <itsme213@hotmail.com> wrote:

irb(main):006:0> class MC < Class
irb(main):007:1> end
TypeError: can't make subclass of Class

Why is this? Is it a language restriction or a 1.8.2 limitation?

Are there other ways to define new meta-classes?

--
Rob

itsme213 ha scritto:

irb(main):006:0> class MC < Class
irb(main):007:1> end
TypeError: can't make subclass of Class

Why is this? Is it a language restriction or a 1.8.2 limitation?

Are there other ways to define new meta-classes?

funnily enough, you can subclass Class with Class.new,
but it will raise an exception when you try to instantiate :slight_smile:

I think most of the use for metaclasses (in the pythonic way you seem to be searching) can be achieved with a mixin with proper module_function's

Hi --

irb(main):006:0> class MC < Class
irb(main):007:1> end
TypeError: can't make subclass of Class

Why is this? Is it a language restriction or a 1.8.2 limitation?

Are there other ways to define new meta-classes?

I was trying to figure out exactly what metaclasses allow you to do,
and I came across the following quote:

Metaclasses are deeper magic than 99% of users should ever worry
about. If you wonder whether you need them, you don't (the people who
actually need them know with certainty that they need them, and don't
need an explanation about why).
-- Python Guru Tim Peters

If you've got an example of something you want to do with metaclasses
in Ruby, post it here. I'll bet you get a lot of suggestions that are
less "brain-melting," as the Python folks like to say.

I don't know what the term refers to in Python, but I do know that in
Ruby there's been a lot of terminological slippage over the years.
Some people use "metaclass" to refer to all singleton classes. Others
use it to refer to the singleton classes of class objects. Other have
argued that the only real metaclass in Ruby is the class Class (if you
define a metaclass as a class that produces classes).

Singleton classes in general are definitely not deep magic. They're
just the way Ruby handles requests for per-object method definitions:
stick them in a per-object class. They're not particularly meta;
they're just anonymous classes.

Singleton classes of class objects are probably the most frequently
made use of, since that's where class methods live. I think you see
this:

   class C
     class << self

more often than class << obj where obj is not a class.

And class Class is just class Class :slight_smile:

So the question of what "metaclasses" allow you to do depends a bit on
what you mean exactly, but I don't think there's much mystery or
reason for people not to try to understand them.

David

···

On Mon, 3 Oct 2005, Rob Rypka wrote:

On 10/2/05, itsme213 <itsme213@hotmail.com> wrote:

--
David A. Black
dblack@wobblini.net

I think you are right. I'll try using modules to inject things into classes
as well as their instances and report back on how that goes.

Thanks!

"Florian Groß" <florgro@gmail.com> wrote in message
news:dhq1al$1af$1@sea.gmane.org...

···

itsme213 wrote:

> TypeError: can't make subclass of Class
> Are there other ways to define new meta-classes?

You can subclass Module. Perhaps that is powerful enough?

Hmmm. Here is what I want:

module M
  # magic for #foo, #bar
end

class C
  include M
  # should do the equivalent of:
    # def C.foo(n); @foo = n; end
    # def bar; self.class.iv_get "@foo"; end
  foo 55 #
end

C.new.bar #=> 55

I'm having trouble getting this to work. Purely my limited prowess, I am
sure ...

"Florian Groß" <florgro@gmail.com> wrote in message
news:dhq1al$1af$1@sea.gmane.org...

···

itsme213 wrote:

> TypeError: can't make subclass of Class
> Are there other ways to define new meta-classes?

You can subclass Module. Perhaps that is powerful enough?

David A. Black wrote:

I don't know what the term refers to in Python, but I do know that in
Ruby there's been a lot of terminological slippage over the years.
Some people use "metaclass" to refer to all singleton classes. Others
use it to refer to the singleton classes of class objects. Other have
argued that the only real metaclass in Ruby is the class Class (if you
define a metaclass as a class that produces classes).

Well said. I would fall into the last camp.

Some people object to the term "singleton class." Matz has suggested
many alternatives, such as "singular class" (which I rather like).
I confess I also like "eigenclass" but I won't push for it.

Whatever it is, though, it isn't a metaclass.

Singleton classes in general are definitely not deep magic. They're
just the way Ruby handles requests for per-object method definitions:
stick them in a per-object class. They're not particularly meta;
they're just anonymous classes.

It has always bothered me a tiny bit to call them classes at all;
after all, you can't instantiate them. What kind of class has no
instances? The Unicorn class perhaps?

Singleton classes of class objects are probably the most frequently
made use of, since that's where class methods live. I think you see
this:

  class C
    class << self

more often than class << obj where obj is not a class.

Probably true.

And class Class is just class Class :slight_smile:

Ha, it makes so much sense in writing, but tell it to a blind guy.

So the question of what "metaclasses" allow you to do depends a bit on
what you mean exactly, but I don't think there's much mystery or
reason for people not to try to understand them.

Definitely agreed.

Hal

Alex martelli's talk on python metaclasses, if you're curious, they're
really not too complicated:

http://www.python.org/pycon/2005/papers/36/pyc05_bla_dp.pdf

Good reminder about meta-slippage. I've been trying to wrap my brain
around all the meta's in Jim Weirich's picture:

http://onestepback.org/images/rubyobjects.png

David A. Black wrote:

···

Hi --

On Mon, 3 Oct 2005, Rob Rypka wrote:

> On 10/2/05, itsme213 <itsme213@hotmail.com> wrote:
>> irb(main):006:0> class MC < Class
>> irb(main):007:1> end
>> TypeError: can't make subclass of Class
>>
>> Why is this? Is it a language restriction or a 1.8.2 limitation?
>>
>> Are there other ways to define new meta-classes?
>
> I was trying to figure out exactly what metaclasses allow you to do,
> and I came across the following quote:
>
> Metaclasses are deeper magic than 99% of users should ever worry
> about. If you wonder whether you need them, you don't (the people who
> actually need them know with certainty that they need them, and don't
> need an explanation about why).
> -- Python Guru Tim Peters
>
> If you've got an example of something you want to do with metaclasses
> in Ruby, post it here. I'll bet you get a lot of suggestions that are
> less "brain-melting," as the Python folks like to say.

I don't know what the term refers to in Python, but I do know that in
Ruby there's been a lot of terminological slippage over the years.
Some people use "metaclass" to refer to all singleton classes. Others
use it to refer to the singleton classes of class objects. Other have
argued that the only real metaclass in Ruby is the class Class (if you
define a metaclass as a class that produces classes).

Singleton classes in general are definitely not deep magic. They're
just the way Ruby handles requests for per-object method definitions:
stick them in a per-object class. They're not particularly meta;
they're just anonymous classes.

Singleton classes of class objects are probably the most frequently
made use of, since that's where class methods live. I think you see
this:

   class C
     class << self

more often than class << obj where obj is not a class.

And class Class is just class Class :slight_smile:

So the question of what "metaclasses" allow you to do depends a bit on
what you mean exactly, but I don't think there's much mystery or
reason for people not to try to understand them.

David

--
David A. Black
dblack@wobblini.net

Maybe that this is not the most elegant method, but heres what I came up with

module M
  def self.append_features(klass)
    class << klass

      def foo= n
  @foo = n
      end

    end

    super
  end

  def foo
    self.class.instance_variable_get "@foo"
  end
end

class C
  include M
  self.foo = 55
end

p C.new.foo

regards,

Brian

···

On 03/10/05, itsme213 <itsme213@hotmail.com> wrote:

Hmmm. Here is what I want:

module M
  # magic for #foo, #bar
end

class C
  include M
  # should do the equivalent of:
    # def C.foo(n); @foo = n; end
    # def bar; self.class.iv_get "@foo"; end
  foo 55 #
end

C.new.bar #=> 55

I'm having trouble getting this to work. Purely my limited prowess, I am
sure ...

--
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/

David A. Black wrote:
>
> I don't know what the term refers to in Python, but I do know that in
> Ruby there's been a lot of terminological slippage over the years.
> Some people use "metaclass" to refer to all singleton classes. Others
> use it to refer to the singleton classes of class objects. Other have
> argued that the only real metaclass in Ruby is the class Class (if you
> define a metaclass as a class that produces classes).

Well said. I would fall into the last camp.

I assumed this was coming from a Python perspective, as almost all of
the Google results were Python related (and the OP was trying to
subclass Class, where you subclass 'type' in Python to create a
metaclass).

I hate debating about terminology - I wish everything just had a firm
definition. If we go by the definition you give for metaclass - a
class that produces a class (I like this definition) - Class is the
only metaclass in Ruby.

> So the question of what "metaclasses" allow you to do depends a bit on
> what you mean exactly, but I don't think there's much mystery or
> reason for people not to try to understand them.

Definitely agreed.

As far as I've been able to tell, they're not that complicated. What
I fail to see is a situation where it gives you anything special,
especially given Ruby's openness with regard to class definitions.

The point of my reply was simply this: If you think you need to create
another metaclass in Ruby, chances are there's another (less painful)
way to get what you want.

itsme213 - do you have an example of a task for which you want to use
metaclasses?

···

On 10/2/05, Hal Fulton <hal9000@hypermetrics.com> wrote:

--
Rob

Abstract classes never have instances.

···

On Sunday 02 October 2005 11:24 pm, Hal Fulton wrote:

What kind of class has no
instances?

--
-- Jim Weirich jim@weirichhouse.org http://onestepback.org
-----------------------------------------------------------------
"Beware of bugs in the above code; I have only proved it correct,
not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)

Hi --

David A. Black wrote:

Singleton classes in general are definitely not deep magic. They're
just the way Ruby handles requests for per-object method definitions:
stick them in a per-object class. They're not particularly meta;
they're just anonymous classes.

It has always bothered me a tiny bit to call them classes at all;
after all, you can't instantiate them. What kind of class has no
instances? The Unicorn class perhaps?

Well, the major example that comes to mind is the singleton class in
Ruby :slight_smile: But I understand the misgivings. Matz has talked about
making them some other kind of "class-like object" in the future. I'm
not rooting for that, though. The reason I like having them be
classes is the great simplicity of the whole design: you just insert
the singleton class on the method search path, and it fits in so
nicely. No need to explain (and I always find myself thinking in
terms of explaining these things) a completely different entity for
this one case.

I suppose one could also say that it's the ultimate emblem of the
class-based/prototype-based hybrid nature of Ruby: a class that
expresses the liberation of the object from its class ancestry.

David

···

On Mon, 3 Oct 2005, Hal Fulton wrote:

--
David A. Black
dblack@wobblini.net

Rob Rypka wrote:

do you have an example of a task for which you want to use
metaclasses?

What if you're trying to build a <a href="http://discuss.joelonsoftware.com/default.asp?joel.3.219431.22&quot;&gt;factory factory factory</a>?

Hmmm. Here is what I want:

module M
# magic for #foo, #bar
end

class C
include M
# should do the equivalent of:
   # def C.foo(n); @foo = n; end
   # def bar; self.class.iv_get "@foo"; end
foo 55 #
end

C.new.bar #=> 55

You gotta do a little extra work for that -- a module gets included as instance methods if you use 'include', or class methods if you use 'extend' (due to that singleton mojo). Override that:

module M
  def self.included(mod)
    mod.extend ClassMethods
  end
  def bar; self.class.instance_variable_get "@foo" end
  module ClassMethods
    def foo(n) @foo = n end
  end
end

class C
  include M
  foo 55
end

p C.new.bar
__END__

Browse through Ara's posts for some more examples of this.

Devin

Hi --

Rob Rypka wrote:

do you have an example of a task for which you want to use
metaclasses?

What if you're trying to build a <a href="http://discuss.joelonsoftware.com/default.asp?joel.3.219431.22&quot;&gt;factory factory factory</a>?

Hmmm. Here is what I want:

module M
# magic for #foo, #bar
end

class C
include M
# should do the equivalent of:
   # def C.foo(n); @foo = n; end
   # def bar; self.class.iv_get "@foo"; end
foo 55 #
end

C.new.bar #=> 55

You gotta do a little extra work for that -- a module gets included as instance methods if you use 'include', or class methods if you use 'extend' (due to that singleton mojo). Override that:

module M
def self.included(mod)
  mod.extend ClassMethods
end
def bar; self.class.instance_variable_get "@foo" end
module ClassMethods
  def foo(n) @foo = n end
end

class C
include M
foo 55
end

p C.new.bar

You can just define the method directly on the object -- you don't
need that second module:

   module M
     def self.included(mod)
       def mod.foo(n)
         @foo = n
       end
     end

etc.

David

···

On Mon, 3 Oct 2005, Devin Mullins wrote:

--
David A. Black
dblack@wobblini.net

"David A. Black" <dblack@wobblini.net> wrote in message

You can just define the method directly on the object -- you don't
need that second module:

   module M
     def self.included(mod)
       def mod.foo(n)
         @foo = n
       end
     end

Thanks Rob, David! That's nice and succinct.