Forum: Ruby irb: require returns false?

Ff6b1f71541cd5dcdebea65115639384?d=identicon&s=25 Mike Carbs (carbohydrate)
on 2008-12-31 08:51
hey everyone (ruby n00b here),

I am trying to compile ruby 1.8.6-p287. I built it using:

./configure --prefix=/usr/local/ruby --enable-pthread --enable-shared

I was playing with how support for things like iconv, gdbm, openssl, etc
worked and did:

irb(main):001:0> require 'gdbm'
LoadError: no such file to load -- gdbm
  from (irb):1:in `require'
  from (irb):1

so I installed the gdbm devel libs, and did recompiled. Now I get:

irb(main):004:0> require 'gdbm'
=> false

When I use Redhats stock 1.8.5 rpm, I get:

irb(main):001:0> require 'gdbm'
=> true

So my question is... what does "false" mean? The first time I tried, it
complained about not being able to load the file. After installing the
devel package, it now just says false. Does this mean that it properly
loading it but just not enabling it somehow?

Can someone shed some light on this for me?

Thanks!
C40020a47c6b625af6422b5b1302abaf?d=identicon&s=25 Stefano Crocco (crocco)
on 2008-12-31 09:42
(Received via mailing list)
Alle Wednesday 31 December 2008, Mike Carbs ha scritto:
> LoadError: no such file to load -- gdbm
> irb(main):001:0> require 'gdbm'
> => true
>
> So my question is... what does "false" mean? The first time I tried, it
> complained about not being able to load the file. After installing the
> devel package, it now just says false. Does this mean that it properly
> loading it but just not enabling it somehow?
>
> Can someone shed some light on this for me?
>
> Thanks!

require returns false if the given file had already been loaded. For
example:

require 'singleton'
=> true
require 'singleton'
=> false

The second time, the 'singleton' feature has already been loaded (by the
first
require), so there's no need to load that again and require tells you
that by
returning false. You can see which features have already been loaded by
inspecting the global variable $". For my irb session, this is the
result:

p $"
=> ["enumerator.so", "rubygems/rubygems_version.rb",
"rubygems/defaults.rb",
"thread.so", "thread.rb", "rbconfig.rb", "rubygems/exceptions.rb",
"rubygems/requirement.rb", "rubygems/version.rb",
"rubygems/dependency.rb",
"rubygems/gem_path_searcher.rb", "rubygems/user_interaction.rb",
"rubygems/platform.rb", "rubygems/specification.rb",
"rubygems/source_index.rb", "rubygems/builder.rb", "stringio.so",
"yaml/error.rb", "syck.so", "yaml/ypath.rb", "yaml/basenode.rb",
"yaml/syck.rb", "yaml/tag.rb", "yaml/stream.rb", "yaml/constants.rb",
"rational.rb", "date/format.rb", "date.rb", "yaml/rubytypes.rb",
"yaml/types.rb", "yaml.rb", "rubygems/config_file.rb",
"rubygems/custom_require.rb", "rubygems.rb", "auto_gem.rb", "e2mmap.rb",
"irb/init.rb", "irb/workspace.rb", "irb/context.rb",
"irb/extend-command.rb",
"irb/output-method.rb", "irb/notifier.rb", "irb/slex.rb",
"irb/ruby-token.rb",
"irb/ruby-lex.rb", "readline.so", "irb/input-method.rb",
"irb/locale.rb",
"irb.rb", "irb/completion.rb", "irb/ext/save-history.rb",
"singleton.rb"]

Calling require with one of these features will return false, because
they've
already been loaded.

What is important is to understand that the return value of require
never
means that an error has occurred. If an error occurs, you'll get an
exception,
as it happened to you when calling require 'gdbm' the first time.

I hope this helps

Stefano
F778607e066c8372ab94b0920ed7b2b8?d=identicon&s=25 Jakub Pavlík jn. (Guest)
on 2008-12-31 11:59
(Received via mailing list)
When require returns false it means that the library has already been
required.
(At least I don't know any other case...)

> LoadError: no such file to load -- gdbm
> irb(main):001:0> require 'gdbm'
> --
> Posted via http://www.ruby-forum.com/.

--
"Configure complete, now type 'make' and PRAY."

                (configure script of zsnes - www.zsnes.com)
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.