Page 2 of 2

Re: Adding a string to string array's indexes with ruby; how

Posted: Wed Mar 11, 2020 7:54 pm
by kortezzzz
RJHollins wrote:Let me ask ... do you just enclose the whole thing into a new module, and then put that into the toolbox ?


Yes :D of course, I first name the enclosed module with my own key words, so whenever I need it, I can't do any mistakes. I'll find it. This may sound as a very primitive and non-elegant solution, but it works for me 8-)

Re: Adding a string to string array's indexes with ruby; how

Posted: Wed Mar 11, 2020 9:48 pm
by pshannon
kortezzzz wrote:
RJHollins wrote:Let me ask ... do you just enclose the whole thing into a new module, and then put that into the toolbox ?


Yes :D of course, I first name the enclosed module with my own key words, so whenever I need it, I can't do any mistakes. I'll find it. This may sound as a very primitive and non-elegant solution, but it works for me 8-)


Everyone's code earlier was great, but here is a simple module. I hope it helps.

Open the module and a help file plus code is in there with a reference. Ruby is not my first language of choice, however, it is what we have for FS and it brings designs to the next level.

Re: Adding a string to string array's indexes with ruby; how

Posted: Wed Mar 11, 2020 10:06 pm
by kortezzzz
pshannon wrote:Everyone's code earlier was great, but here is a simple module. I hope it helps.

Open the module and a help file plus code is in there with a reference. Ruby is not my first language of choice, however, it is what we have for FS and it brings designs to the next level.


Beautiful, Patrick! :) Thank you so much for this tool (and the dedicated GUI!). I know that most of the "silent crowd" on this forum which hardly write comments are Desperately waiting for such tools, since they chose FS because of it's ability to work with graphical elements. Those pieces of codes aren't very different from greens and blues, and that's why it's so important to share them here. Thank you.

Re: Adding a string to string array's indexes with ruby; how

Posted: Wed Mar 11, 2020 10:56 pm
by tulamide
pshannon wrote:Everyone's code earlier was great, but here is a simple module. I hope it helps.

Open the module and a help file plus code is in there with a reference. Ruby is not my first language of choice, however, it is what we have for FS and it brings designs to the next level.

I love your attitude! You surely don't lack self-confidence, Mr Coolhacker :mrgreen:

I hope you don't mind, but there was a reason I used

Code: Select all

"text" << "newtext"


and not

Code: Select all

"text" + "newtext"


It's a little trick to save some time. It adds up the longer the array is, so it's worth it. text + newtext tells Ruby to create a new string class with the combined text and newtext.
Whereas text << newtext tells Ruby to use the already existing class and just append newtext. That saves you the time and memory needed to create a class instance.

If you would change your module to do that as well, it will be a good tool for the toolbox.

Re: Adding a string to string array's indexes with ruby; how

Posted: Thu Mar 12, 2020 2:02 am
by pshannon
tulamide wrote:
pshannon wrote:Everyone's code earlier was great, but here is a simple module. I hope it helps.

Open the module and a help file plus code is in there with a reference. Ruby is not my first language of choice, however, it is what we have for FS and it brings designs to the next level.

I love your attitude! You surely don't lack self-confidence, Mr Coolhacker :mrgreen:

I hope you don't mind, but there was a reason I used

Code: Select all

"text" << "newtext"


and not

Code: Select all

"text" + "newtext"


It's a little trick to save some time. It adds up the longer the array is, so it's worth it. text + newtext tells Ruby to create a new string class with the combined text and newtext.
Whereas text << newtext tells Ruby to use the already existing class and just append newtext. That saves you the time and memory needed to create a class instance.

If you would change your module to do that as well, it will be a good tool for the toolbox.


Wow, I got the attention from the popular tula. I have read many of your posts and downloaded several examples from you. You know your code and FS very well. Thanks for the tip and I didn't know it made a difference in the ruby interpreter. I work in cyber security for the past 16 years and someone once called me "Coolhacker" and it stuck. :) Ruby is the newest language for me vs previous traditional ones. It looked like someone really needed help with this. I did try your suggestion for optimized code which is always welcomed. For some reason it kept adding more strings and produced unpredictable results. It did work well with the static arrays like you demonstrated, I wonder if the green string array outputs something different? I uploaded one more revision with some more added features. If someone wants to try the << vs the +, it works for me.

Re: Adding a string to string array's indexes with ruby; how

Posted: Thu Mar 12, 2020 3:27 am
by RJHollins
Nice update ..... Thanks !

I could not get the << to work either ... it was adding 'more strings' as you say.

Re: Adding a string to string array's indexes with ruby; how

Posted: Thu Mar 12, 2020 10:20 am
by tulamide
pshannon wrote:I work in cyber security for the past 16 years and someone once called me "Coolhacker" and it stuck. :)
I like that. You don't get such a nickname by accident!

pshannon wrote:It looked like someone really needed help with this.
We could use a lot more of your kind on the Ruby front. It seems lately there's only trog and I left to help. It can become a little too much at times. So, any other helping hand is welcome.

pshannon wrote:I did try your suggestion for optimized code which is always welcomed. For some reason it kept adding more strings and produced unpredictable results. It did work well with the static arrays like you demonstrated, I wonder if the green string array outputs something different?
I didn't spend enough time on your code, and that's why my tip didn't work as you would wish.

Remember, "<<" appends the right class to the existing left class, without creating a new class. Since you use the inputs directly, you change the content of those, in this case @stringsub.

::map iterates over the original stringarray, which is 14 items. Everytime the "<<"-method appends to the original @stringsub.
1st iter. @stringsub becomes "red bike"
2nd iter. @stringsub becomes "red bikecar"
3rd iter. @stringsub becomes "red bikecarplane"
etc.
But @stringsub also replaces the item in the array (that's what ::map does), and so it ends up with a mess of predictable but very wrong results. Furthermore, @stringsub isn't cleared for the next time, so the already long @stringsub again goes through the iteration, becoming an even longer nonsense.

This could even crash Flowstone at one point, when RAM isn't sufficient or the timeout is reached.

That was the long explanation.

Here's the short: I derped.

In your tool, a new empty string class is needed per iteration, which is most simply done by using +. (There are other ways as well, but why inventing the wheel a second time?)

I'm sorry for any confusion my tip may have caused!

Re: Adding a string to string array's indexes with ruby; how

Posted: Thu Mar 12, 2020 9:39 pm
by pshannon
You didn't cause any issues for me and I took it as a teaching moment from you. I get what you meant it was predictable, but an unwanted outcome and it was bad wording on my part. I could see the patterns and thanks for the explanations.

Re: Adding a string to string array's indexes with ruby; how

Posted: Fri Mar 13, 2020 5:11 am
by RJHollins
Thanks for the explanation tulamide.
I try to follow along [to learn] as best I can.

8-)