Replacing processing convenience methods (original) (raw)
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
pow(a, b)
usea**b
ruby methods have always been preferredday
uset = Time.now
andt.day
avoid magic methodshour
uset = Time.now
andt.hour
avoid magic methodsminute
uset = Time.now
andt.min
avoid magic methodssecond
uset = Time.now
andt.sec
avoid magic methodsyear
uset = Time.now
andt.year
avoid magic methodssq(a)
prefera * a
avoid pointless convenience methodsdegrees(theta)
usetheta.degrees
in ruby everything is an objectradians(theta)
usetheta.radians
in ruby everything is an objecthex(string)
preferstring.hex
a regular ruby methodprintln(val)
preferputs val
or evenp val
abs(val)
useval.abs
in ruby everything is an objectround(val)
useval.round
in ruby everything is an objectceil(val)
useval.ceil
in ruby everything is an objectbinary(c)
usec.to_s(2)
in ruby everything is an objectunbinary(string)
preferstring.to_i(base=2)
in ruby everything is an objectunhex(string)
preferstring.to_i(base=16)
in ruby everything is an objecttrim(string)
usestring.strip
in ruby everything is an objectnf(float_value, 0, 2)
preferformat('%.2f', float_value)
nf(num, digit)
prefernum.to_s.rjust(digit, '0')
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.