Experiments with ruby-processing (processing-2.2.1) and JRubyArt for processing-3.0

Tuesday 7 January 2014

JRubyArt (ruby-processing development) and processing-2.0

It is highly likely that by the time jruby-9000 gets released ruby-2.0 if not ruby-2.1 will be standard. For that reason it is interesting to look at least at ruby-2.0 if not ruby-2.1 to see if the new features would be useful in ruby-processing. The new keyword argument syntax in particular looks quite interesting and I have included it in a revised version of the boids library (rboids in JRubyArt).
#####
# Original version
#####
def update(opts={}) # Just flutter, little boids ... just flutter away.         
  options = {
    shuffled: true, # Shuffling keeps things flowing smooth.
    cohesion: 100.0,
    separation: 10.0,
    alignment: 5.0,
    goal: 20.0,
    limit: 30.0
  }
  options.merge! opts
  # .... use options[var] in method body 
  # ....
end

#####
#  Updated version 
#  Featuring ruby 2.0 syntax
#####

def update(goal: 20.0, limit: 30.0, **opts) # Just flutter, little boids ... just flutter away.         

  shuffled = opts.fetch(:shuffled, true) # Shuffling keeps things flowing smooth.
  cohesion = opts.fetch(:cohesion, 100.0)
  separation = opts.fetch(:separation, 10.0)
  alignment = opts.fetch(:alignment, 5.0)
  # .... use variable directly in method body
  # ....
end

Previously opts = {} was used as the argument, and default goal and limit needed to be defined in a separate options hash, that was merged.
It occurs to me it could possibly be useful more generally where java methods are heavily overloaded eg camera (and entry order is annoyingly important under java). I do not anticipate using it for background, fill etc however. NB: remember to run in ruby-2.0 mode (eg by setting compat.version=2.0 in .jrubyrc).

No comments:

Post a Comment

Followers

Blog Archive

About Me

My photo
I have developed JRubyArt and propane new versions of ruby-processing for JRuby-9.1.5.0 and processing-3.2.2