##### # 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