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

comparing in Ruby

For general discussion related FlowStone

comparing in Ruby

Postby kortezzzz » Mon Oct 13, 2014 3:39 pm

Hi, I need a simple ruby code with 2 float inputs and one float output. Ruby should then compare the 2 inputs,
and if equal, it outputs the second input's value. Other than that, it outputs 0.
Can someone shed some light? Can be a very useful tool for any one of us.
User avatar
kortezzzz
 
Posts: 763
Joined: Tue Mar 19, 2013 4:21 pm

Re: comparing in Ruby

Postby KG_is_back » Mon Oct 13, 2014 4:08 pm

Code: Select all
def event i,v
if @ins[0]==@ins[1]
      then
           output 0,@ins[1]
      else
           output 0,0.0
      end
end


a simple if else statement.
KG_is_back
 
Posts: 1196
Joined: Tue Oct 22, 2013 5:43 pm
Location: Slovakia

Re: comparing in Ruby

Postby kortezzzz » Mon Oct 13, 2014 6:59 pm

Thanks KG, works great. Recommend anyone who doesn't knows programing in Ruby to adopt this. No doubt you'll need it one day. 8-)
User avatar
kortezzzz
 
Posts: 763
Joined: Tue Mar 19, 2013 4:21 pm

Re: comparing in Ruby

Postby tulamide » Mon Oct 13, 2014 8:08 pm

And for those who want to dive deeper:

In short programs like these you don't need the method definition. That are structures that help you keeping overview in larger programs. Something like this, for example, just to replace some math prims, is valid without.

Also, 'then' is only needed when you write the if-expression in one line. So, you can just write

Code: Select all
if @ins[0] == @ins[1]
   output 0, @ins[1]
else
   output 0, 0.0
end


without the

Code: Select all
def event i,v
end


The magic comes in when you realize that 'if' is not a statement, but an expression. it returns a value. Therefore the following are all valid expressions of the very same action as above:

Code: Select all
@ins[1] = 0.0 if @ins[0] != @ins[1]
output 0, @ins[1]

Here you are directly manipulating the input value before sending it out.

Code: Select all
output 0, @ins[1] if @ins[0] == @ins[1]
output 0, 0.0 if @ins[0] != @ins[1]

In this context it reads as 'output @ins[1] if true, output 0.0 if false

Code: Select all
output 0, @ins[0] == @ins[1] ? @ins[1] : 0.0

This is the so called ternary operator, a short form of 'if else': test_exp ? true_exp : false_exp
Since it either returns @ins[1] or 0.0, it can be directly written as a parameter of output

Code: Select all
output 0, if @ins[0] == @ins[1] then @ins[1] else 0.0 end

The long form. And the most preferable one. And the most beautiful one :mrgreen:

I hope it helps!
Last edited by tulamide on Mon Oct 13, 2014 8:18 pm, edited 1 time in total.
"There lies the dog buried" (German saying translated literally)
tulamide
 
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: comparing in Ruby

Postby RJHollins » Mon Oct 13, 2014 8:17 pm

Thanks tulamide,

Always appreciate the education ! :D
RJHollins
 
Posts: 1571
Joined: Thu Mar 08, 2012 7:58 pm

Re: comparing in Ruby

Postby tulamide » Mon Oct 13, 2014 8:21 pm

You're very welcome :)

:geek:
"There lies the dog buried" (German saying translated literally)
tulamide
 
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: comparing in Ruby

Postby kortezzzz » Mon Oct 13, 2014 9:46 pm

Wow, thanks tulamide. Now i realy have to take a second look into my older clumsy green based schematics. The only thing I'v learned to love in Ruby is those naughty little time/resorces/ nerves savers that save the day (Literally).
So lately, I'v become ,reluctantly, kind of Ruby "if then" modules collector (untill i'll finally learn ruby. Or not...).
Thanks.
User avatar
kortezzzz
 
Posts: 763
Joined: Tue Mar 19, 2013 4:21 pm


Return to General

Who is online

Users browsing this forum: No registered users and 85 guests