-
Notifications
You must be signed in to change notification settings - Fork 89
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**bruby methods have always been preferred -
dayuset = Time.nowandt.dayavoid magic methods -
houruset = Time.nowandt.houravoid magic methods -
minuteuset = Time.nowandt.minavoid magic methods -
seconduset = Time.nowandt.secavoid magic methods -
yearuset = Time.nowandt.yearavoid magic methods -
sq(a)prefera * aavoid pointless convenience methods -
degrees(theta)usetheta.degreesin ruby everything is an object -
radians(theta)usetheta.radiansin ruby everything is an object -
hex(string)preferstring.hexa regular ruby method -
println(val)preferputs valor evenp val -
abs(val)useval.absin ruby everything is an object -
round(val)useval.roundin ruby everything is an object -
ceil(val)useval.ceilin 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.stripin 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.