Support

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

How to add few statements in one Ruby command?

For general discussion related FlowStone

How to add few statements in one Ruby command?

Postby kortezzzz » Sun Dec 20, 2015 10:32 pm

Hi,

A ruby timer solution is needed. I run a midi sequencer that sends out 6 different triggers from Ruby code, each for different fixed count values. The very basic code looks like that:

case @A[0]
when 128
output 0,nil

I would like to add another statement to that "case", based on float input. in human language its should sound like:

"only when 128 and @input = 5 output the nil". So only when 2 (or as many as I have) conditions are true, the nil should be sent. How should I formulate it to a working code?

Thanks.
Last edited by kortezzzz on Mon Dec 21, 2015 12:35 am, edited 1 time in total.
User avatar
kortezzzz
 
Posts: 763
Joined: Tue Mar 19, 2013 4:21 pm


Re: How ro add few statements in one Ruby command?

Postby kortezzzz » Mon Dec 21, 2015 12:33 am

Thanks RJHollins, but no, it isn't :(

The topic from your link contains only "1 statement per 1 action" examples. No mixed or more complex statements are shown. Moreover, since FS's Ruby editor communicates with the green graphical inputs\outputs, I'm afraid there should be a quite different formulation for such a command (?)
User avatar
kortezzzz
 
Posts: 763
Joined: Tue Mar 19, 2013 4:21 pm

Re: How to add few statements in one Ruby command?

Postby RJHollins » Mon Dec 21, 2015 2:15 am

hmmm ....

a CASE statement is like an IFTHENELSE routine.

It's possible that CASE might be a restriction for this. But if you can first structure it to work as an IFTHEN logic, then maybe a CASE can be designed.

Hopefully one of the GURU's will light the path so we can all share in the knowledge.
RJHollins
 
Posts: 1571
Joined: Thu Mar 08, 2012 7:58 pm

Re: How to add few statements in one Ruby command?

Postby Tronic » Mon Dec 21, 2015 1:36 pm

:roll:
Code: Select all
case @A[0]
when 128
output 0,nil if @input==5
Tronic
 
Posts: 539
Joined: Wed Dec 21, 2011 12:59 pm

Re: How to add few statements in one Ruby command?

Postby MyCo » Mon Dec 21, 2015 1:55 pm

Ok, not sure if I get it correct but maybe that helps:
Code: Select all
case (true)
   when (a == 1)
      output(1)
      
   when (a == 2) && (b == 1)
      output(2)
end


Thats a reverse case/switch, there are only a few script languages that support it and fortunately Ruby is one of them. This version just runs the statements for the 1. case that returns true from its condition.
User avatar
MyCo
 
Posts: 718
Joined: Tue Jul 13, 2010 12:33 pm
Location: Germany

Re: How to add few statements in one Ruby command?

Postby tulamide » Mon Dec 21, 2015 2:08 pm

Tronic wrote::roll:
Code: Select all
case @A[0]
when 128
output 0,nil if @input==5

This

Also, kortezzzz, in general:
The case statement is the equivalent of the multiplexer. There's one input that can have different states, and you select the correct output based on the state.
For complex chains of conditions use the if statement. It can be used similar to the case statement with the keyword "elsif":
Code: Select all
if @a[0] == 128 and @input == 5
#do something
elsif @a[0] == 144 and @input == 4
#do something else
elsif @[0] == 0
    if @input == 5 and @a[1] == 64
        #@a[0] is 0 while @input is 5 and @a[1] is 64
    else
        #here only @a[0] is guaranteed to be 0, plus it is given that @input and @a[1]
        #are not in the combination 5 and 64, but they could be 5 and 31, or 4 and 64
    end
else
    #none of the above conditions are true
end


Also, you shouldn't use capital letters at the beginning of a variable. Capital letters are interpreted as classes. You get away because of the preceding @, but the danger of using such notation for local variables as well is high. Anf local variables are not preceded - errors will occur.

Myco's solution is nice as well, but you need a more advanced understanding of the flow of programming languages for it.
"There lies the dog buried" (German saying translated literally)
tulamide
 
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: How to add few statements in one Ruby command?

Postby kortezzzz » Mon Dec 21, 2015 11:10 pm

Thank you, guys. You are marvelous!!! :D
Principally tronic's solution did the trick (thanks a lot tronic!), but it was worth asking this question just to see your entire statements arsenal :o MyCo, that's very interesting approach. And tulamide, as always, you go deep into to details and I love it. Also never heard about the capital letters issue... Thanks for spotting.
Learned a bunch of cool stuff today. Thanks gang :)
User avatar
kortezzzz
 
Posts: 763
Joined: Tue Mar 19, 2013 4:21 pm

Re: How to add few statements in one Ruby command?

Postby RJHollins » Tue Dec 22, 2015 2:49 am

Just what I hoped for too .... a mini learning session :D

Thanks !
8-)
RJHollins
 
Posts: 1571
Joined: Thu Mar 08, 2012 7:58 pm

Re: How to add few statements in one Ruby command?

Postby Tronic » Tue Dec 22, 2015 9:39 am

Another option is to use an array of value and use the splat operator *
in case statement,
like this

Code: Select all
theTittle = [@input0,@input1,@input2]
theTattle = [value0,value1,value2]

case input # the case, input
# NOTE THE SPLAT OPERATOR *
when *theTittle | theTattle # if any of this value in this two array match the input case
   watch "one of value in this two array match the input"
when *theTattle # if any of this value in this two array match the input case
   watch "one of value in this array match input"
end

this piece of code is usefull? :)
Tronic
 
Posts: 539
Joined: Wed Dec 21, 2011 12:59 pm

Next

Return to General

Who is online

Users browsing this forum: No registered users and 57 guests