Page 2 of 3

Re: Ruby constants

Posted: Fri Feb 08, 2013 9:32 am
by MyCo
I know what self is, this wasn't the question. The question is, why you define a method explicitly with self (which makes it static).

Re: Ruby constants

Posted: Fri Feb 08, 2013 10:22 am
by trogluddite
MyCo wrote:Is it safe to assume, that all RubyEdits are initialized, before they receive external Inputs from other components

I'd want to test some more, but it certainly seems that way - what appears to happen is..
- 'init' run for all RubyEdits
- triggers from any 'output' methods in init get buffered.
- Green starts, and acts on the buffered triggers
- 'event' methods of Rubies act on the triggered data as usual.
I've not yet been able to trip this up by copy/pasting to change the startup order - but, as with you, I have only tried it so far on small schematics with only a few Ruby editors.

MyCo wrote:For the constants problem? From the documentation it looks like this should lock the variable. But in FS, when I try it, I still can change the value. Don't know what's going on there

Yes it's not what I expected either - but it is consistent with Ruby.
Variables are just pointers to objects - so the freeze command doesn't lock the variable, it locks the object to which the variable is pointing. So you can then no longer modify the object by calling its methods - but you can make the variable point to a different object entirely., because the '=' is an assignment, not a method call with the object as its receiver.

Re: Ruby constants

Posted: Fri Feb 08, 2013 8:16 pm
by MyCo
I've played around, and it doesn't work in bigger schematics. Looks like the trigger in this case is sent out before other elements are initialized. When the elements get initialized later, they don't even get the last value from the input, instead they used the values that were stored when the schematic was saved.

Re: Ruby constants

Posted: Fri Feb 08, 2013 10:26 pm
by trogluddite
MyCo wrote:I've played around, and it doesn't work in bigger schematics

Dammit - I thought it was a bit too good to be true. :(

The only workaround I can see is to use an external trigger (After Load prim' ?) to trigger the value send - or maybe schedule a Ruby event from init to trigger it. But then, of course, you've got to make sure that nothing else reads it before that happens (or at least tests for that case and can handle the exception).

Re: Ruby constants

Posted: Fri Feb 08, 2013 10:41 pm
by MyCo
I've already tried the "After Load" that you mentioned. This actually works, but in other RubyEdits you have to make sure, that the callback object is valid. Because there could be other triggered events before the Ruby Callbacks initial "After Load" comes in. Therefor I use "begin... rescue...end".

Re: Ruby constants

Posted: Fri Feb 08, 2013 11:16 pm
by trogluddite
MyCo wrote:Callbacks initial "After Load" comes in. Therefor I use "begin... rescue...end".

Yes, I'm starting to feel the need to look into that too - had a few times where resolving execution order was a lot harder than I though it would be.

Re: Ruby constants

Posted: Fri Feb 08, 2013 11:29 pm
by digitalwhitebyte
MyCo wrote:I know what self is, this wasn't the question. The question is, why you define a method explicitly with self (which makes it static).


def self.Const
1
end

If you send any message to self (e.g., self.Const) it is calling the Const method of your class.
Const= is just a setter method. If you want Const to be a variable, you must not use self.

Re: Ruby constants

Posted: Sat Feb 09, 2013 12:00 am
by MyCo
As far as I understand it, if you define a method with self, you get a static class method. And actually, you don't need static methods in FS Ruby, unless you want to create multiple instances of a class... which you can't do with RubyEdit.

Here is what I mean:

Code: Select all

class Test
   def self.Const
      1
   end
   
   def Const
      2
   end
end

watch "static", Test.Const         # outputs 1
watch "instance", Test.new.Const   # outputs 2

Re: Ruby constants

Posted: Sat Feb 09, 2013 1:12 am
by MyCo
Another Ruby question:
Why does the ruby interpreter completely locks up, when you just type:

Code: Select all

redo


is there an outer loop, that we can't see?

Re: Ruby constants

Posted: Sat Feb 09, 2013 3:44 am
by digitalwhitebyte
Ruby redo Statement:
Syntax: redo
Restarts this iteration of the most internal loop, without checking loop condition.
Restarts yield or call if called within a block.