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

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

For general discussion related FlowStone

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

Postby kortezzzz » Tue Mar 10, 2020 1:07 pm

Hi,
It has been discussed before but with integers \ floats situation. This time, I'm looking for a simple ruby code with 1 string array and one string input that can add a given string to a given string array's indexes. Seems like that it'll need 2 versions, one for adding a string before every index and one for adding after every index. For instance, let's say we have an array like:

dog
cat
fish
mouse

And we'd like to add the word "red" before every index, like "red dog", "red cat" red fish", red mouse"
And then vice versa like "dog red", "cat red", fish red", mouse red". How do we do that with Ruby? Wish it could be just done easily with a green primitive...

Thanks!
User avatar
kortezzzz
 
Posts: 763
Joined: Tue Mar 19, 2013 4:21 pm

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

Postby tulamide » Tue Mar 10, 2020 1:50 pm

kortezzzz wrote:dog
cat
fish
mouse

we'd like to add the word "red" before every index, like "red dog", "red cat" red fish", red mouse"
And then vice versa like "dog red", "cat red", fish red", mouse red".


Code: Select all
animals = ["dog", "cat", "fish", "mouse"]

red_animals = animals.map { |item| "red " << item }
animals_red = animals.map { |item| item << " red"}
"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

Postby kortezzzz » Tue Mar 10, 2020 7:12 pm

Thanks tula, but it's not what I was looking for. The idea is to connect to it any random string array and just add a fixed string to each index (before or after the index), no matter which amount of indexes or words the string array contains. Your example refers to a fixed size array which it's components are already known, I guess.
User avatar
kortezzzz
 
Posts: 763
Joined: Tue Mar 19, 2013 4:21 pm

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

Postby tulamide » Tue Mar 10, 2020 8:36 pm

kortezzzz wrote:Thanks tula, but it's not what I was looking for. The idea is to connect to it any random string array and just add a fixed string to each index (before or after the index), no matter which amount of indexes or words the string array contains. Your example refers to a fixed size array which it's components are already known, I guess.

No. It does exactly what you want. It doesn't matter how many itmes there are in the array. Just copy the code and do your tests by adding removing items, or whatever else you have in mind.
"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

Postby kortezzzz » Tue Mar 10, 2020 9:58 pm

Well, took me a little while until I nailed the exact formulation of the Ruby code for FS. This one worked for me:

Code: Select all
output @ins[0].map { |item| "INPUT " << item }


In this code example, the Ruby module should have one string array input and one string array output. the word "INPUT" in the code means that "INPUT" will be added before every index in the coming array.

Thanks.
User avatar
kortezzzz
 
Posts: 763
Joined: Tue Mar 19, 2013 4:21 pm

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

Postby tulamide » Wed Mar 11, 2020 1:41 pm

kortezzzz wrote:Well, took me a little while until I nailed the exact formulation of the Ruby code for FS. This one worked for me:

Code: Select all
output @ins[0].map { |item| "INPUT " << item }


In this code example, the Ruby module should have one string array input and one string array output. the word "INPUT" in the code means that "INPUT" will be added before every index in the coming array.

Thanks.

I knew you could! I hope this experience gives you a little more self-confidence. Well done!
"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

Postby kortezzzz » Wed Mar 11, 2020 3:53 pm

Thanks tula. Well, it's less a self-confidence issue. It's more about a kind of difficulty to process written data and remembering every single textual command for future usage. When things go visual (greens, blues), my brain works much better and faster. I understand that it's impossible to give FS the depth we need without text coding, but unfortunately, my best solution for that challenge is visualizing my schematics as much as possible. Other than that, almost every messing with a text code becomes a little misery. I know you love Scattering hints around to improve our coding skills, but it looses it's effect 2 days after I use the code, because I can't remember it anymore. That's why I keep every single helpful ruby code in my toolbox :lol:
User avatar
kortezzzz
 
Posts: 763
Joined: Tue Mar 19, 2013 4:21 pm

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

Postby RJHollins » Wed Mar 11, 2020 4:44 pm

I with kortezzz ...

I also appreciate the GURU's here that help.

There have been the odd time where I have put together a RUBY code [after much scanning, experimenting from the RUBY sites] ... that I actually got something that worked.

They are usually not optimized, or slick in any way ... in fact, probably just the opposite. But what it did saved me
an enormous amount of time, and a mess of a schematic trying to use Greens.

I do try to save off any of the posted RUBY modules that do specific things ... but trying to find them afterwards is a navigational nightmare.

Maybe if there is a new FS, it would have a separate RUBY Toolbox section that we could store and organize from. [a part from the Greens, Blues ...]
RJHollins
 
Posts: 1571
Joined: Thu Mar 08, 2012 7:58 pm

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

Postby kortezzzz » Wed Mar 11, 2020 5:17 pm

RJHollins wrote:I with kortezzz ...

I also appreciate the GURU's here that help.

There have been the odd time where I have put together a RUBY code [after much scanning, experimenting from the RUBY sites] ... that I actually got something that worked.

They are usually not optimized, or slick in any way ... in fact, probably just the opposite. But what it did saved me
an enormous amount of time, and a mess of a schematic trying to use Greens.

I do try to save off any of the posted RUBY modules that do specific things ... but trying to find them afterwards is a navigational nightmare.

Maybe if there is a new FS, it would have a separate RUBY Toolbox section that we could store and organize from. [a part from the Greens, Blues ...]


Agreed. So why wouldn't you just create a "Ruby modules" tag in your toolbox and push anything related to Ruby into it? I even name them according to their developers like "trog's ruby bla bla bla" or "tula's ruby bla bla" since it makes easier for me to find something.
User avatar
kortezzzz
 
Posts: 763
Joined: Tue Mar 19, 2013 4:21 pm

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

Postby RJHollins » Wed Mar 11, 2020 7:21 pm

kortezzzz wrote:
RJHollins wrote:I with kortezzz ...

I also appreciate the GURU's here that help.

There have been the odd time where I have put together a RUBY code [after much scanning, experimenting from the RUBY sites] ... that I actually got something that worked.

They are usually not optimized, or slick in any way ... in fact, probably just the opposite. But what it did saved me
an enormous amount of time, and a mess of a schematic trying to use Greens.

I do try to save off any of the posted RUBY modules that do specific things ... but trying to find them afterwards is a navigational nightmare.

Maybe if there is a new FS, it would have a separate RUBY Toolbox section that we could store and organize from. [a part from the Greens, Blues ...]


Agreed. So why wouldn't you just create a "Ruby modules" tag in your toolbox and push anything related to Ruby into it? I even name them according to their developers like "trog's ruby bla bla bla" or "tula's ruby bla bla" since it makes easier for me to find something.


I like to have the, sometime supplied, IN/OUT displays that the RUBY operates on.

Let me ask ... do you just enclose the whole thing into a new module, and then put that into the toolbox ?

I could sure use suggestions to better organize .... everything in my life ... but, specific to FS :oops:
RJHollins
 
Posts: 1571
Joined: Thu Mar 08, 2012 7:58 pm

Next

Return to General

Who is online

Users browsing this forum: Google [Bot] and 40 guests