"Sam Sungshik Kong" <ssk / chol.nospam.net> schrieb im Newsbeitrag news:U5ZBc.76284$zo2.55015 / newssvr29.news.prodigy.com... > Hi! > > Thank you for the explanation. > Now I understand the concept...maybe...:) > > I think that Class is a very unique one. > All other classes are an instance of Class (Object is an instance of Class). > What about Class? > It's an instance of itself, right? > Class is a concept as well as a thing. Yes, it's self referencing: irb(main):001:0> Class.class => Class irb(main):002:0> Class.id == Class.class.id => true irb(main):003:0> Class.ancestors => [Class, Module, Object, Kernel] > To test this strange thing, I tried to subclass Class but it failed. > > class MyClass < Class > end > =>TypeError: can't make subclass of Class > > Now, I created an instance of Class. > > aClass = Class.new > > Is aClass a class like Object or just an object like obj (obj = Object.new)? > > It seems like a class. Right. > aaClass = aClass.new #->Works! Note, that there is some magic involved depending on whether you assign to a constant or a variable: irb(main):001:0> Foo = Class.new => Foo irb(main):002:0> Foo.name => "Foo" irb(main):003:0> Foo.new.class => Foo irb(main):004:0> foo = Class.new => #<Class:0x10182f00> irb(main):005:0> foo.name => "" irb(main):006:0> foo.new.class => #<Class:0x10182f00> i.e. if the class instance is assigned a constant it's a named class, otherwise it's an anonymous class. > So Class is actually a class maker. > It's instances are classes. > That means that to make a class, I don't have to define a class. > If I just create an instance of Class, it's a class. Yes. > When I think of Class and Object, the "chicken and egg" problem comes to my > mind. > Object is an instance of Class and Class inherits from Object. > Which one is first? (Which one should exist first?) > > I am sorry that what I said is not very structured. Well, that's normal as soon as self referencingness comes into play. :-) In fact, Class and Object are special in the way that the Ruby interpreter ensures they are there and can reference one another before any user Ruby code is executed (especially class definitions). So you can just use them like other classes and don't have to worry about these specialities. Kind regards robert > > Sam > > > > "Robert Klemme" <bob.news / gmx.net> wrote in message > news:2jq7gfF14nmiuU1 / uni-berlin.de... > > > > "Sam Sungshik Kong" <ssk / chol.nospam.net> schrieb im Newsbeitrag > > news:paHBc.6843$_e1.3669 / newssvr27.news.prodigy.com... > > > Hello! > > > > > > I found a strange thing. > > > > > > Object.class > > > =>Class > > > Class.class > > > =>Class > > > > > > As you see, Object and Class are of same type. > > > > > > Object.methods.length > > > =>73 > > > Class.methods.length > > > =>74 > > > Class.methods - Object.methods > > > =>["nesting"] > > > > > > I expected that Object has same methods as Class but it's not. > > > Can somebody explain and teach me please? > > > > There is also a general answer in Ruby: even if two instances belong to > > the same class they need not have the same number of methods, because > > methods can be defined on a per instance basis. Consider: > > > > irb(main):015:0> class Foo; def test; "test"; end; end > > => nil > > irb(main):016:0> f1 = Foo.new > > => #<Foo:0x1016f920> > > irb(main):017:0> f2 = Foo.new > > => #<Foo:0x1016c788> > > irb(main):018:0> class << f2; def test2; "test2"; end; end > > => nil > > irb(main):019:0> f1.class > > => Foo > > irb(main):020:0> f2.class > > => Foo > > irb(main):021:0> f1.methods.grep(/test/) > > => ["test"] > > irb(main):022:0> f2.methods.grep(/test/) > > => ["test2", "test"] > > irb(main):023:0> f2.methods - f1.methods > > => ["test2"] > > > > Regards > > > > robert > > > >