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
Adding a string to string array's indexes with ruby; how?
19 posts
• Page 2 of 2 • 1, 2
Re: Adding a string to string array's indexes with ruby; how
RJHollins wrote:Let me ask ... do you just enclose the whole thing into a new module, and then put that into the toolbox ?
Yes 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
-
kortezzzz - Posts: 763
- Joined: Tue Mar 19, 2013 4:21 pm
Re: Adding a string to string array's indexes with ruby; how
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 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
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.
- Attachments
-
- simple ruby code for strings_patrick.fsm
- (3.31 KiB) Downloaded 872 times
-
pshannon - Posts: 144
- Joined: Fri Jan 02, 2015 3:08 am
Re: Adding a string to string array's indexes with ruby; how
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.
-
kortezzzz - Posts: 763
- Joined: Tue Mar 19, 2013 4:21 pm
Re: Adding a string to string array's indexes with ruby; how
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
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.
"There lies the dog buried" (German saying translated literally)
- tulamide
- Posts: 2714
- Joined: Sat Jun 21, 2014 2:48 pm
- Location: Germany
Re: Adding a string to string array's indexes with ruby; how
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
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.
- Attachments
-
- simple ruby code for strings 1.1_patrick.fsm
- (29.66 KiB) Downloaded 854 times
-
pshannon - Posts: 144
- Joined: Fri Jan 02, 2015 3:08 am
Re: Adding a string to string array's indexes with ruby; how
Nice update ..... Thanks !
I could not get the << to work either ... it was adding 'more strings' as you say.
I could not get the << to work either ... it was adding 'more strings' as you say.
- RJHollins
- Posts: 1571
- Joined: Thu Mar 08, 2012 7:58 pm
Re: Adding a string to string array's indexes with ruby; how
I like that. You don't get such a nickname by accident!pshannon wrote:I work in cyber security for the past 16 years and someone once called me "Coolhacker" and it stuck.
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:It looked like someone really needed help with this.
I didn't spend enough time on your code, and that's why my tip didn't work as you would wish.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?
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!
"There lies the dog buried" (German saying translated literally)
- tulamide
- Posts: 2714
- Joined: Sat Jun 21, 2014 2:48 pm
- Location: Germany
Re: Adding a string to string array's indexes with ruby; how
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.
-
pshannon - Posts: 144
- Joined: Fri Jan 02, 2015 3:08 am
Re: Adding a string to string array's indexes with ruby; how
Thanks for the explanation tulamide.
I try to follow along [to learn] as best I can.
I try to follow along [to learn] as best I can.
- RJHollins
- Posts: 1571
- Joined: Thu Mar 08, 2012 7:58 pm
19 posts
• Page 2 of 2 • 1, 2
Who is online
Users browsing this forum: No registered users and 62 guests