Page 1 of 1
Array in Ruby component
Posted: Mon Jun 15, 2015 9:32 pm
by rob.keij
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 (25.19 KiB) Viewed 12788 times
Re: Array in Ruby component
Posted: Mon Jun 15, 2015 9:50 pm
by KG_is_back
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).
Re: Array in Ruby component
Posted: Mon Jun 15, 2015 10:29 pm
by rob.keij
Thanks, for the explanation,
Very clear I implement it tommorrow
Re: Array in Ruby component
Posted: Tue Jun 16, 2015 2:05 am
by RJHollins
Thanks for the lesson [Ruby this time] KG. Always appreciated !

Re: Array in Ruby component
Posted: Tue Jun 16, 2015 3:23 am
by Tronic
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
Re: Array in Ruby component
Posted: Tue Jun 16, 2015 9:56 am
by tulamide
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}
Re: Array in Ruby component
Posted: Tue Jun 16, 2015 10:00 pm
by rob.keij
Thanks for the all information,
I'm 5 steps further in my program,
regards,
Rob