Re: Ruby MIDI switch/glitch oddity
Posted: Tue Mar 27, 2018 5:43 pm
I needed to take this day off, but I can't let this be uncommented. So, just a quick bit:
No higher language will force a time limit on you. That would be counter-productive. You might use an iteration to just add numbers. I might use an iteration to calculate the amount of stars in any given universe. How should I get to my goal if I had only 10ms per step?
Instead, as described in the user manual, Ruby is event-driven. Whenever an event occurs, it is put on the event queue. As long as it isn't empty, the events are worked on one by one. This means, the accuracy of Ruby is at around 10ms, but there's no upper limit for executing events (well, there is, but that is introduced by Flowstone, which will interrupt if Ruby takes too long. Too long is defined by a time factor in the range of some 10 or 20 seconds or so)
Never, I repeat, never use a for-loop in Ruby. Ruby is object-oriented, and therefore it has a module called enumerable. Every other class that implements the enumerable module, is able to iterate. For example, the array class implements the enumerable module.
You iterate through an array with one of the many methods of the enumerable module and a block.
For longer code, use the second form of a block, which is 'do' 'end'
Here's more about the enumerable module. Everything is documented there, including every existing method.
Enumerable module on ruby-doc.org
No higher language will force a time limit on you. That would be counter-productive. You might use an iteration to just add numbers. I might use an iteration to calculate the amount of stars in any given universe. How should I get to my goal if I had only 10ms per step?
Instead, as described in the user manual, Ruby is event-driven. Whenever an event occurs, it is put on the event queue. As long as it isn't empty, the events are worked on one by one. This means, the accuracy of Ruby is at around 10ms, but there's no upper limit for executing events (well, there is, but that is introduced by Flowstone, which will interrupt if Ruby takes too long. Too long is defined by a time factor in the range of some 10 or 20 seconds or so)
Never, I repeat, never use a for-loop in Ruby. Ruby is object-oriented, and therefore it has a module called enumerable. Every other class that implements the enumerable module, is able to iterate. For example, the array class implements the enumerable module.
You iterate through an array with one of the many methods of the enumerable module and a block.
- Code: Select all
myarray = ['apple', 'banana', 'peach', 'strawberry']
len = 0
myarray.each { |n| len += n.length } #'n' can be any name you like, it represents each object in the array
watch len #total count of characters in this array
For longer code, use the second form of a block, which is 'do' 'end'
- Code: Select all
#'n' = object in array, 'i' = index of the object
myarray.each.with_index do |n, i|
... #code
... #code
... #code etc.
end
Here's more about the enumerable module. Everything is documented there, including every existing method.
Enumerable module on ruby-doc.org