|
|||||
|
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? Thanks in advance. kong |
|
Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Sam Sungshik Kong () wrote: > Hello! >=20 > I found a strange thing. >=20 > Object.class > =3D>Class > Class.class > =3D>Class >=20 > As you see, Object and Class are of same type. > > Object.methods.length > =3D>73 > Class.methods.length > =3D>74 > Class.methods - Object.methods > =3D>["nesting"] >=20 > I expected that Object has same methods as Class but it's not. > Can somebody explain and teach me please? See Module#nesting in ri irb(main):001:0> Object.ancestors =3D> [Object, Kernel] irb(main):002:0> Class.ancestors =3D> [Class, Module, Object, Kernel] Class and Module have a nesting, while classes not descended from Class or Module do not. --=20 Eric Hodel - All messages signed with fingerprint: FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04 --v9Ux+11Zm5mwPlX6 Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.2 (FreeBSD) iD8DBQFA10ZIMypVHHlsnwQRAhn2AJ9FR+xXFN+XCDX13dS5tY 9bGHZOkQCfRKyB NcG8G28T+nzFShcRge7Zlcc= =M6Dd -----END PGP SIGNATURE----- --v9Ux+11Zm5mwPlX6-- |
|
$ irb irb(main):001:0> Object.superclass => nil irb(main):002:0> Class.superclass => Module irb(main):003:0> Cheers, Kent. On Jun 21, 2004, at 4:08 PM, Sam Sungshik Kong wrote: > 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? > > Thanks in advance. > > kong > > > |
|
Thanks for your reply.
But I still don't understand it very well. Object is type of Class, right? (Object.class -> Class) Class has a method - nesting. Then Object should have it because it's type of Class. Maybe something in my logic is wrong. Could you point me to it? kong "Kent Sibilev" <> wrote in message news: > You should care about superclass, not class: > > $ irb > irb(main):001:0> Object.superclass > => nil > irb(main):002:0> Class.superclass > => Module > irb(main):003:0> > > Cheers, > Kent. > > On Jun 21, 2004, at 4:08 PM, Sam Sungshik Kong wrote: > > > 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? > > > > Thanks in advance. > > > > kong > > > > > > > > > |
|
--Hf61M2y+wYpnELGG
Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Sam Sungshik Kong () wrote: > Thanks for your reply. > But I still don't understand it very well. >=20 > Object is type of Class, right? > (Object.class -> Class) > Class has a method - nesting. > Then Object should have it because it's type of Class. >=20 > Maybe something in my logic is wrong. > Could you point me to it? Class inherits from Module, Object does not. A Module has a nesting, while an object does not (unless it is a Class or Module). See Module#nesting in ri. --=20 Eric Hodel - All messages signed with fingerprint: FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04 --Hf61M2y+wYpnELGG Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.2 (FreeBSD) iD8DBQFA11yvMypVHHlsnwQRAh1aAJ4hGAsaEjRCJXmf6GsEYg aXpdxvHgCgsMVb ytfL28KIc/WXfeauG+iQfdU= =zKdO -----END PGP SIGNATURE----- --Hf61M2y+wYpnELGG-- |
|
Sam,
'Object', 'Module', and 'Class' are all components of Ruby's meta-object protocol, so the relationships between them are a special case. 'Object.class' returns 'Class', but internally, the Object type is created in the C code that initializes the object model, before that model is complete. In other words, a few core classes must be bootstrapped in the runtime to avoid circular inheritance relationships. That can make them appear to violate the usual semantics, but there is really no way aside from layering a class-based object model on top of a simpler one (such as simple prototypes) to avoid having certain primitives with special status and semantics. Lennon |
|
What version of Ruby are you running? From irb,
Object.class.methods.length return 74. Instances of Object and Class should not necessarily have the same number of methods, but Object.class and Class.class should since they both return an instance of Class. The exception being somewhere someone added or a removed a method from the instance of Class returned by Object.class. On Tue, 22 Jun 2004 05:08:21 +0900, Sam Sungshik Kong <> wrote: > > 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? > > Thanks in advance. > > kong > > |
|
Hi!
I'm using Ruby 1.8. Object.class.methods.length #->74 Object.methods.length #->73 Object.class is Class. Object is type of Class. I'm still confused with class and object and metaclass... Sam "wilkes joiner" <> wrote in message news: > What version of Ruby are you running? From irb, > Object.class.methods.length return 74. > > Instances of Object and Class should not necessarily have the same > number of methods, but Object.class and Class.class should since they > both return an instance of Class. The exception being somewhere > someone added or a removed a method from the instance of Class > returned by Object.class. > > On Tue, 22 Jun 2004 05:08:21 +0900, Sam Sungshik Kong > <> wrote: > > > > 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? > > > > Thanks in advance. > > > > kong > > > > > > |
|
"Sam Sungshik Kong" <> schrieb im Newsbeitrag news aHBc.6843$ m...> 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 |
|
"Sam Sungshik Kong" <> wrote
> I'm using Ruby 1.8. > > Object.class.methods.length #->74 > Object.methods.length #->73 > > Object.class is Class. > Object is type of Class. > > I'm still confused with class and object and metaclass... Hi Sam -- The key is that what class an object is doesn't say everything about what methods it has, because of the existence of singleton methods. [And the list of methods an object has doesn't say everything about what messages it responds to, because of the existence of method_missing - though this is irrelevant to your question ] JamesBritt's recent message explains this, though without mentioning singleton methods explicitly. In this case, the additional method ("nesting") is sneaking in as singleton method on Module: irb(main):015:0> Module.singleton_methods => ["constants", "nesting"] constants is also a Module singleton method, but nesting is the only new one: irb(main):016:0> Object.methods.include?("nesting") => false irb(main):017:0> Object.methods.include?("constants") => true ... hence the difference of 1 in the number of methods that Object and Module have. Hope this helps, George. |