Jenna Pederson
Jenna Pederson

Reputation: 5961

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?

Upvotes: 5

Views: 405

Answers (1)

Andrew Marshall
Andrew Marshall

Reputation: 96924

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"]'

Upvotes: 1

Related Questions