require statement clarification

N

Neville Franks

For require "filename" if the filename doesn't include an extension is
rb assumed. I've been unable to find any documentation on this.

Also the Ruby distribution telnet.rb source file includes:
require "socket"

however I'm unable to find any socket.rb file anywhere, only a socket.c
So what does this file refer to?
 
S

Stefano Crocco

Alle marted=C3=AC 6 febbraio 2007, Neville Franks ha scritto:
For require "filename" if the filename doesn't include an extension is
.rb assumed. I've been unable to find any documentation on this.

Also the Ruby distribution telnet.rb source file includes:
require "socket"

however I'm unable to find any socket.rb file anywhere, only a socket.c
So what does this file refer to?

You can find the documentation you want with the command 'ri require'. At a=
ny=20
rate, here's a summary: if the filename has a .rb extension, require loads =
it=20
as a ruby source file. If it has the extension typical of libraries on that=
=20
system (for example, .so on unix or .dll on windows), it loads it as a ruby=
=20
extension. If the extension isn't specified, it tries adding the extensions=
=20
to the filename, until it finds an existing file.=20

Regarding socket, I have a socket.so file located=20
in /usr/lib/ruby/1.8/i686-linux (This is on gentoo linux. I think other lin=
ux=20
distributions may have it on different paths. I don't know about windows).=
=20
This means that socket is a C extension. using require 'socket' will load t=
he=20
socket.so file.

I hope this helps

Stefano
 
T

Timothy Hunter

Neville said:
For require "filename" if the filename doesn't include an extension is
.rb assumed. I've been unable to find any documentation on this.

Also the Ruby distribution telnet.rb source file includes:
require "socket"

however I'm unable to find any socket.rb file anywhere, only a socket.c
So what does this file refer to?
If no extension is specified .rb is the default. If Ruby can't find
filename.rb it will try to load filename.so. You can specify either .rb
or .so explicitly.
 

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

Forum statistics

Threads
474,232
Messages
2,571,168
Members
47,803
Latest member
ShaunaSode

Latest Threads

Top