I'm reading a tutorial and it has an example like this"
require 'rubygems'
require 'RedCloth'
The tutorial says:
"...the RubyGems library over-rides the require method and enables it to
be used to
load gems as if they were normal, local libraries."
I don't know how it works.
Is there a rubygems require and an ordinary require?
The salient point is that require depends on your LOAD_PATH. There are many ways to inspect your load path, for example, you can start irb and look at the $: global variable. If you use require with a name like 'string_extensions' it searches your load path for a matching file (it adds 'rb' or the appropriate extension file extension automatically). Basically, you can think of requiring 'rubygems' as adding all your installed gems to the load path so that they will be found if you require them.
Most developers (in my experience) discourage the use of explicitly requiring ruby gems in libraries. For a detailed explanation see: 54177’s gists · GitHub
Whenever you require one of your files and it can't be found, it means that the file isn't in your current load path. For that reason, Ryan Davis above suggested you use ruby -I. (this explicitly adds your working directory to the load path) and Luke Gruber explained how you can use require with an absolute path.
···
On Nov 5, 2011, at 1:26 PM, Kaye Ng wrote:
I'm using 1.9.2 and I know *require 'rubygems'* is not necessary as
RubyGems is loaded
by default. But I still don't understand the logic behind the
"over-rides the require
method and enables it to be used to load gems as if they were normal,
local libraries"
part.
1. Does RedCloth have the .rb suffix? Is it different from any simple
library I can
make (e.g. string_extensions.rb) in that it has to use the rubygems
version of
'require'?
2. Regarding the 'string_extensions.rb' , is RubyGems even necessary?
That is, does
the automatically-loaded RubyGems have any bearing on simple libraries
that I may
create? Or is it just for downloaded gems like RedCloth?
Can someone please explain it to me in simple terms?
1.Actually, a ruby library is a ruby file, the require method will
checks the file with the .rb suffix, and the path of the file. the
path of the require file maybe in current directory, or rubygems
directory.
2.The ruby interpreter(>1.9.2) includes the rubygems lib, so you not
really need to require it.