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
Ruby - Persistent local variables
11 posts
• Page 1 of 2 • 1, 2
Ruby - Persistent local variables
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.
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.
- Perfect Human Interface
- Posts: 643
- Joined: Sun Mar 10, 2013 7:32 pm
Re: Ruby - Persistent local variables
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.
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.
- KG_is_back
- Posts: 1196
- Joined: Tue Oct 22, 2013 5:43 pm
- Location: Slovakia
Re: Ruby - Persistent local variables
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.
- Perfect Human Interface
- Posts: 643
- Joined: Sun Mar 10, 2013 7:32 pm
Re: Ruby - Persistent local variables
just define a new Class or Module and assign an constant variable like:
MYCONSTVARIABLE = value
MYCONSTVARIABLE = value
- Tronic
- Posts: 539
- Joined: Wed Dec 21, 2011 12:59 pm
Re: Ruby - Persistent local variables
Can you give me an example of that in syntax?
For example say I want to do this (just random):
and I want @Array to be persistent.
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.
- Perfect Human Interface
- Posts: 643
- Joined: Sun Mar 10, 2013 7:32 pm
Re: Ruby - Persistent local variables
What you mean with "persitent" ?
- Tronic
- Posts: 539
- Joined: Wed Dec 21, 2011 12:59 pm
Re: Ruby - Persistent local variables
Define your variable in the init function and do your processing in the event function.
- TheOm
- Posts: 103
- Joined: Tue Jan 28, 2014 7:35 pm
- Location: Germany
Re: Ruby - Persistent local variables
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.
- Perfect Human Interface
- Posts: 643
- Joined: Sun Mar 10, 2013 7:32 pm
Re: Ruby - Persistent local variables
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
@@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
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
- Tronic
- Posts: 539
- Joined: Wed Dec 21, 2011 12:59 pm
11 posts
• Page 1 of 2 • 1, 2
Who is online
Users browsing this forum: Google [Bot] and 43 guests