views:

158

answers:

2

I happen to be working in a sandboxed Ruby environment and have been trying to add libraries to it with mixed results. However, it has raised some interesting questions about how Ruby works. I'm looking to understand how Ruby links libraries and how it decides what to load when it starts up.

What is going on when I do a

require 'someLib/someClass'

I have required .rb files before but the behavior is different with libs. How does a require work if it looks like it is requiring a directory like in

require 'DL'

How does it know what files to load and how is that name 'DL' registered in the interpreter?

P.S.: The only exposed part of Ruby I have to work with right now is 'msvcrt-ruby18.dll'

+3  A: 

When you require 'something' Ruby searches for a file called either something.rb or something.dll/so/bundle depending on your platform.

In case it finds a library dll/so/bundle it dynamically loads it and search for a symbol called Init_something. The convention when creating a native extension is to include such a function which gets used by the ruby interpreter to hook things up.

Where (in which directories) the intrepeter looks for rb files and libs is determined by the load path, which you can append using the -I options of the interpreter. At runtime, the current load path is in $: (you can append further directories to this at runtime as well), for example:

$ irb
irb(main):001:0> puts $:
/opt/local/lib/ruby/site_ruby/1.8
/opt/local/lib/ruby/site_ruby/1.8/i686-darwin9
/opt/local/lib/ruby/site_ruby
/opt/local/lib/ruby/vendor_ruby/1.8
/opt/local/lib/ruby/vendor_ruby/1.8/i686-darwin9
/opt/local/lib/ruby/vendor_ruby
/opt/local/lib/ruby/1.8
/opt/local/lib/ruby/1.8/i686-darwin9
.

have a look at the documentation of require (http://ruby-doc.org/core-1.8.7/classes/Kernel.html#M001077)

I'm not sure what you mean by:

P.S. The only exposed part of Ruby I have to work with right now is 'msvcrt-ruby18.dll'

Also you mentioned something about sandboxing. This may interfere with your ability to require modules. Search for $SAFE, if $SAFE is set to >2 you won't be able to use require at all.

$SAFE => 0; This is exactly what I was looking for!
JustSmith
A: 

Ruby uses a built-in global, $: (dollar-colon) that is an array of strings, specifying the set of directories used by load and require.

When you require 'DL', Ruby will search $:, which (on my machine at least, and I'd expect typically everywhere) includes "c:/ruby/lib/ruby/1.8/i386-mswin32", which is where (Ruby is installed in c:/ruby on this PC) I find dl.so, the compiled library that contains the DL functionality.

Mike Woodhouse