Page 1 of 1

Ruby & and'ing

Posted: Fri Aug 01, 2014 11:10 am
by JB_AU
Okay i tried some ruby , ruby says it's much like "C" , so i thought if i '&' two arrays and find the matching element, which works 'kinda' , i get the correct result, i don't understand the index based output though;

and'array.png
and'array.png (10.46 KiB) Viewed 7296 times


Result "7"

I really need the result as output ? How can index 0 & 4 also be true ?

Re: Ruby & and'ing

Posted: Fri Aug 01, 2014 11:57 am
by trogluddite
Hi there,

You have the right idea with the '&'. As you can see at the bottom in the 'de-bugging' area, It is returning an Array containing the common elements - it will always return another Array, as it has no idea how many matches there might be, so this ensures that the returned type is consistent for any number of matches.

However, you're not sending the result to the output - the data there is probably the result of a previous experiment, as the ins and outs remember their values, even between FS sessions. You need to use the "output" method to send the result out to the connector.

You then have a choice - you could change the output connector to be String Array type, and just directly output the result...

Code: Select all

output 0, (@in1 & @in2)


If you just want the one match, de-reference the first array item and send that...

Code: Select all

output 0, (@in1 & @in2)[0]


You might need to take care with that to make sure that there is only one match - if the result array is empty, de-referencing will return 'nil' rather than a String. You can do this with an 'if' that checks the array size...

Code: Select all

result = (@in1 & @in2)
if result.size == 1
  output 0, result[0]
else
  # Some other action
end

To treat all of the data as Integers rater than Strings, just change the connectors to Integer Arrays and Integer types - there's only one kind of Ruby Array that can hold data of any type.

Re: Ruby & and'ing

Posted: Fri Aug 01, 2014 12:24 pm
by JB_AU
Just as i worked out the first part & was curious how to... you answered all my questions (probable the ones i would eventually ask) , cheers mate!

and'array.png
and'array.png (18.26 KiB) Viewed 7290 times