I feel that a native extension is like libraries that you should install onto your system before trying to install those gems, which depend on the native extensions. Like the ImageMagic
library. Is that correct? Is there something else that we should know about native extensions?
A Gemfile is a file that is created to describe the gem dependencies required to run a Ruby program. A Gemfile should always be placed in the root of the project directory.
gem install , in its simplest form, does something kind of like this. It grabs the gem and puts its files into a special directory on your system. You can see where gem install will install your gems if you run gem environment (look for the INSTALLATION DIRECTORY: line):
Ruby native extensions are libraries written in C using the built-in functions of the RubyVM. Basically, it's C programming with a ton of functions and macros to interact with the virtual machine. Anything that can be achieved by using pure Ruby can also be implemented using the built-in instructions.
Ruby installs the dependency rbtree and builds its extension, installs the drip gem, then builds documentation for the installed gems. You can disable documentation generation using the --no-document argument when installing gems.
A gem native extension might link to a separate library that needs to be pre-installed, and RMagick
is an example of that.
However, the term really just means "includes some code that needs to be compiled for your machine". The code is compiled and linked so that the resulting executable file can be require
d by Ruby on the machine.
The usual reasons for writing a Ruby C or C++ extension are:
Speed. For some CPU-intense tasks, C code can be 100 times faster than Ruby. In this case a native extension can be completely stand-alone with all C source code included in the gem.
Third-party library already written in C. In this case the gem will have C source code that binds the library functions into Ruby modules, classes and methods.
You can view the C source code for gems with native extensions, it is installed alongside the Ruby source code. By convention, there is a folder inside the gem called ext/gem_name
which contains a Ruby file extconf.rb
that passes information to the compiler (technically it creates a make file). In addition the C source files are put there as well.
MRI Ruby is implemented as a very "flat" structure in C, basically consisting of a large number of C functions. This makes it relatively easy to learn how to implement a native extension, even if you don't know much C. You can read Extending Ruby 1.9 as an introduction to the topic.
Native extensions can fail to install or work properly. There are many questions on Stack Overflow asking for help with specific failed installs. The usual problems are:
Missing libraries. Hopefully the gem author will explain what you need to pre-install in the README, but is not always clear.
Compiler mismatches. It is difficult to test all target systems, so sometimes the instructions in extconf.rb
won't work on a specific system, or the C code will raise warnings or not compile due to differences. In Windows, you probably won't have a working compiler unless you install the Ruby Devkit
Will not work with all versions of Ruby. For instance, JRuby can use C native extensions, if it has been enabled but it is not always advisable - the topic is quite complex, but generally avoid mixing JRuby and native extensions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With