简体   繁体   中英

How can I determine if a ruby gem is actually a native C extension?

How can I determine if a ruby gem is actually a native C extension?

When running gem install some_gem I can see when it tries to build the native extension, but is there a way to determine which of the gems are native C extensions before installing it?

You can check the gem specification to see if extensions is defined. You have to download the gem or check its source to do this, but it's not to difficult to do programmatically with a bit of unix-fu:

curl -L <gem-url> | tar xOf - metadata.gz | gunzip | ruby -r yaml -e 'p YAML.load($stdin.read).extensions.any?'

Let's compare bson & bson_ext (since they're the first non-C-extension and C-extension versions of the same gem I could think of):

% curl -L https://rubygems.org/downloads/bson-1.8.0.gem | tar xOf - metadata.gz | gunzip | ruby -r yaml -e 'p YAML.load($stdin.read).extensions.any?'
false

% curl -L https://rubygems.org/downloads/bson_ext-1.8.0.gem | tar xOf - metadata.gz | gunzip | ruby -r yaml -e 'p YAML.load($stdin.read).extensions.any?'
true

You could automate the need to know the current version of the gem by using the RubyGems API :

curl https://rubygems.org/api/v1/gems/bson.yaml | ruby -r yaml -e 'p YAML.load($stdin.read)["version"]'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM