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

For general discussion related FlowStone
User avatar
kortezzzz
Posts: 763
Joined: Tue Mar 19, 2013 4:21 pm

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

Post by kortezzzz »

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!
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

Post by tulamide »

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)
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

Post by kortezzzz »

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.
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

Post by tulamide »

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)
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

Post by kortezzzz »

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.
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

Post by tulamide »

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)
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

Post by kortezzzz »

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:
RJHollins
Posts: 1573
Joined: Thu Mar 08, 2012 7:58 pm

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

Post by RJHollins »

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 ...]
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

Post by kortezzzz »

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.
RJHollins
Posts: 1573
Joined: Thu Mar 08, 2012 7:58 pm

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

Post by RJHollins »

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:
Post Reply