Project

General

Profile

Actions

Feature #6376

open

Feature lookup and checking if feature is loaded

Added by trans (Thomas Sawyer) almost 12 years ago. Updated about 6 years ago.

Status:
Assigned
Target version:
-
[ruby-core:44748]

Description

$LOADED_FEATURES is useful to know what "files" have been loaded. But it doesn't really tell us what "features" have been loaded. If there where were a way to look-up a load path, without actually loading it then it would be possible to compare that to $LOADED_FEATURES and thus know. e.g.

require 'ostruct'
$LOADED_FEATURES #=> [..., "/home/trans/.local/lib/ry/rubies/1.9.3-p125/lib/ruby/1.9.1/ostruct.rb"]

path = require_path('ostruct') #=> "/home/trans/.local/lib/ry/rubies/1.9.3-p125/lib/ruby/1.9.1/ostruct.rb"
$LOADED_FEATURES.include?(path)

Of course, it would be nice to also have:

required?('ostruct')  #=> true

These methods could be class methods of special module, if it's important to keep the Kernel more tidy, e.g. Ruby.required?('ostruct').

I am currently working on a project where I need this (and have a couple of other projects that could use it too) and I've had to implement the whole thing from scratch, which isn't simple, nor fast, nor am I 100% confident that it specs exactly to Ruby's own lookup procedure. So it would be much better if Ruby would expose its lookup functionality.

Updated by trans (Thomas Sawyer) almost 12 years ago

Oh, I forget to mention that there seems to be no way to see what the "current loading feature" is either, as it appears that it is not added to $LOADED_FEATURES until after loading is completed, which kind of surprised me. Is that right?

Updated by now (Nikolai Weibull) almost 12 years ago

On Sun, Apr 29, 2012 at 00:03, trans (Thomas Sawyer)
wrote:

These methods could be class methods of special module, if it's important to keep the Kernel more tidy, e.g. Ruby.required?('ostruct').

Why not add it to $LOADED_FEATURES?

Updated by mame (Yusuke Endoh) almost 12 years ago

  • Status changed from Open to Feedback

Of course you know what is defined by the feature you loaded, don't you?
(If not, you must not load such a file; it is very dangerous)

So you can use: defined?(OpenStruct)

--
Yusuke Endoh

Updated by now (Nikolai Weibull) almost 12 years ago

On Thu, May 3, 2012 at 6:02 AM, mame (Yusuke Endoh) wrote:

Issue #6376 has been updated by mame (Yusuke Endoh).

Status changed from Open to Feedback

Of course you know what is defined by the feature you loaded, don't you?
(If not, you must not load such a file; it is very dangerous)

So you can use: defined?(OpenStruct)

Except that defined? won’t work for paths:

class A
end
class B
end
p defined?(A::B)

so if you have a file that provides A::B and B has already been
provided by another library, then defined? won’t work.

Updated by mame (Yusuke Endoh) almost 12 years ago

2012/5/3 Nikolai Weibull :

so if you have a file that provides A::B and B has already been
provided by another library, then defined? won’t work.

Then, defined?(A::B.some_class_method) or else.

Anyway, I don't think it is a good idea to depend whether a feature is
"loaded" by require or not.
What we really need to know is, whether a feature that you need is
"defined", doesn't it?

--
Yusuke Endoh

Updated by trans (Thomas Sawyer) almost 12 years ago

I think it depends. For on thing, a library's api can change over time. So that might not be the best fit, if what your asking is if library xyz is being used?. That's a more general question. Two different libraries might share some of the same module namespaces.

Updated by now (Nikolai Weibull) almost 12 years ago

On Thu, May 3, 2012 at 6:53 AM, Yusuke Endoh wrote:

2012/5/3 Nikolai Weibull :

so if you have a file that provides A::B and B has already been
provided by another library, then defined? won’t work.

Then, defined?(A::B.some_class_method) or else.

That’s far from good enough.

Anyway, I don't think it is a good idea to depend whether a feature is
"loaded" by require or not.
What we really need to know is, whether a feature that you need is
"defined", doesn't it?

Yes, certainly. Add loaded?/feature? that works like defined? except
that it doesn’t leak its lookup into parent namespaces.

Updated by ko1 (Koichi Sasada) over 11 years ago

  • Assignee set to mame (Yusuke Endoh)

mame-san, could you judge this ticket?

Updated by mame (Yusuke Endoh) over 11 years ago

  • Status changed from Feedback to Assigned
  • Target version changed from 2.0.0 to 2.6

If I had a right to judge a feature request, I would reject this.
But actually I have no right. Assigning to matz. (virtual rejection?)

I don't see what OP really want to do. Doesn't Rescue'ing a LoadError from require help?

begin
require "foo"
FooExist = true
rescue LoadError
FooExist = false
end

A dirty operation should be performed by a dirty code.

--
Yusuke Endoh

Updated by trans (Thomas Sawyer) over 11 years ago

=begin
@mame (Yusuke Endoh) Your example would load the library. The request is to know where a library comes from ((({require_path('ostruct')}))). As an additional benefit it would allow us to know if a library has been loaded or not. It's nothing to do with catching an load error.
=end

Updated by mame (Yusuke Endoh) over 11 years ago

  • Assignee changed from mame (Yusuke Endoh) to matz (Yukihiro Matsumoto)

Yes I know what you want. But I don't know why you want it. In general, I don't think that it is a good idea to depend on whether a feature is loaded or not. Rather, you should make sure to require what feature you need.

So, please elaborate your use case. I guessed one use case: you want to use one of many alternative libraries, for example, either eventmachine or Celluloid::IO. In such a case, you may want to try to require one, and if a LoadError is raised, then require the other.

--
Yusuke Endoh

Actions #12

Updated by naruse (Yui NARUSE) about 6 years ago

  • Target version deleted (2.6)
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0