Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > A question about Class and Object

Reply
Thread Tools

A question about Class and Object

 
 
Sam Sungshik Kong
Guest
Posts: n/a
 
      06-21-2004
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


 
Reply With Quote
 
 
 
 
Eric Hodel
Guest
Posts: n/a
 
      06-21-2004
--v9Ux+11Zm5mwPlX6
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

Sam Sungshik Kong ((E-Mail Removed)) 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 - http://www.velocityreviews.com/forums/(E-Mail Removed) - http://segment7.net
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--


 
Reply With Quote
 
 
 
 
Kent Sibilev
Guest
Posts: n/a
 
      06-21-2004
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
>
>
>




 
Reply With Quote
 
Sam Sungshik Kong
Guest
Posts: n/a
 
      06-21-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
> >
> >
> >

>
>
>



 
Reply With Quote
 
Eric Hodel
Guest
Posts: n/a
 
      06-21-2004
--Hf61M2y+wYpnELGG
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

Sam Sungshik Kong ((E-Mail Removed)) 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 - (E-Mail Removed) - http://segment7.net
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--


 
Reply With Quote
 
Lennon Day-Reynolds
Guest
Posts: n/a
 
      06-21-2004
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


 
Reply With Quote
 
wilkes joiner
Guest
Posts: n/a
 
      06-21-2004
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
<(E-Mail Removed)> 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
>
>



 
Reply With Quote
 
Sam Sungshik Kong
Guest
Posts: n/a
 
      06-22-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
> <(E-Mail Removed)> 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
> >
> >

>
>



 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      06-22-2004

"Sam Sungshik Kong" <(E-Mail Removed)> schrieb im Newsbeitrag
newsaHBc.6843$(E-Mail Removed) 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

 
Reply With Quote
 
George Marrows
Guest
Posts: n/a
 
      06-22-2004
"Sam Sungshik Kong" <(E-Mail Removed)> 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 ] James
Britt's recent message http://www.ruby-talk.org/104273 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why should base class object points to derived class object (virtualfunction question) ? DanielJohnson C++ 7 01-17-2009 10:15 PM
Why 'class spam(object)' instead of class spam(Object)' ? Sergio Correia Python 7 09-18-2007 02:07 AM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM
Passing derived class object array in place of base class object array justanotherguy63@yahoo.com C++ 9 12-03-2004 10:57 PM



Advertisments