The Ruby Script Context

When you enter a script, am I right in thinking that automatically an
overall Module object and a Class object are created, so that all your
methods not within another class or module are part of these objects?
What are the names of these two default objects?

On Thu, Jun 18, 2009 at 1:47 PM, Mike S. [email protected]
wrote:

When you enter a script, am I right in thinking that automatically an
overall Module object and a Class object are created, so that all your
methods not within another class or module are part of these objects?
What are the names of these two default objects?

You can see for yourself by using ruby

puts self.inspect
puts self.class.inspect
puts self.class.included_modules.inspect

Andrew T.
http://ramblingsonrails.com

http://MyMvelope.com - The SIMPLE way to manage your savings

2009/6/18 Andrew T. [email protected]:

puts self.class.inspect
puts self.class.included_modules.inspect

Note that there are differences between versions of Ruby:

16:13:18 JavaProducts_NGCP_dev_R1.0_PoA$ allruby -e ‘p self,
self.class, class<<self;ancestors;end’
CYGWIN_NT-5.1 padrklemme1 1.5.25(0.156/4/2) 2008-06-12 19:34 i686 Cygwin

ruby 1.8.7 (2008-08-11 patchlevel 72) [i386-cygwin]
main
Object
[Object, Kernel]

ruby 1.9.1p129 (2009-05-12 revision 23412) [i386-cygwin]
main
Object
[Object, Kernel, BasicObject]
16:13:24 JavaProducts_NGCP_dev_R1.0_PoA$

Kind regards

robert

On Thu, Jun 18, 2009 at 10:14 AM, Robert
Klemme[email protected] wrote:

16:13:18 JavaProducts_NGCP_dev_R1.0_PoA$ allruby -e 'p self,
****************


ruby 1.8.7 (2008-08-11 patchlevel 72) [i386-cygwin]
ruby 1.9.1p129 (2009-05-12 revision 23412) [i386-cygwin]
16:13:24 JavaProducts_NGCP_dev_R1.0_PoA$
****************

I see a certain irony in this. I wonder if Charlie Nutter would as well!


Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale

On Jun 18, 7:47 am, Mike S. [email protected] wrote:

When you enter a script, am I right in thinking that automatically an
overall Module object and a Class object are created, so that all your
methods not within another class or module are part of these objects?
What are the names of these two default objects?

This is only one. At the TOPLEVEL you are in an a special instance of
Object which has some special method which are delegated to the Object
class itself.

You can see this because while say, #include work at the top level,
#define_method does not. Try it and you will get:

undefined method `define_method’ for main:Object

I hope that helps.

Taking some leeway with the topic… I’ve done enough meta-programming
to tell you I so not like the way this works. It can be notoriously
difficult to create a module, heavy in the meta-code, that can be made
to apply to the entire system as it does to an individual class. I
have always maintained that the TOPLEVEL should be a self-extended
module, which would correct this issue and simplify the whole TOPLEVEL
design, but alas my suggestion has continually fallen on deaf ears.

T.

2009/6/18 Rick DeNatale [email protected]:

On Thu, Jun 18, 2009 at 10:14 AM, Robert
Klemme[email protected] wrote:

16:13:18 JavaProducts_NGCP_dev_R1.0_PoA$ allruby -e 'p self,
**************** *******
ruby 1.8.7 (2008-08-11 patchlevel 72) [i386-cygwin]
ruby 1.9.1p129 (2009-05-12 revision 23412) [i386-cygwin]
16:13:24 JavaProducts_NGCP_dev_R1.0_PoA$
****************

I see a certain irony in this. I wonder if Charlie Nutter would as well!

LOL, darn information leakage. You have seen too much.

/me pulls out neuralyzer and presses button

Cheers

robert

On Thu, Jun 18, 2009 at 10:50 AM, Robert
Klemme[email protected] wrote:

I see a certain irony in this. I wonder if Charlie Nutter would as well!

LOL, darn information leakage. You have seen too much.

The Seeing Eye, sees all, knows all!

Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale

We’ve got an object called main which presumably is a class. We may have
Kernel, Object and BasicObject as included modules but is main itself
part of an overall module that gets automatically created? Is there some
way of listing all the objects within a scripts’s state at any moment in
time?

2009/6/19 Mike S. [email protected]:

We’ve got an object called main which presumably is a class.

No. See my first posting in this thread or try it out for yourself.

We may have
Kernel, Object and BasicObject as included modules but is main itself
part of an overall module that gets automatically created? Is there some
way of listing all the objects within a scripts’s state at any moment in
time?

ObjectSpace.each_object does it.

Kind regards

robert

2009/6/18 Rick DeNatale [email protected]:

On Thu, Jun 18, 2009 at 10:14 AM, Robert
Klemme[email protected] wrote:

16:13:18 JavaProducts_NGCP_dev_R1.0_PoA$ allruby -e 'p self,
**************** *******
ruby 1.8.7 (2008-08-11 patchlevel 72) [i386-cygwin]
ruby 1.9.1p129 (2009-05-12 revision 23412) [i386-cygwin]
16:13:24 JavaProducts_NGCP_dev_R1.0_PoA$
****************

I see a certain irony in this. I wonder if Charlie Nutter would as well!

LOL, darn information leakage. You have seen too much.

/me pulls out neuralyzer and presses button

Cheers

robert