PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > require functionality
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
require functionality

Réponse
 
LinkBack Outils de la discussion
Vieux 20/11/2007, 11h36   #1
Junkone
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut require functionality

hello
can i verify if a library is loaded before doing a require 'library'.
if so how.
thanks in advance
  Réponse avec citation
Vieux 20/11/2007, 11h51   #2
Casimir
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: require functionality

Junkone kirjoitti:
> hello
> can i verify if a library is loaded before doing a require 'library'.
> if so how.
> thanks in advance

As far as I know the very nature of 'require' is such that if it fails
to include the required file or library, the whole program will halt.

The predefined variable $" contains the module names loaded by require.

See http://ruby.about.com/od/learnruby/s...ire_load_2.htm

Csmr
  Réponse avec citation
Vieux 20/11/2007, 11h56   #3
Xavier Noria
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: require functionality

On Nov 20, 2007, at 12:39 PM, Junkone wrote:

> hello
> can i verify if a library is loaded before doing a require 'library'.
> if so how.
> thanks in advance


require loads a library only once, it returns a boolean that indicates
whether it actually loaded the file[*].

The files loaded so far by require are stored in the array $", so you
could check that if you really need it[**].

-- fxn
[*] In Rails require is redefined and returns a different thing.

[**] Actual file names as passed or resolved by require are stored, so
strictly speaking you have _paths_ and they are not normalized,
expanded, whatever. Thus, the same "library" may have been loaded
twice if the paths to the .rb were different. See footnote on page 117
of the Pickaxe.



  Réponse avec citation
Vieux 20/11/2007, 14h59   #4
yermej
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: require functionality

On Nov 20, 5:36 am, Junkone <junko...@gmail.com> wrote:
> hello
> can i verify if a library is loaded before doing a require 'library'.
> if so how.
> thanks in advance


By "if a library is loaded", do you mean if a library exists on the
system?

If that's what you're looking for, it's probably easiest to wrap the
require in a begin/rescue/end, catch the LoadError which is raised if
the library doesn't exist, and then do what needs to be done.

begin
require "optional_library"
rescue LoadError
puts "couldn't find optional_library, continuing without it"
end
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 02h58.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,11688 seconds with 12 queries