Page 1 of 1

Can't add more then one value to array(solved)

Posted: Sat Mar 14, 2015 7:40 am
by Parki
WIth this one I can add more then one value to array.
----------------------
def init
@v1 = []
end

def event i,v
v2 = @ins[0]

if i == 1
@v1.push v2
end

watch "v1:",@v1
end
-----------------------
But with this I can't code the same but v1 is local. Does it possible make it with local array?
------------------------
def init
@v1 = []
end

def event i,v
v2 = @ins[0]

if i == 1
@v1.push v2
end

watch "v1:",@v1
end

Re: Can't add more then one value to array if the array is l

Posted: Sat Mar 14, 2015 9:45 am
by KG_is_back
Your problem is, that local variable is initialized new every time the method is called (in case of event method - every time a trigger is received). in the beginning of each event the array is empty and you add one value to it. There is no way to prevent that - you will have to use @array to store values between events (like in your second example). That's simply how ruby works.
Local variable is local to that given method within each Rubyedit. instance variables (@variable) are local to each Ruby edit (you may have multiple Ruby codes with variable "@variable" and each will have its own.