The Ruby Script Context

M

Mike Stephens

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?
 
A

Andrew Timberlake

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 Timberlake
http://ramblingsonrails.com

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

Robert Klemme

2009/6/18 Andrew Timberlake said:
You can see for yourself by using ruby

puts self.inspect
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
 
R

Rick DeNatale

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! <G>

--
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
 
T

trans

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.
 
R

Robert Klemme

2009/6/18 Rick DeNatale said:
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0**************** =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0*******
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$
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 ****************

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

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

/me pulls out neuralyzer and presses button

Cheers

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
R

Robert Klemme

2009/6/18 Rick DeNatale said:
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0**************** =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0*******
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$
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 ****************

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

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

/me pulls out neuralyzer and presses button

Cheers

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
R

Rick DeNatale

2009/6/18 Rick DeNatale said:
16:13:18 JavaProducts_NGCP_dev_R1.0_PoA$ allruby -e 'p self,
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0**************** =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0*******
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$
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 ****************

I see a certain irony in this. I wonder if Charlie Nutter would as well!=
LOL, darn information leakage. =A0You have seen too much.

The Seeing Eye, sees all, knows all!
--=20
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
 
M

Mike Stephens

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?
 
R

Robert Klemme

2009/6/19 Mike Stephens said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,172
Messages
2,570,933
Members
47,472
Latest member
blackwatermelon

Latest Threads

Top