Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ruby

rubygems

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?

like image 837
Jenna Pederson Avatar asked Nov 13 '22 15:11

Jenna Pederson


1 Answers

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"]'
like image 110
Andrew Marshall Avatar answered Nov 15 '22 06:11

Andrew Marshall