Array in Ruby component

For general discussion related FlowStone
Post Reply
rob.keij
Posts: 13
Joined: Mon May 25, 2015 9:23 pm
Location: IJmuiden the Netherlands
Contact:

Array in Ruby component

Post 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
screen.png (25.19 KiB) Viewed 12783 times
KG_is_back
Posts: 1196
Joined: Tue Oct 22, 2013 5:43 pm
Location: Slovakia

Re: Array in Ruby component

Post 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).
rob.keij
Posts: 13
Joined: Mon May 25, 2015 9:23 pm
Location: IJmuiden the Netherlands
Contact:

Re: Array in Ruby component

Post by rob.keij »

Thanks, for the explanation,

Very clear I implement it tommorrow
RJHollins
Posts: 1573
Joined: Thu Mar 08, 2012 7:58 pm

Re: Array in Ruby component

Post by RJHollins »

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

8-)
Tronic
Posts: 539
Joined: Wed Dec 21, 2011 12:59 pm

Re: Array in Ruby component

Post 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
tulamide
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: Array in Ruby component

Post 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}
"There lies the dog buried" (German saying translated literally)
rob.keij
Posts: 13
Joined: Mon May 25, 2015 9:23 pm
Location: IJmuiden the Netherlands
Contact:

Re: Array in Ruby component

Post by rob.keij »

Thanks for the all information,

I'm 5 steps further in my program,

regards,

Rob
Post Reply