Forum: Ruby-core [ruby-trunk - Feature #6376][Open] Feature lookup and checking if feature is loaded

Posted by Thomas Sawyer (7rans)
on 2012-04-29 00:05
(Received via mailing list)
Issue #6376 has been reported by trans (Thomas Sawyer).

----------------------------------------
Feature #6376: Feature lookup and checking if feature is loaded
https://bugs.ruby-lang.org/issues/6376

Author: trans (Thomas Sawyer)
Status: Open
Priority: Normal
Assignee:
Category: core
Target version: 2.0.0


$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.
Posted by Thomas Sawyer (7rans)
on 2012-04-29 00:07
(Received via mailing list)
Issue #6376 has been updated by trans (Thomas Sawyer).


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?
----------------------------------------
Feature #6376: Feature lookup and checking if feature is loaded
https://bugs.ruby-lang.org/issues/6376#change-26312

Author: trans (Thomas Sawyer)
Status: Open
Priority: Normal
Assignee:
Category: core
Target version: 2.0.0


$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.
Posted by Nikolai Weibull (Guest)
on 2012-04-29 10:25
(Received via mailing list)
On Sun, Apr 29, 2012 at 00:03, trans (Thomas Sawyer)
<transfire@gmail.com> 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?
Posted by mame (Yusuke Endoh) (Guest)
on 2012-05-03 06:03
(Received via mailing list)
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)

--
Yusuke Endoh <mame@tsg.ne.jp>
----------------------------------------
Feature #6376: Feature lookup and checking if feature is loaded
https://bugs.ruby-lang.org/issues/6376#change-26407

Author: trans (Thomas Sawyer)
Status: Feedback
Priority: Normal
Assignee:
Category: core
Target version: 2.0.0


$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.
Posted by Nikolai Weibull (Guest)
on 2012-05-03 06:10
(Received via mailing list)
On Thu, May 3, 2012 at 6:02 AM, mame (Yusuke Endoh) <mame@tsg.ne.jp> 
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.
Posted by Yusuke Endoh (Guest)
on 2012-05-03 06:53
(Received via mailing list)
2012/5/3 Nikolai Weibull <now@bitwi.se>:
> so if you have a file that provides A::B and B has already been
> provided by another library, then defined? wont 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?
Posted by Thomas Sawyer (7rans)
on 2012-05-03 07:05
(Received via mailing list)
Issue #6376 has been updated by trans (Thomas Sawyer).


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.

----------------------------------------
Feature #6376: Feature lookup and checking if feature is loaded
https://bugs.ruby-lang.org/issues/6376#change-26415

Author: trans (Thomas Sawyer)
Status: Feedback
Priority: Normal
Assignee:
Category: core
Target version: 2.0.0


$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.
Posted by Nikolai Weibull (Guest)
on 2012-05-03 07:29
(Received via mailing list)
On Thu, May 3, 2012 at 6:53 AM, Yusuke Endoh <mame@tsg.ne.jp> wrote:
> 2012/5/3 Nikolai Weibull <now@bitwi.se>:
>> 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.
Posted by msmill (mill mr) (Guest)
on 2012-06-11 09:15
(Received via mailing list)
Issue #6376 has been updated by msmill (mill mr).




I sincerely got a kick from your article. I really do not truly have 
much to say in response, I only wanted to comment to reply great 
work."ASTM Steel Tube":
http://sunmin.typepad.com/blog/2012/06/-stainless-...
----------------------------------------
Feature #6376: Feature lookup and checking if feature is loaded
https://bugs.ruby-lang.org/issues/6376#change-27158

Author: trans (Thomas Sawyer)
Status: Feedback
Priority: Normal
Assignee:
Category: core
Target version: 2.0.0


$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.
Posted by ko1 (Koichi Sasada) (Guest)
on 2012-10-26 23:57
(Received via mailing list)
Issue #6376 has been updated by ko1 (Koichi Sasada).

Assignee set to mame (Yusuke Endoh)

mame-san, could you judge this ticket?

----------------------------------------
Feature #6376: Feature lookup and checking if feature is loaded
https://bugs.ruby-lang.org/issues/6376#change-31687

Author: trans (Thomas Sawyer)
Status: Feedback
Priority: Normal
Assignee: mame (Yusuke Endoh)
Category: core
Target version: 2.0.0


$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.
Posted by mame (Yusuke Endoh) (Guest)
on 2012-11-19 18:45
(Received via mailing list)
Issue #6376 has been updated by mame (Yusuke Endoh).

Status changed from Feedback to Assigned
Target version changed from 2.0.0 to next minor

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 <mame@tsg.ne.jp>
----------------------------------------
Feature #6376: Feature lookup and checking if feature is loaded
https://bugs.ruby-lang.org/issues/6376#change-33107

Author: trans (Thomas Sawyer)
Status: Assigned
Priority: Normal
Assignee: mame (Yusuke Endoh)
Category: core
Target version: next minor


$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.
Posted by Thomas Sawyer (7rans)
on 2012-11-19 19:36
(Received via mailing list)
Issue #6376 has been updated by trans (Thomas Sawyer).


=begin
@mame 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

----------------------------------------
Feature #6376: Feature lookup and checking if feature is loaded
https://bugs.ruby-lang.org/issues/6376#change-33121

Author: trans (Thomas Sawyer)
Status: Assigned
Priority: Normal
Assignee: mame (Yusuke Endoh)
Category: core
Target version: next minor


$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.
Posted by mame (Yusuke Endoh) (Guest)
on 2012-11-20 13:10
(Received via mailing list)
Issue #6376 has been updated by mame (Yusuke Endoh).

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 <mame@tsg.ne.jp>
----------------------------------------
Feature #6376: Feature lookup and checking if feature is loaded
https://bugs.ruby-lang.org/issues/6376#change-33206

Author: trans (Thomas Sawyer)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: core
Target version: next minor


$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.
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.