How to add few statements in one Ruby command?

For general discussion related FlowStone
User avatar
kortezzzz
Posts: 763
Joined: Tue Mar 19, 2013 4:21 pm

How to add few statements in one Ruby command?

Post by kortezzzz »

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?

Post by kortezzzz »

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 (?)
RJHollins
Posts: 1573
Joined: Thu Mar 08, 2012 7:58 pm

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

Post by RJHollins »

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.
Tronic
Posts: 539
Joined: Wed Dec 21, 2011 12:59 pm

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

Post by Tronic »

:roll:

Code: Select all

case @A[0] 
when 128
output 0,nil if @input==5
User avatar
MyCo
Posts: 718
Joined: Tue Jul 13, 2010 12:33 pm
Location: Germany
Contact:

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

Post by MyCo »

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.
tulamide
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

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

Post by tulamide »

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)
User avatar
kortezzzz
Posts: 763
Joined: Tue Mar 19, 2013 4:21 pm

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

Post by kortezzzz »

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 :)
RJHollins
Posts: 1573
Joined: Thu Mar 08, 2012 7:58 pm

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

Post by RJHollins »

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

Thanks !
8-)
Tronic
Posts: 539
Joined: Wed Dec 21, 2011 12:59 pm

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

Post by Tronic »

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? :)
Post Reply