Skip to content

Replacing processing convenience methods

Martin Prout edited this page Sep 11, 2015 · 11 revisions

Justification

Processing provides a number of convenience methods, not all of which can be readily implemented in ruby-processing. Some of these methods were previously provided in ruby-processing using a hack to make class methods available as instance methods, this is unsatisfactory, but also unecessary where good/better alternatives exist using ruby. Ruby-processing-2.5.1 did not remove these methods (but since ruby-processing-2.6.0 they were mainly deleted, along with the hack that supported them).

Removed Methods

pow, second, minute, hour, day, month, year, sq, degrees, radians, mag, println, hex, abs, binary, ceil, nf, nfc, nfp, nfs, round, trim, unbinary, unhex

Alternative Methods

  1. pow(a, b) use a**b ruby methods have always been preferred
  2. day use t = Time.now and t.day avoid magic methods
  3. hour use t = Time.now and t.hour avoid magic methods
  4. minute use t = Time.now and t.min avoid magic methods
  5. second use t = Time.now and t.sec avoid magic methods
  6. year use t = Time.now and t.year avoid magic methods
  7. sq(a) prefer a * a avoid pointless convenience methods
  8. degrees(theta) use theta.degrees in ruby everything is an object
  9. radians(theta) use theta.radians in ruby everything is an object
  10. hex(string) prefer string.hex a regular ruby method
  11. println(val) prefer puts val or even p val
  12. abs(val) use val.abs in ruby everything is an object
  13. round(val) use val.round in ruby everything is an object
  14. ceil(val) use val.ceil in ruby everything is an object
  15. binary(c) use c.to_s(2) in ruby everything is an object
  16. unbinary(string) prefer string.to_i(base=2) in ruby everything is an object
  17. unhex(string) prefer string.to_i(base=16) in ruby everything is an object
  18. trim(string) use string.strip in ruby everything is an object
  19. nf(float_value, 0, 2) prefer format('%.2f', float_value)
  20. nf(num, digit) prefer num.to_s.rjust(digit, '0')
  21. nf(num, left, right) prefer
num.to_s.rjust(left, '0').ljust(left + right, '0')

better if you'd never seen the processing version (works for floats and int), you can pad other than zeros if you wish see examples.

etc.... The other formatting methods (nfc, nfp, nfs) are all readily replaced in ruby sprintf or format is your friend.

Alternative Classes

Regular processing provides the PVector class for both 2D and 3D vector operations. Vec2D and Vec3D classes provide the same functionality, but in a much more ruby like way and with extended functionality. To use these classes in ruby-processing you need to load_library :vecmath, which also loads the ArcBall library. For usage see the vecmath library examples.

The processing map (range to range) method

This is deprecated in ruby-processing-2.6.15+ (the possible confusion with the ruby enumerable map function, should be avoided), further there is a more ruby friendly alternative map1d that you should explore (yes it is a one, not a letter 'l') see implementation here, or if you really must use the 5 args version use p5map as direct replacement for processing map. A processing variant on map is the norm method, where values are mapped to the range 0 to 1.0 (which is un-clamped like vanilla processing). For strict (ie clamped behaviour) ruby-processing has norm_strict.