Every repository with this icon (
Every repository with this icon (
Using the Processing API
All of the Processing methods, as explained in the Processing Language API, are available as instance methods on your Processing::App. (frame_rate, ellipse, and the 158 others.) This makes it easy as pie to use them within your sketch.
# Triangles gone wild def setup color_mode RGB, 1.0 frame_rate 30 fill 0.8, 0.6 smooth end def draw triangle(rand(width), rand(height), rand(width), rand(height), rand(width), rand(height)) end
Many methods that you might expect to find under their Processing names will be available by more Rubyish monikers. “keyPressed” becomes “key_pressed?”, for instance. And some things are better done with regular Ruby than with Processing; instead of using load_strings("file.txt") to read in a file, consider File.readlines("file.txt").
Because of this method madness, Processing::Apps have a convenience method for searching through them. $app.find_method("ellipse") will return a list of the method names that may match what you’re looking for: “ellipse”, “ellipseMode”, and “ellipse_mode”.
Transliteration vs. Translation
Despite the fact that the entire Processing API is available to you, it’s best to work in idiomatic Ruby as much as possible.
For example, if the Processing code was:
// Each line is split into an array of String
String[] values = split(line, ",");
Though it could be translated like so:
values = split(line, ",")
You’d be better off to write it with vanilla Ruby:
values = line.split(",")
Let’s do another one. If the original Processing code was:
// code snippet from example 18-3
// of Learning Processing by Daniel Shiffman
String[] data = loadStrings("data.txt");
bubbles = new Bubble[data.length];
for (int i = 0; i < bubbles.length; i++) {
float[] values = float(split(data[i], ","));
bubbles[i] = new Bubble(values[0], values[1], values[2]);
}
A literal translation in Ruby-Processing would be only slightly less clunky:
data = loadStrings("data.txt")
bubbles = Array.new(data.length)
for i in 0 .. bubbles.length - 1 do
values = float(split(data[i], ","))
bubbles[i] = Bubble.new(values[0], values[1], values[2])
end
Ruby-fying the code above, gives you:
bubbles = load_strings("data.txt").map do |line|
values = line.split(",").map{|num| num.to_f }
Bubble.new(*values)
end
If you feel moved to push some of the samples even more towards the Ruby way, those contributions are warmly welcomed.






