M
Mike Stephens
I don't have access to an instance of Ruby at the moment and I am not
clear what is being said in The Ruby Programming Language (Flanagan &
Matsumoto). It says require code is executed immediately. It also says
you can't insert the require code in the middle of something as if you
are copy-and-pasting, so can you explain some basics of what actually
happens?
If I have a file myprog.rb and libfile1.rb, and I run myprog:
myprog1.rb========
statement_1
require libfile1
==============
libfile1.rb=======
statement_2
==============
... I believe Ruby will run statement 1 and then run statement 2
Now if I change this to:
myprog2.rb========
statement_1
require libfile2
==============
libfile2.rb=================
Module Libfilemod
statement_2
end
======================
...I assume Ruby only runs statement 1 and stops
Now if I change myprog:
myprog3=========
statement_1
def calllibfile
require libfile1
end
================
...well, can you do that? If you can, presumably Ruby reads libfile1 but
doesn't execute it?
Also does require get executed in the order of text encountered in the
file or does the Ruby runtime pick out the keyword require up front and
expand it before the native code?
myprog1.rb========
statement_1
require libfile1
==============
libfile1.rb=======
statement_2
==============
Does this do 1 then 2 or 2 first?
What are the differences between wrapping require code in 'Module',
'Def' and 'Class'?
The final question is in typical web environments like webrick, apache
or eruby, does the require code get loaded and 'compiled' everytime any
program refers to it or is there some mechanism to cache common code in
'compiled' form?
clear what is being said in The Ruby Programming Language (Flanagan &
Matsumoto). It says require code is executed immediately. It also says
you can't insert the require code in the middle of something as if you
are copy-and-pasting, so can you explain some basics of what actually
happens?
If I have a file myprog.rb and libfile1.rb, and I run myprog:
myprog1.rb========
statement_1
require libfile1
==============
libfile1.rb=======
statement_2
==============
... I believe Ruby will run statement 1 and then run statement 2
Now if I change this to:
myprog2.rb========
statement_1
require libfile2
==============
libfile2.rb=================
Module Libfilemod
statement_2
end
======================
...I assume Ruby only runs statement 1 and stops
Now if I change myprog:
myprog3=========
statement_1
def calllibfile
require libfile1
end
================
...well, can you do that? If you can, presumably Ruby reads libfile1 but
doesn't execute it?
Also does require get executed in the order of text encountered in the
file or does the Ruby runtime pick out the keyword require up front and
expand it before the native code?
myprog1.rb========
statement_1
require libfile1
==============
libfile1.rb=======
statement_2
==============
Does this do 1 then 2 or 2 first?
What are the differences between wrapping require code in 'Module',
'Def' and 'Class'?
The final question is in typical web environments like webrick, apache
or eruby, does the require code get loaded and 'compiled' everytime any
program refers to it or is there some mechanism to cache common code in
'compiled' form?