Ruby - Persistent local variables

For general discussion related FlowStone
Perfect Human Interface
Posts: 643
Joined: Sun Mar 10, 2013 7:32 pm

Ruby - Persistent local variables

Post by Perfect Human Interface »

A little while ago I was a bit surprised to find out that variable states in DSP code blocks persist from one step to the next and aren't initialized every time the code runs through.

Now I'm wondering how this works with Ruby. Specifically working with arrays right now but I'd like to understand just in general. The manual states that the values stored on the inputs and outputs are persistent, but I'm talking about local variables defined within the code. From what I read you can define "events" that will prevent the code block from re-executing code outside of the events, so do you need to utilize this to keep things from re-initializing every time new input is received? Or is that unnecessary, or perhaps there's a different way about it? ...or is it not possible? I really don't know haha.

Hoping it's a simple question to answer. Thanks.
KG_is_back
Posts: 1196
Joined: Tue Oct 22, 2013 5:43 pm
Location: Slovakia

Re: Ruby - Persistent local variables

Post by KG_is_back »

in ruby there are two kinds of variables: local which are colored black and global which are written with @ at the beginning and are colored gold. Actually, @ins and @outs are simply global variables that bridge FS and ruby (they are readable or writable from both).
local variables are specific to a method. You may have several local variables with the same name as long as they are in different methods (in case you don't know, you can initialize your own methods in similar fashion to functions in C). Global variables are simply global. They are accessible from all methods within the ruby component and keep their value until you change it or reload the schematic. You initialize them in the same way like local variables - simply by assigning value to them.
Look at the example in the manual at page 143. There is a sample and hold recreation in ruby that uses global variable "@last" to store the value between events.
Perfect Human Interface
Posts: 643
Joined: Sun Mar 10, 2013 7:32 pm

Re: Ruby - Persistent local variables

Post by Perfect Human Interface »

KG_is_back wrote:You initialize them in the same way like local variables - simply by assigning value to them.


But what if you want to use the variable in a conditional, but don't want to reset the value at every step? The variable needs to be initialized before the conditional, but by assigning a value to it you're overwriting it.
Tronic
Posts: 539
Joined: Wed Dec 21, 2011 12:59 pm

Re: Ruby - Persistent local variables

Post by Tronic »

just define a new Class or Module and assign an constant variable like:
MYCONSTVARIABLE = value
Perfect Human Interface
Posts: 643
Joined: Sun Mar 10, 2013 7:32 pm

Re: Ruby - Persistent local variables

Post by Perfect Human Interface »

Can you give me an example of that in syntax?

For example say I want to do this (just random):

Code: Select all

for i in 0...(@Array.length-1)

if @Array[i] == @input
@Array[i] = 0;
end

end


and I want @Array to be persistent.
Tronic
Posts: 539
Joined: Wed Dec 21, 2011 12:59 pm

Re: Ruby - Persistent local variables

Post by Tronic »

What you mean with "persitent" ?
TheOm
Posts: 103
Joined: Tue Jan 28, 2014 7:35 pm
Location: Germany

Re: Ruby - Persistent local variables

Post by TheOm »

Define your variable in the init function and do your processing in the event function.
Perfect Human Interface
Posts: 643
Joined: Sun Mar 10, 2013 7:32 pm

Re: Ruby - Persistent local variables

Post by Perfect Human Interface »

Tronic wrote:What you mean with "persitent" ?

I want the values to persist every time the code is run through (when an input is received), without being reset (when they're initialized).

TheOm wrote:Define your variable in the init function and do your processing in the event function.

Ok, so I should use the event function. Again would you mind giving me an example of this in syntax I can look at? It's hard to pull up specific things like this searching documentation.
Tronic
Posts: 539
Joined: Wed Dec 21, 2011 12:59 pm

Re: Ruby - Persistent local variables

Post by Tronic »

ok, just initialize an
@@my_global_class_variable
so you ca use it in any other class instance, without initialize it every time.

def init
@@my_global_class_array = new Array[size]
# function to populate array
end

OR

this is only available in current class declaration

def init
@my_local_class_array = new Array[size]
# function to populate array
end
Tronic
Posts: 539
Joined: Wed Dec 21, 2011 12:59 pm

Re: Ruby - Persistent local variables

Post by Tronic »

Full Example:

Code: Select all

def init
    # local class variable present only in this instance
    @my_local_class_array = new Array[size]
    # global class variable present in every other instance
    @@my_global_class_array = new Array[size]
end

def event(i)
# event to to populate the array
    if(i==0) # only if trig is from first input
        @my_local_class_array
        @@my_global_class_array
        #function to populate the array
    end
end
Post Reply