-
Notifications
You must be signed in to change notification settings - Fork 94
Replacing processing convenience methods
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).
pow, second, minute, hour, day, month, year, sq, degrees, radians, mag, println, hex, abs, binary, ceil, nf, nfc, nfp, nfs, round, trim, unbinary, unhex
-
pow(a, b)
usea**b
ruby methods have always been preferred -
day
uset = Time.now
andt.day
avoid magic methods -
hour
uset = Time.now
andt.hour
avoid magic methods -
minute
uset = Time.now
andt.min
avoid magic methods -
second
uset = Time.now
andt.sec
avoid magic methods -
year
uset = Time.now
andt.year
avoid magic methods -
sq(a)
prefera * a
avoid pointless convenience methods -
degrees(theta)
usetheta.degrees
in ruby everything is an object -
radians(theta)
usetheta.radians
in ruby everything is an object -
hex(string)
preferstring.hex
a regular ruby method -
println(val)
preferputs val
or evenp val
-
abs(val)
useval.abs
in ruby everything is an object -
round(val)
useval.round
in ruby everything is an object -
ceil(val)
useval.ceil
in ruby everything is an object -
binary(c)
usec.to_s(2)
in ruby everything is an object -
unbinary(string)
preferstring.to_i(base=2)
in ruby everything is an object -
unhex(string)
preferstring.to_i(base=16)
in ruby everything is an object -
trim(string)
usestring.strip
in ruby everything is an object -
nf(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.
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.
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.