[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

gems cleverness?

Giles Bowkett

12/12/2006 12:32:00 AM

Is there an easy way to find out all the gems you're running in a
particular codebase? (It's a Rails app but I'd imagine the technique
has general usefulness?)

--
Giles Bowkett
http://www.gilesg...
http://gilesbowkett.bl...
http://gilesgoatboy.bl...

13 Answers

Devin Mullins

12/12/2006 1:21:00 AM

0

Giles Bowkett wrote:
> Is there an easy way to find out all the gems you're running in a
> particular codebase? (It's a Rails app but I'd imagine the technique
> has general usefulness?)

Cheap hack:
$:.map{|s|s=~Regexp.new(Regexp.escape(Config::CONFIG['libdir']+'/ruby/gems/1.8/gems/')+'(.*)/lib$');$1}.compact
(or http://preview.tinyurl.... in Firefox)

I'm sure if you dig through the RubyGems code, you'll find a better way
of specifying that directory.

Devin

Giles Bowkett

12/12/2006 1:42:00 AM

0

On 12/11/06, Devin Mullins <twifkak@comcast.net> wrote:
> Giles Bowkett wrote:
> > Is there an easy way to find out all the gems you're running in a
> > particular codebase? (It's a Rails app but I'd imagine the technique
> > has general usefulness?)
>
> Cheap hack:
> $:.map{|s|s=~Regexp.new(Regexp.escape(Config::CONFIG['libdir']+'/ruby/gems/1.8/gems/')+'(.*)/lib$');$1}.compact
> (or http://preview.tinyurl.... in Firefox)
>
> I'm sure if you dig through the RubyGems code, you'll find a better way
> of specifying that directory.

I hate to admit it, because digging through the RubyGems code is
probably much more worthwhile in the long term, but cheap hacks are
totally what I need right now.

Is it the Perl-y $: thing that gives it its magic special sauce? Also,
couldn't the Regexp part work just as well without the Config stuff,
because of the minimal likelihood of a non-gem having that string in
its path?

Anyway, it works perfectly:

gilesb@a2s8 [~/railsapp]# script/console
Loading development environment.
>> $:.map{|s|s=~Regexp.new(Regexp.escape(Config::CONFIG['libdir']+'/ruby/gems/1.8/gems/')+'(.*)/lib$');$1}.compact
=> ["net-sftp-1.1.0", "net-ssh-1.0.10", "needle-1.3.0",
"ferret-0.10.13", "rails-1.1.6", "actionwebservice-1.1.6",
"actionmailer-1.2.5", "BlueCloth-1.0.0", "RedCloth-3.0.4",
"actionpack-1.12.5", "activesupport-1.3.1",
"activerecord-1.14.4/lib/../../activesupport", "activerecord-1.14.4",
"rake-0.7.1"]

Thanks!

Actually, wait a minute -- the app also uses ImageMagick, but that
didn't show up here.

--
Giles Bowkett
http://www.gilesg...
http://gilesbowkett.bl...
http://gilesgoatboy.bl...

Devin Mullins

12/12/2006 5:24:00 AM

0

Giles Bowkett wrote:
> Is it the Perl-y $: thing that gives it its magic special sauce?
Yup. $: == $LOAD_PATH == the list of directories Ruby looks in when you
do a require. FWIW, $" == $LOADED_FEATURES == the list of files (ruby
and .so) that have been require'd.

> Also,
> couldn't the Regexp part work just as well without the Config stuff,
> because of the minimal likelihood of a non-gem having that string in
> its path?
I was just trying to be a little robust. It just expands to
/usr/local/lib or c:/prog/ruby/lib or whatever. *Very* unlikely that
that path would contain a regex metacharacter that needs escaping, too,
but what the hell. Yeah, you certainly could do:
$:.map{|s|s=~%r{ruby/gems/1.8/gems/([^/]+)/lib$};$1}.compact.uniq

> Actually, wait a minute -- the app also uses ImageMagick, but that
> didn't show up here.
You might not have triggered the loading of the code that actually
depends on it. ActiveSupport overrides const_missing so you need not
require everything. (At least, for... I blogsumed that the feature is
being deprecated, or at least limited to your own rb files.)

Devin

Eric Hodel

12/13/2006 10:39:00 PM

0

On Dec 11, 2006, at 19:31, Giles Bowkett wrote:

> Is there an easy way to find out all the gems you're running in a
> particular codebase? (It's a Rails app but I'd imagine the technique
> has general usefulness?)

Gem::SourceIndex.from_installed_gems.search(//).map { |spec|
spec.full_name } # *

You get back an Array of Gem::Specification objects.

(I should fix that search in there.)

* I'm running from SVN, but this should still work.

--
Eric Hodel - drbrain@segment7.net - http://blog.se...

I LIT YOUR GEM ON FIRE!


Giles Bowkett

12/13/2006 11:32:00 PM

0

I think I should submit this question to Ruby Quiz. your approach is
very complete, but overkill -- it gives me everything on my system,
rather than everything my app is actually using. Devin's solution gave
me everything my app was using at the moment I ran the command, but
missed things which were used in the app but not yet loaded.

On 12/13/06, Eric Hodel <drbrain@segment7.net> wrote:
> On Dec 11, 2006, at 19:31, Giles Bowkett wrote:
>
> > Is there an easy way to find out all the gems you're running in a
> > particular codebase? (It's a Rails app but I'd imagine the technique
> > has general usefulness?)
>
> Gem::SourceIndex.from_installed_gems.search(//).map { |spec|
> spec.full_name } # *
>
> You get back an Array of Gem::Specification objects.
>
> (I should fix that search in there.)
>
> * I'm running from SVN, but this should still work.
>
> --
> Eric Hodel - drbrain@segment7.net - http://blog.se...
>
> I LIT YOUR GEM ON FIRE!
>
>
>


--
Giles Bowkett
http://www.gilesg...
http://gilesbowkett.bl...
http://gilesgoatboy.bl...

Eric Hodel

12/14/2006 4:21:00 PM

0

On Dec 13, 2006, at 18:32, Giles Bowkett wrote:
> On 12/13/06, Eric Hodel <drbrain@segment7.net> wrote:
>> On Dec 11, 2006, at 19:31, Giles Bowkett wrote:
>>
>> > Is there an easy way to find out all the gems you're running in a
>> > particular codebase? (It's a Rails app but I'd imagine the
>> technique
>> > has general usefulness?)
>>
>> Gem::SourceIndex.from_installed_gems.search(//).map { |spec|
>> spec.full_name } # *
>>
>> You get back an Array of Gem::Specification objects.
>>
>> (I should fix that search in there.)
>>
>> * I'm running from SVN, but this should still work.
>
> I think I should submit this question to Ruby Quiz. your approach is
> very complete, but overkill -- it gives me everything on my system,
> rather than everything my app is actually using.

Oh, sorry.

require 'rubygems'
require 'inline'

class << Gem; attr_reader :loaded_specs; end

p Gem.loaded_specs.keys

I'll make this cleaner.

> Devin's solution gave me everything my app was using at the moment
> I ran the command, but missed things which were used in the app but
> not yet loaded.

I don't see how you could possibly expect to know what files would be
loaded by your program in the future.

--
Eric Hodel - drbrain@segment7.net - http://blog.se...

A: Yes
Q: Is top-posting bad?
— Derek Milhous Zumsteg



Eric Hodel

12/14/2006 11:09:00 PM

0

On Dec 14, 2006, at 11:20, Eric Hodel wrote:
> On Dec 13, 2006, at 18:32, Giles Bowkett wrote:
>> On 12/13/06, Eric Hodel <drbrain@segment7.net> wrote:
>>> On Dec 11, 2006, at 19:31, Giles Bowkett wrote:
>>>
>>> > Is there an easy way to find out all the gems you're running in a
>>> > particular codebase? (It's a Rails app but I'd imagine the
>>> technique
>>> > has general usefulness?)
>>>
>>> Gem::SourceIndex.from_installed_gems.search(//).map { |spec|
>>> spec.full_name } # *
>>>
>>> You get back an Array of Gem::Specification objects.
>>>
>>> (I should fix that search in there.)
>>>
>>> * I'm running from SVN, but this should still work.
>>
>> I think I should submit this question to Ruby Quiz. your approach is
>> very complete, but overkill -- it gives me everything on my system,
>> rather than everything my app is actually using.
>
> Oh, sorry.
>
> require 'rubygems'
> require 'inline'
>
> class << Gem; attr_reader :loaded_specs; end
>
> p Gem.loaded_specs.keys
>
> I'll make this cleaner.

Ok, This will be going in once rubygems is up. Same interface.

--
Eric Hodel - drbrain@segment7.net - http://blog.se...

I LIT YOUR GEM ON FIRE!


Giles Bowkett

12/15/2006 5:56:00 PM

0

> > Devin's solution gave me everything my app was using at the moment
> > I ran the command, but missed things which were used in the app but
> > not yet loaded.
>
> I don't see how you could possibly expect to know what files would be
> loaded by your program in the future.

Well, that's the challenge, isn't it? It's only when you start to hack
time-travel knowledge discovery problems that you discover how truly
astounding Ruby's dynamicity is.

Seriously a human can do it just by looking at the source. There were
image magick things in there, they weren't caught by checking all gems
currently in use. Maybe because the code lives in a plugins dir and
wasn't invoked yet? I don't know.

--
Giles Bowkett
http://www.gilesg...
http://gilesbowkett.bl...
http://gilesgoatboy.bl...

Eric Hodel

12/15/2006 8:16:00 PM

0

On Dec 15, 2006, at 12:56, Giles Bowkett wrote:
>> > Devin's solution gave me everything my app was using at the moment
>> > I ran the command, but missed things which were used in the app but
>> > not yet loaded.
>>
>> I don't see how you could possibly expect to know what files would be
>> loaded by your program in the future.
>
> Well, that's the challenge, isn't it? It's only when you start to hack
> time-travel knowledge discovery problems that you discover how truly
> astounding Ruby's dynamicity is.
>
> Seriously a human can do it just by looking at the source.

Not accurately. Load path manipulation or require order may cause
files to be loaded or not loaded contrary to your expectations.
Files may have been orphaned by the author, or your code may not need
certain files, so they won't be required.

> There were image magick things in there, they weren't caught by
> checking all gems
> currently in use. Maybe because the code lives in a plugins dir and
> wasn't invoked yet? I don't know.

Which is why human inspection doesn't work.

--
Eric Hodel - drbrain@segment7.net - http://blog.se...

I LIT YOUR GEM ON FIRE!


Ara.T.Howard

12/15/2006 8:59:00 PM

0