Page 1 of 3
Ruby constants
Posted: Fri Feb 08, 2013 1:53 am
by MyCo
Hi,
I'm a little bit confused now. I'm looking for a way to define a constant in Ruby, that is only accessable in the RubyEdit it is defined in. Trying:
ended up, being readable in all RubyEdits. The only ways getting the results that I want, is using instance variables (but these would be overwriteable) or using a method that return the value for an input param. Is there another way, that I forgot?
Maik
Re: Ruby constants
Posted: Fri Feb 08, 2013 2:57 am
by trogluddite
Hi MyCo,
No, I don't think you've missed anything - constants are unique to a module or class namespace, but like classes and modules readable anywhere in the interpreter.
If you are looking to keep the value secure from overwrite, using a constant may not help anyway - Ruby allows constants to be overwritten; a warning is sent to STOUT, but no error is raised. In FS, even the STOUT warning doesn't appear.
Using an external value wouldn't help either, as the Ruby inputs can be written to using the input() method and by changing the @ins array contents - they are basically just instance variables.that happen to get overwritten by external inputs whenever a trigger arrives.
The only way I can think of would be to use a method that returns the value you want - if you need the initial value to be dynamic, the method definition could be within an eval that runs at the appropriate time, so long as the value can be turned into a parsable string...
Code: Select all
eval("def getCONSTANT ; #{value} ; end"
Clunky, but I can't think of anything else to suggest.
Re: Ruby constants
Posted: Fri Feb 08, 2013 3:51 am
by MyCo
I'm totally confused by the way Ruby works in FS. Thanks for the confirmation, this means I'm not a complete idiot
Here is another question. In the attached schematic, there is a sender, that sends out its RubyEdit (self) to wireless transmitter (and basically all other RubyEdits).
Is it safe to assume, that all RubyEdits are initialized, before they receive external Inputs from other components, even if they are RubyEdits? Or does it only work when the schematic is that simple like my example?
I'm working on a callback system... so there is one component, that receives "messages" from several other objects, and I want to be sure, that all "sender" objects receive the reference to the "callback" object.
Re: Ruby constants
Posted: Fri Feb 08, 2013 4:01 am
by Tronic
I do not know if it can be useful, but in ruby 1.9.3 there is a feature freeze->obj.
a = [1]
a.freeze
.....
might work?
Re: Ruby constants
Posted: Fri Feb 08, 2013 4:11 am
by MyCo
Tronic wrote:I do not know if it can be useful, but in ruby 1.9.3 there is a feature freeze->obj.
a = [1]
a.freeze
.....
might work?
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
Re: Ruby constants
Posted: Fri Feb 08, 2013 4:16 am
by Tronic
Here it work...

- FS3_Ruby_OBJ_Freeze.JPG (16.31 KiB) Viewed 24990 times
Re: Ruby constants
Posted: Fri Feb 08, 2013 4:38 am
by MyCo
yeah, you can't change an object, but you can just re-assign it... doesn't make sense at all

Re: Ruby constants
Posted: Fri Feb 08, 2013 6:23 am
by digitalwhitebyte
I am using this method and it seems to work, simple and effective
Code: Select all
def self.Const
1
end
watch "1.self.Const", self.Const
# happens here: "1.self.Const = 1"
self.Const = 2 #Try re-assign it
# happens here: "NoMethodError: undefined method 'Const='
watch "2.self.Const", self.Const
# happens here: nothing happens :)
Re: Ruby constants
Posted: Fri Feb 08, 2013 6:42 am
by MyCo
that's the same way, that I've mentioned earlier, but why do you use self?
Re: Ruby constants
Posted: Fri Feb 08, 2013 7:01 am
by digitalwhitebyte
MyCo wrote:that's the same way, that I've mentioned earlier, but why do you use self?
simple but comprehensive answer
here.