Skip to content

Java and Ruby Libraries

alx edited this page Sep 12, 2010 · 41 revisions

Ruby-Processing can load a whole mess o’ libraries. From pure-Java libraries, to hybrid Java-C native libs, to pure-Ruby ones, you can get them to work pretty well. For libraries that use Java (including native libs), use the class method load_java_library "my_lib". This will look in the directory library/my_lib, and load in all of the .jars and native files that it can find. If you have a Ruby library that you’d like to load, use the class method load_ruby_library "ruby_lib". This will load a ruby file of the same name as the directory: library/ruby_lib/ruby_lib.rb. So, all together now, an example that loads both OpenGL and the Boids algorithmic flocking library would look like this:

require 'ruby-processing'

class Sketch < Processing::App
  load_java_library :opengl
  # We need the OpenGL classes to be included here.
  include_package "processing.opengl"
  load_ruby_library :boids

  # Code goes here . . .
end

Which can be used to make some pretty glowing flocking business. The glow is a freely configurable ability of OPENGL, using the method gl_blend_func. Read the complete code.

There’s also a way to check if a library has been successfully loaded, so you can conditionally enable bits of your app that need certain libraries. For example: trying to use OpenGL acceleration if it’s available, and falling back on P3D when it’s not. library_loaded? :library_name will let you know.

Clone this wiki locally