If you have a problem or need to report a bug please email : support@dsprobotics.com
There are 3 sections to this support area:
DOWNLOADS: access to product manuals, support files and drivers
HELP & INFORMATION: tutorials and example files for learning or finding pre-made modules for your projects
USER FORUMS: meet with other users and exchange ideas, you can also get help and assistance here
NEW REGISTRATIONS - please contact us if you wish to register on the forum
Users are reminded of the forum rules they sign up to which prohibits any activity that violates any laws including posting material covered by copyright
Ruby advanced "IF THEN"
9 posts
• Page 1 of 1
Ruby advanced "IF THEN"
Hi,
Dealing here with annoying problem that refuses to respond correctly with green, so maybe there is a ruby solution for that. I need a ruby module that has one (int) input and 1 (int) output. it's purpose would be kinda "if then"
module that outputs Predetermined values. for example, if input=50, and we determined that 50=1 in the code, then it should output "1". if input=62 then output=2 and so on. But in case that there is no
"legal command" in the code for incoming value (for instance, the input becomes to 76, but 76 is not in the list), then the module remains keeping the very last "legal command".
possible on ruby?
Dealing here with annoying problem that refuses to respond correctly with green, so maybe there is a ruby solution for that. I need a ruby module that has one (int) input and 1 (int) output. it's purpose would be kinda "if then"
module that outputs Predetermined values. for example, if input=50, and we determined that 50=1 in the code, then it should output "1". if input=62 then output=2 and so on. But in case that there is no
"legal command" in the code for incoming value (for instance, the input becomes to 76, but 76 is not in the list), then the module remains keeping the very last "legal command".
possible on ruby?
-
kortezzzz - Posts: 763
- Joined: Tue Mar 19, 2013 4:21 pm
Re: Ruby advanced "IF THEN"
Ruby hashes are your friend for this. You can define any input value to look up an output value - if the output value doesn't exists, it sends back 'nil' which you can test for using a simple 'if' because 'nil' counts as 'false'.
This should do the trick...
This should do the trick...
All schematics/modules I post are free for all to use - but a credit is always polite!
Don't stagnate, mutate to create!
Don't stagnate, mutate to create!
-
trogluddite - Posts: 1730
- Joined: Fri Oct 22, 2010 12:46 am
- Location: Yorkshire, UK
Re: Ruby advanced "IF THEN"
Thanks Trog. Its a brilliant! better that an Aspirin... my headache is just gone
Thanks a lot. This one should be included in every toolbox
Thanks a lot. This one should be included in every toolbox
-
kortezzzz - Posts: 763
- Joined: Tue Mar 19, 2013 4:21 pm
Re: Ruby advanced "IF THEN"
No problem!
Hashes are one of the most useful structures in Ruby, they are well worth getting to know - it's one part of Ruby that really does add some features that would be almost impossible to do using primitives.
The 'keys' (on the left), and 'values' (on the right) can be almost anything you want - Strings, numbers, MIDI objects, you name it. The 'not found' value can also be anything you like - 'nil' is just the default that's built into the language.
Hashes are one of the most useful structures in Ruby, they are well worth getting to know - it's one part of Ruby that really does add some features that would be almost impossible to do using primitives.
The 'keys' (on the left), and 'values' (on the right) can be almost anything you want - Strings, numbers, MIDI objects, you name it. The 'not found' value can also be anything you like - 'nil' is just the default that's built into the language.
All schematics/modules I post are free for all to use - but a credit is always polite!
Don't stagnate, mutate to create!
Don't stagnate, mutate to create!
-
trogluddite - Posts: 1730
- Joined: Fri Oct 22, 2010 12:46 am
- Location: Yorkshire, UK
Re: Ruby advanced "IF THEN"
This looks really useful !
I had a situation monitoring knob values [0..1], and needed to alter [tweek] the final output values [as MIDI values] to align an external midi unit.
To do this, I used a series of CASE statements. [128 of them] Doing that for several knobs, my brain turned to 'hash' I need to explore this concept to see if it would apply to what I needed to do.
Thanks TROG !
I had a situation monitoring knob values [0..1], and needed to alter [tweek] the final output values [as MIDI values] to align an external midi unit.
To do this, I used a series of CASE statements. [128 of them] Doing that for several knobs, my brain turned to 'hash' I need to explore this concept to see if it would apply to what I needed to do.
Thanks TROG !
- RJHollins
- Posts: 1571
- Joined: Thu Mar 08, 2012 7:58 pm
Re: Ruby advanced "IF THEN"
You're welcome.
Now that's dedication!
As well as a lot less typing, the big advantage of a hash look-up is that it will be WAY faster than big sequences of conditional logic. Ruby uses hashes internally for looking up method and variable names etc., so they're really well optimised - faster even than looking up an Array index.
You can even make a Hash (or Array) contain blocks of code to execute depending on the key...
...this is a really handy technique if you have lots of drop-down options or serial commands that have to perform very different actions. The code blocks can 'see' all of your user-defined methods and @instance variables too (plain local variables are trickier, but can also be done - something to learn when you really need to).
Adding a new value to a Hash, or changing an existing one, is as simple as for an array...
...and entries are easy to remove, too...
PS) The modules that TheOm has been posting to help tester out lately are well worth a look too; there are some great examples of filtering and transforming data in those - I learned a few good tips from there.
RJHollins wrote:To do this, I used a series of CASE statements. [128 of them]
Now that's dedication!
As well as a lot less typing, the big advantage of a hash look-up is that it will be WAY faster than big sequences of conditional logic. Ruby uses hashes internally for looking up method and variable names etc., so they're really well optimised - faster even than looking up an Array index.
You can even make a Hash (or Array) contain blocks of code to execute depending on the key...
...this is a really handy technique if you have lots of drop-down options or serial commands that have to perform very different actions. The code blocks can 'see' all of your user-defined methods and @instance variables too (plain local variables are trickier, but can also be done - something to learn when you really need to).
Adding a new value to a Hash, or changing an existing one, is as simple as for an array...
- Code: Select all
@my_hash[key] = value
...and entries are easy to remove, too...
- Code: Select all
@my_hash.delete(key) # Curved brackets for this one!
PS) The modules that TheOm has been posting to help tester out lately are well worth a look too; there are some great examples of filtering and transforming data in those - I learned a few good tips from there.
All schematics/modules I post are free for all to use - but a credit is always polite!
Don't stagnate, mutate to create!
Don't stagnate, mutate to create!
-
trogluddite - Posts: 1730
- Joined: Fri Oct 22, 2010 12:46 am
- Location: Yorkshire, UK
Re: Ruby advanced "IF THEN"
Thanks for the explanations, trog. Very useful, and very cool !
So here is another idea how to implement hashes in one of the most popular modules in FS world:
Radio buttons. But unlike the known one, which is green based, the idea is to let using as many inputs as needed
for radio buttons but also let the ability for multi outputs. In that way, each radio button can send out 2 or more different values.
example: If I press on button "1", it outputs "5" from the first out put, "7" from the second, "9" from the third.
if I press button "2", the output would be "6", "9" and "2" on outputs 1-3 respectively and so on.
That would allow us to control multiple features in the schematic with a single radio buttons set
possible?
So here is another idea how to implement hashes in one of the most popular modules in FS world:
Radio buttons. But unlike the known one, which is green based, the idea is to let using as many inputs as needed
for radio buttons but also let the ability for multi outputs. In that way, each radio button can send out 2 or more different values.
example: If I press on button "1", it outputs "5" from the first out put, "7" from the second, "9" from the third.
if I press button "2", the output would be "6", "9" and "2" on outputs 1-3 respectively and so on.
That would allow us to control multiple features in the schematic with a single radio buttons set
possible?
-
kortezzzz - Posts: 763
- Joined: Tue Mar 19, 2013 4:21 pm
Re: Ruby advanced "IF THEN"
kortezzzz wrote:That would allow us to control multiple features in the schematic with a single radio buttons set
possible?
That would certainly be do-able. The 'Proc' code blocks can take ANY code - hundreds of lines even - so multiple commands for one button would be no problem.
The most "Ruby" way to do it would be to do the GUI within Ruby as well. If you use separate "button" modules, it can get tricky if you need the right one to light up after making a selection - lots of 'loop-back' links to tell them when to light up, and lots of separate things to draw, adding to the GUI CPU load.
Ideally, what you would do is to create a new Class that defines what a button is and what it does - but, sadly that's rather tricky in FS just at the moment - such a thing would make sharing and customising the code so much easier.
But if you do want separate buttons, the 'event' method has the answer. Just create as many trigger inputs to the RubyEdit as you need - when the "event" method is called, the first parameter value says which input was triggered. So as long as the trigger inputs are all at the bottom of the list, you don't need to re-write the code when you add and remove them (apart from changing your command Hash of course!)
You have to be careful with this though - although you can compare the index with an integer, it isn't really an integer itself (it's a RubyEditConnector object). But you can turn it into an Integer using the '.index' method.
- Code: Select all
def event(i, v, t)
triggered_input = i.index
end
All schematics/modules I post are free for all to use - but a credit is always polite!
Don't stagnate, mutate to create!
Don't stagnate, mutate to create!
-
trogluddite - Posts: 1730
- Joined: Fri Oct 22, 2010 12:46 am
- Location: Yorkshire, UK
Re: Ruby advanced "IF THEN"
Ok, thanks trog. didn't really understand the whole explanation, as it hard for me to be explained about something, without seeing it. But from what I understand, it's better keeping the green version of radio buttons.
I trust ruby with small and very specific tasks. when it becomes complicated, it becomes dangerous.
I trust ruby with small and very specific tasks. when it becomes complicated, it becomes dangerous.
-
kortezzzz - Posts: 763
- Joined: Tue Mar 19, 2013 4:21 pm
9 posts
• Page 1 of 1
Who is online
Users browsing this forum: No registered users and 95 guests