Skip to content

How to mimic "vanilla processing" inner classes

Martin Prout edited this page Apr 14, 2015 · 6 revisions

Processing inner classes

Regular processing makes much use of java "inner classes" to make PApplet methods and variables available to the inner classes (with ruby-processing you can include Processing::Proxy module to achieve a similar result.

JRubyArt

Since JRubyArt version 0.2.5.pre Processing::Proxy module provide similar access, but now implemented using :method_missing to access PConstants, also include PConstants (or use PConstants::). However you can use ruby forwardable instead, to produce an even more elegant solution (inmho). See Node class below:-

require 'forwardable'

class Node < Physics::VerletParticle2D
  extend Forwardable
  def_delegators(:@app, :fill, :stroke, :stroke_weight, :ellipse)
  def initialize(pos)
    super(pos)
    @app = $app
  end

  # All we're doing really is adding a display function to a VerletParticle
  def display
    fill(50, 200, 200, 150)
    stroke(50, 200, 200)
    stroke_weight(2)
    ellipse(x, y, 16, 16)
  end
end

To be even more kosher you could replace the global $app via an extra initialize argument.