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
7 posts
• Page 1 of 1
comparing in Ruby
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.
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.
-
kortezzzz - Posts: 763
- Joined: Tue Mar 19, 2013 4:21 pm
Re: comparing in Ruby
- 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
Thanks KG, works great. Recommend anyone who doesn't knows programing in Ruby to adopt this. No doubt you'll need it one day.
-
kortezzzz - Posts: 763
- Joined: Tue Mar 19, 2013 4:21 pm
Re: comparing in Ruby
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
without the
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:
Here you are directly manipulating the input value before sending it out.
In this context it reads as 'output @ins[1] if true, output 0.0 if false
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
The long form. And the most preferable one. And the most beautiful one
I hope it helps!
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
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
Thanks tulamide,
Always appreciate the education !
Always appreciate the education !
- RJHollins
- Posts: 1571
- Joined: Thu Mar 08, 2012 7:58 pm
Re: comparing in Ruby
You're very welcome
"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
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.
So lately, I'v become ,reluctantly, kind of Ruby "if then" modules collector (untill i'll finally learn ruby. Or not...).
Thanks.
-
kortezzzz - Posts: 763
- Joined: Tue Mar 19, 2013 4:21 pm
7 posts
• Page 1 of 1
Who is online
Users browsing this forum: No registered users and 72 guests