Determining loaded module

Hello,

when I run a ruby script how can I determine all the modules that are
getting loaded?

Thanks

ObjectSpace.each_object(Module) {|m| p m}

Alternatively you could try to traverse across all constants, e.g.

seen = {}
queue = [Object]

until queue.empty?
  x = queue.shift
  seen = x

  p x if Module === x

  if Module === x || Class === x
    x.constants.each do |c|
      o = x.const_get c
      queue << o if !seen[o] && (Module === x || Class == x)
    end
  end
end

Kind regards

robert

···

On Wed, Mar 30, 2016 at 4:29 PM, Rajinder Yadav <devguy.ca@gmail.com> wrote:

when I run a ruby script how can I determine all the modules that are
getting loaded?

--
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.com/

Do you actually mean Module or "package like thing". I ask simply because of the ambiguity of the word "loaded".

···

On Mar 30, 2016, at 07:29, Rajinder Yadav <devguy.ca@gmail.com> wrote:

when I run a ruby script how can I determine all the modules that are
getting loaded?

I don't know if that's what you are looking for, but there's also the
$LOADED_FEATURES variable (aka $").

  ruby -e 'puts $LOADED_FEATURES'

Robert thank you, I was not expecting to see that many modules getting dumped!

···

On Wed, Mar 30, 2016 at 12:43 PM, Robert Klemme <shortcutter@googlemail.com> wrote:

On Wed, Mar 30, 2016 at 4:29 PM, Rajinder Yadav <devguy.ca@gmail.com> wrote:

when I run a ruby script how can I determine all the modules that are
getting loaded?

ObjectSpace.each_object(Module) {|m| p m}

Alternatively you could try to traverse across all constants, e.g.

seen = {}
queue = [Object]

until queue.empty?
  x = queue.shift
  seen = x

  p x if Module === x

  if Module === x || Class === x
    x.constants.each do |c|
      o = x.const_get c
      queue << o if !seen[o] && (Module === x || Class == x)
    end
  end
end

Kind regards

robert

--
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.com/

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

--
Kind Regards,
Rajinder Yadav

SafetyNet Test Driven Development
http://safetynet.devmentor.org

Hello,
so how do you get a list of all used gems / things that are required?

Thanks
Berg

What I used the word module I meant is in the sense of a module file
that gets pulled in using require.

From the initial source file it's easy to just look at all the require
statement, however the included modules may have dependencies on other
modules, so I wanted to determine a list of these modules getting
loaded for the code to function.

I assume this would also allow me to figure out things like what ruby
gems are being loaded?

···

On Thu, Mar 31, 2016 at 12:27 AM, Ryan Davis <ryand-ruby@zenspider.com> wrote:

On Mar 30, 2016, at 07:29, Rajinder Yadav <devguy.ca@gmail.com> wrote:

when I run a ruby script how can I determine all the modules that are
getting loaded?

Do you actually mean Module or "package like thing". I ask simply because of the ambiguity of the word "loaded".

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

--
Kind Regards,
Rajinder Yadav

SafetyNet Test Driven Development
http://safetynet.devmentor.org

Robert, Lucas thanks the output seems more manageable, I'll play
around with both and do more reading up.

···

On Fri, Apr 1, 2016 at 8:48 PM, Lucas Buchala <lucasbuchala@gmail.com> wrote:

I don't know if that's what you are looking for, but there's also the
$LOADED_FEATURES variable (aka $").

  ruby -e 'puts $LOADED_FEATURES'

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

--
Kind Regards,
Rajinder Yadav

SafetyNet Test Driven Development
http://safetynet.devmentor.org

What I used the word module I meant is in the sense of a module file
that gets pulled in using require.

You might be able to pull something off with set_trace_func. Try

$ ruby -e 'set_trace_func ->(*a){p a};require "thread"'

From the initial source file it's easy to just look at all the require
statement, however the included modules may have dependencies on other
modules, so I wanted to determine a list of these modules getting
loaded for the code to function.

I assume this would also allow me to figure out things like what ruby
gems are being loaded?

Yeah, probably. I guess you can also find a constant that contains
loaded gems somewhere.

Kind regards

robert

···

On Fri, Apr 1, 2016 at 9:13 PM, Rajinder Yadav <devguy.ca@gmail.com> wrote:

--
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.com/