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

Array in Ruby component

For general discussion related FlowStone

Array in Ruby component

Postby rob.keij » Mon Jun 15, 2015 9:32 pm

If I make an array in a ruby component
Code: Select all
c = Array.new(64,52) # 64 elements with a value 52


and I initialisize the elements with a value of 52 and by every call of this ruby component I change 1 element.

so after 64 calls I have 64 avaraged values? No..... every time the other 63 element are again 52????

How can I get it not everytime initialisized? I have read 3 ruby books but I can find it how a one time initialising works

I try to use stage(0) but that can not work because Ruby needs to see continue that c is an array.

The green line is a value of 50 the upper white line are the actual values and the white line in the second screen
need to be 64 avaraged values of the upper screen.

screen.png
screen.png (25.19 KiB) Viewed 11115 times
rob.keij
 
Posts: 13
Joined: Mon May 25, 2015 9:23 pm
Location: IJmuiden the Netherlands

Re: Array in Ruby component

Postby KG_is_back » Mon Jun 15, 2015 9:50 pm

You need to use instance variables. You initialize an instance variable by putting "@" at the beginning of a name (for example @variable ). Best place to do so is in the "init" method, which is called first, when the schematic is loaded.

here's an example code (note: it will not work in the form down there - you have to specify the "someIndex" and "someValue" depending on your needs):
Code: Select all
def init
   @a=Array.new(64,52) #initializes instance variable @a, which is array of 64 integers with initial value 52
end

def event i,v
   @a[someIndex]=someValue
   output @a
end



variables wothout the "@" are local variables and they are local to each function call (they are deleted after function returns).
KG_is_back
 
Posts: 1196
Joined: Tue Oct 22, 2013 5:43 pm
Location: Slovakia

Re: Array in Ruby component

Postby rob.keij » Mon Jun 15, 2015 10:29 pm

Thanks, for the explanation,

Very clear I implement it tommorrow
rob.keij
 
Posts: 13
Joined: Mon May 25, 2015 9:23 pm
Location: IJmuiden the Netherlands

Re: Array in Ruby component

Postby RJHollins » Tue Jun 16, 2015 2:05 am

Thanks for the lesson [Ruby this time] KG. Always appreciated !

8-)
RJHollins
 
Posts: 1571
Joined: Thu Mar 08, 2012 7:58 pm

Re: Array in Ruby component

Postby Tronic » Tue Jun 16, 2015 3:23 am

the average for array
Code: Select all
@array = [5,6,7,8]
array_sum = @array.inject(:+).to_f
array_mean = array_sum / @array.size
watch 'mean', array_mean # => 6.5
watch 'sum', array_sum # => 26
Tronic
 
Posts: 539
Joined: Wed Dec 21, 2011 12:59 pm

Re: Array in Ruby component

Postby tulamide » Tue Jun 16, 2015 9:56 am

For those that need an explanantion to tronic's post, have a look at "Ruby Stripped, Part 3: Arrays" on Flowstone GURU, especially paragraph 4, "Enumerators".

'inject' (also known as 'reduce') is a method of the enumerator class, and so is usable for everything that includes the enumerator class, not only arrays. Its purpose is to combine all elements by applying an operation.
You can specify a method or an operator, either by a block or by a symbol. Everything that starts with ':' is a symbol. Ruby automatically creates symbols for standards (if you create a variable @myvar, the symbol :myvar also exists)
':+' is a symbol that represents the plus operator and therefore you order to sum the enumerator list.
By calling @array.inject(0, :+) you give an initial value 0 that will be used instead of nil (array cells or even the array itself could be nil), which will prevent error messages.
You can also call a block instead of specifying a symbol. The same sum operation in block form looks like this:
@array.inject {|sum, var| sum + var}
or, with 0 as initial value
@array.inject(0) {|sum, var| sum + var}
"There lies the dog buried" (German saying translated literally)
tulamide
 
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: Array in Ruby component

Postby rob.keij » Tue Jun 16, 2015 10:00 pm

Thanks for the all information,

I'm 5 steps further in my program,

regards,

Rob
rob.keij
 
Posts: 13
Joined: Mon May 25, 2015 9:23 pm
Location: IJmuiden the Netherlands


Return to General

Who is online

Users browsing this forum: No registered users and 68 guests