Skip to content

From java processing to ruby processing

Martin Prout edited this page Aug 15, 2014 · 28 revisions

Here are some the main differences moving from vanilla processing to ruby-processing:-

  1. You do not declare types in ruby

vec = PVector.new instead of PVector vec = new PVector() for example, however you can in this case use Vec2D and Vec3D alternatives to PVector (but with methods that are much more ruby-like).

  1. There are no void methods (what's evaluated gets returned without an explicit return)

  2. Everything is an object (this includes primitive types float, integer etc)

  3. Confusing for beginners and especially pythonistas there is often more than one way to do it

In general you should try and code in regular ruby (in ruby-processing), only using processing shortcuts / methods when you need to (ie when ruby alternatives don't exist, many processing shortcuts just aren't needed in ruby). From 3. above you should prefer:-

  • a**b to pow(a, b)
  • theta.degrees to degrees(theta)
  • theta.radians to radians(theta)
  • x.abs to abs(x)
  • x.ceil to ceil(x)
  • x.round to round(x)
  • str.strip to trim(str)
  • str.hex to hex(str)
  • string.to_i(base=16) to unhex(str)

Other ruby methods to prefer:-

  • rand(x) to random(x)
  • rand(lo .. hi) to random(lo, hi)
  • puts val (or even just p val) to println(val)

Oddities to be aware of

The processing map(val, in_start, in_end, out_start, out_end) method has been implemented in ruby-processing, this is not to be confused with ruby map which is much used in ruby to map a function to an array.

Clone this wiki locally