Page 1 of 2

How to add few statements in one Ruby command?

Posted: Sun Dec 20, 2015 10:32 pm
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.

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

Posted: Sun Dec 20, 2015 11:04 pm
by RJHollins

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

Posted: Mon Dec 21, 2015 12:33 am
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 (?)

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

Posted: Mon Dec 21, 2015 2:15 am
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.

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

Posted: Mon Dec 21, 2015 1:36 pm
by Tronic
:roll:

Code: Select all

case @A[0] 
when 128
output 0,nil if @input==5

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

Posted: Mon Dec 21, 2015 1:55 pm
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.

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

Posted: Mon Dec 21, 2015 2:08 pm
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.

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

Posted: Mon Dec 21, 2015 11:10 pm
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 :)

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

Posted: Tue Dec 22, 2015 2:49 am
by RJHollins
Just what I hoped for too .... a mini learning session :D

Thanks !
8-)

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

Posted: Tue Dec 22, 2015 9:39 am
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? :)