Ruby string randomizer

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

Ruby string randomizer

Post by kortezzzz »

Hi,
Just another ruby challenge:

There is a quite nice ruby randomizer code posted on the forum some while a go which is great for floats. The current code outputs 0-4 values in random order

Code: Select all

def init

end

def event i,v
    @array=[0,1,2,3,4]

    if i == 0 then
       output 0,@array
    end
   
    if i == 1 then
       @randarray = @array.shuffle
       output 0,@randarray
    end

 

end



How can we change this code to deal with string values like [a,b,c,d] ?
KG_is_back
Posts: 1196
Joined: Tue Oct 22, 2013 5:43 pm
Location: Slovakia

Re: Ruby string randomizer

Post by KG_is_back »

It should work with any type of array. You should be able to simply change the connector from float array to string array and it should work.
User avatar
kortezzzz
Posts: 763
Joined: Tue Mar 19, 2013 4:21 pm

Re: Ruby string randomizer

Post by kortezzzz »

Well, It doesn't. It only works if you deal with numbers, but not if you change it to anything else like letters or symbols :?
billv
Posts: 1165
Joined: Tue Aug 31, 2010 3:34 pm
Location: Australia
Contact:

Re: Ruby string randomizer

Post by billv »

Hi Kortezzz..have you got brackets on the strings..eg: 'a','b','c'....?? It should work
adamszabo
Posts: 667
Joined: Sun Jul 11, 2010 7:21 am

Re: Ruby string randomizer

Post by adamszabo »

try this

Code: Select all

def event i,v
    @array=['a','b','c','d','e']

    if i == 0 then
       output 0,@array
    end
   
    if i == 1 then
       @randarray = @array.shuffle
       output 0,@randarray
    end

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

Re: Ruby string randomizer

Post by kortezzzz »

Hi billv and adamszabo,

Yes, that was the problem :D everything is just fine now.

Thank you!

Since I'm kinda sick a little and gave up for working today, I'm trying to take advantage of this free extra time and put some stuff together in some of my old projects. That's why I bother you guy with ruby questions for the whole day :lol:
And still have some loose ends here and there. Here is another one:

Trog posted a long while a go a "ruby-replace string" code. Very useful one, but yet suffers from some lags.
example:

I would like to replace a "space" (which actually means an empty string primitive) with the string "$".
The problem is that code would replace the "space" with "$" correctly, but as long as the sentence contains only letters and numbers. If any symbols added in the sentence (like "%", "*", "@", etc.), it just passes over the replace rule and leave it as a"space". Maybe I just do something wrong?

Attached a little schematic for testing.
Attachments
(string replace problem).fsm
(584 Bytes) Downloaded 894 times
KG_is_back
Posts: 1196
Joined: Tue Oct 22, 2013 5:43 pm
Location: Slovakia

Re: Ruby string randomizer

Post by KG_is_back »

in the line " find=/\b#{@find}\b/ " change it to " find=/#{@find}/ ". Basically remove the "\b" parts.
User avatar
kortezzzz
Posts: 763
Joined: Tue Mar 19, 2013 4:21 pm

Re: Ruby string randomizer

Post by kortezzzz »

Yes :D
Thanks KG!

Guys, you made my day. Thought it's just another wasted one, but I almost finished my whole fix list thanks to you 8-)

There is only one fix left for today, but I'm not sure it would be that easy...

Do you know that old lag with the edit box? I mean when you type something in it and the whole stuff just disappear randomly in the middle of typing? Wondering if there is any creative way to fix this :roll:

T***H***A***N***k YOU ALL :)
RJHollins
Posts: 1573
Joined: Thu Mar 08, 2012 7:58 pm

Re: Ruby string randomizer

Post by RJHollins »

can you post an example with the 'Edit Box' you're using?
User avatar
kortezzzz
Posts: 763
Joined: Tue Mar 19, 2013 4:21 pm

Re: Ruby string randomizer

Post by kortezzzz »

can you post an example with the 'Edit Box' you're using?


Nothing special. Just the stock one. Yesterday I started to play with it a little, and found something interesting:
When you add the edit box in one of your schematic's internal stages, it won't behave wrong as long as you try to edit the text from that stage. But once you try to edit the text from the "top" stage, the problem occurs :roll:
Any ideas?
Post Reply