Ruby & and'ing

For general discussion related FlowStone
Post Reply
User avatar
JB_AU
Posts: 171
Joined: Tue May 21, 2013 11:01 pm

Ruby & and'ing

Post 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 7294 times


Result "7"

I really need the result as output ? How can index 0 & 4 also be true ?
"Two things are infinite: the universe and human stupidity; and I'm not sure about the the universe."

Albert Einstein
User avatar
trogluddite
Posts: 1730
Joined: Fri Oct 22, 2010 12:46 am
Location: Yorkshire, UK

Re: Ruby & and'ing

Post 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.
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
JB_AU
Posts: 171
Joined: Tue May 21, 2013 11:01 pm

Re: Ruby & and'ing

Post 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 7288 times
"Two things are infinite: the universe and human stupidity; and I'm not sure about the the universe."

Albert Einstein
Post Reply