JRubyArt is the closest thing to a ruby DSL for vanilla processing, it supports
bare sketches, and
static sketches, and most of the processing-api see below:-
Most 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, but you should prefer snake case to camel case and there are some caveats. Here is an example of a
bare sketch (you can if you wish create a class wrapped sketch), the main difference with vanilla processing (from the processing ide) is that you need to explicitly create a settings method to set the size and sketch mode.
def settings size 200, 200 smooth 8 end def setup sketch_title 'Triangles Gone Wild' # JRubyArt & propane color_mode RGB, 1.0 frame_rate 30 fill 0.8, 0.6, 0.4 end def draw triangle(rand(width), rand(height), rand(width), rand(height), rand(width), rand(height)) end
Here are some the main differences moving from vanilla processing to JRubyArt:-
You do not declare types in ruby
vec = PVector.newinstead of
PVector vec = new PVector()for example, however in this case you should use Vec2D and Vec3D, which are alternatives to PVector (but with methods that are much more ruby-like, and have extended functionality).
There are no void methods (what’s evaluated gets returned without needing an explicit return)
- Everything is an object (this includes primitive types float, integer etc cf. java) see more
Confusing for beginners and especially pythonistas there is often more than one way to do it
Processing makes heavy use of java
innerclasses (to make methods and values somewhat globally available) JRubyArt provides the
Processing::Proxymixin to somewhat mimic this behaviour see Ball. An alternative to consider is to use delegator methods using
extend Forwardable, requires
require 'forwardable'see JRubyArt example.
key_pressed?to access mouse_pressed and key_pressed as variables (ruby can’t cope with overloading the
key_pressedmethods like java) see example.
In general you should try and code in regular ruby (in JRubyArt), only using processing short-cuts / methods when you need to (ie when ruby alternatives don’t exist, many processing short-cuts just aren’t needed in ruby). From 3. above you should use:-
Other ruby methods to prefer are:-
puts val(or even just
p val) to
map1d(val, (range1), (range2))to
map(value, start1, stop1, start2, stop2)
constrain(amt, lo, hi)it is how it is implemented
To avoid confusion use with ruby
map1d for processing
map function see example.