Chapter 22. Extending Ruby with Other Languages
When you decide to use an interpreted language such as Ruby, you’re trading raw speed for ease of use. It’s far easier to develop a program in a higher-level language, and you get a working program faster, but you sacrifice some of the speed you might get by writing the program in a lower-level language like C and C++.
That’s the simplified view. Anyone who’s spent any serious amount of time working with higher-level languages knows that the truth is usually more complex. In many situations, the tradeoff doesn’t really matter: if the program is only going to be run once, who cares if it takes twice as long to do its job? If a program is complex enough, it might be prohibitively hard to implement in a low-level language: you might never actually get it working right without using a language like Ruby.
But even Ruby zealots must admit that there are still situations where it’s useful to be able to call code written in another language. Maybe you need a particular part of your program to run blazingly fast, or maybe you want to use a particular library that’s implemented in C or Java. When that happens you’ll be grateful for Ruby’s extension mechanism, which lets you call C code from a regular Ruby program; and the JRuby interpreter, which runs atop the Java Virtual Machine and uses Java classes as though they were Ruby classes.
Compared to other dynamic languages, it’s pretty easy to write C extensions in Ruby. The interfaces you need to ...
Get Ruby Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.