Page 1 of 1

Input names in Ruby code?

Posted: Sat Nov 16, 2013 6:23 pm
by strangeChild
User Guide wrote:It's useful to be able to name connectors, not only for readability (so it's clear what the data represents)
but also because in the case of input connectors the label can be used within your Ruby code as a
variable or as a reference to an input.

That made perfect sense when I red it the first time... but now I'm trying to learn from existing code in stock modules and I've come across something perplexing.

Inside the toggle switch module is some code that stores/changes the Boolean state of the switch and displays part of a gif strip.

One of the connectors is labeled 'setState' and it is clearly the Boolean input where the switch gets set by the preset.

So looking in the code I expect to see it as '@setState' but instead a variable '@on' seems to be there in the role.

So I wonder what happens when you change input names and I found I could change all the input names and the code will still work. So I'm wondering what the actual mechanism for defining input names is.

Can anyone clear up my confusion on this?

Re: Input names in Ruby code?

Posted: Sat Nov 16, 2013 8:10 pm
by Nubeat7
it is only used in the event methode and compares the input with the @on variable

Code: Select all

def event i,v
   if i==2
      if @on != v
         @on = v
         output 0,@on
      end
   end
   redraw 0
end

it means if i==2 (input ==2)

then compare if @on is not v (value)
so if is not the value at i2
@on takes the value from i2(input 2)

you could also write

Code: Select all

def event i,v
   if i=='setState'
      if @on != v
         @on = v
         output 0,@on
      end
   end
   redraw 0
end

like this it is more clear...

i think the switch is a bit "old" and was done before this feature of using the input names like this (i think its since the last version or the one before)

but yes its a bit confusing you also could write

Code: Select all

def event i,v
  if i == 'setState'
    @on = @setState
    output @on
  end
  redraw
end

like this there is no extracomparing with the @on variable and it always sets @on to the @setState value when a trigger comes in..
i dont really know which difference this makes because the redraw is done anyway?

Re: Input names in Ruby code?

Posted: Sun Nov 17, 2013 1:25 am
by strangeChild
Thanks... while I still don't understand everything about what's going on at least I know where to look.