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

ruby binary operators for arrays

For general discussion related FlowStone

ruby binary operators for arrays

Postby tester » Sun Jun 29, 2014 1:31 pm

What are the ruby binary operators for arrays?

Like OR:

1
0
1
0

with

1
1
0
0

gives

1
1
1
0
Need to take a break? I have something right for you.
Feel free to donate. Thank you for your contribution.
tester
 
Posts: 1786
Joined: Wed Jan 18, 2012 10:52 pm
Location: Poland, internet

Re: ruby binary operators for arrays

Postby trogluddite » Sun Jun 29, 2014 5:22 pm

Hi tester,
Good question - that works a little bit strangely in Ruby because it doesn't represent true and false the same way that 'green' does.

In 'green', zero = false, any other number = true.
In Ruby, 'nil' or 'false' = false, anything else = true (i.e. the number zero is also a 'true' value)

For what you want to do, though, using ones and zeros in an Integer Array is probably the best way - but you will have to be sure to use the 'bitwise' operators ('|' for OR, '&' for AND - single character) rather than the usual boolean ones ('||' for OR, '&&' for AND - two characters).

To do what you want, you'll need to use the 'zipping' technique, similar so your previous number and String code - something like this...
Code: Select all
output 0, @array1.zip(@array2).map{|a, b|  a | b}   # OR version
output 0, @array1.zip(@array2).map{|a, b|  a & b}  # AND version
output 0, @array1.zip(@array2).map{|a, b|  a ^ b}  # XOR version


Note that when using Integers this way, you won't be able to use the NOT operator ('!') - you'll need to use "(1 - x)" to turn ones into zeros and vice-versa.
All schematics/modules I post are free for all to use - but a credit is always polite!
Don't stagnate, mutate to create!
User avatar
trogluddite
 
Posts: 1730
Joined: Fri Oct 22, 2010 12:46 am
Location: Yorkshire, UK

Re: ruby binary operators for arrays

Postby tester » Sun Jun 29, 2014 6:24 pm

Ok, thanks.

So the not will be something like this?
Code: Select all
output 0, @a.zip(@a).map{|a, b|  1-a}   # NOT version

This one works, but I guess it should be simpler :mrgreen:
Need to take a break? I have something right for you.
Feel free to donate. Thank you for your contribution.
tester
 
Posts: 1786
Joined: Wed Jan 18, 2012 10:52 pm
Location: Poland, internet

Re: ruby binary operators for arrays

Postby trogluddite » Sun Jun 29, 2014 6:41 pm

For NOT, just...
Code: Select all
@a.map{|a| 1 - a}

As NOT takes only one argument, there's no need to zip a copy of the array in this case.
All schematics/modules I post are free for all to use - but a credit is always polite!
Don't stagnate, mutate to create!
User avatar
trogluddite
 
Posts: 1730
Joined: Fri Oct 22, 2010 12:46 am
Location: Yorkshire, UK


Return to General

Who is online

Users browsing this forum: Majestic-12 [Bot] and 64 guests