Ruby constants

For general discussion related FlowStone
User avatar
MyCo
Posts: 718
Joined: Tue Jul 13, 2010 12:33 pm
Location: Germany
Contact:

Ruby constants

Post 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:

Code: Select all

XYZ = 123


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
User avatar
trogluddite
Posts: 1730
Joined: Fri Oct 22, 2010 12:46 am
Location: Yorkshire, UK

Re: Ruby constants

Post 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.
All schematics/modules I post are free for all to use - but a credit is always polite!
Don't stagnate, mutate to create!
User avatar
MyCo
Posts: 718
Joined: Tue Jul 13, 2010 12:33 pm
Location: Germany
Contact:

Re: Ruby constants

Post 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 :mrgreen:

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.
Attachments
trig_test.fsm
(3.98 KiB) Downloaded 1142 times
Tronic
Posts: 539
Joined: Wed Dec 21, 2011 12:59 pm

Re: Ruby constants

Post 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?
User avatar
MyCo
Posts: 718
Joined: Tue Jul 13, 2010 12:33 pm
Location: Germany
Contact:

Re: Ruby constants

Post 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
Tronic
Posts: 539
Joined: Wed Dec 21, 2011 12:59 pm

Re: Ruby constants

Post by Tronic »

Here it work...
FS3_Ruby_OBJ_Freeze.JPG
FS3_Ruby_OBJ_Freeze.JPG (16.31 KiB) Viewed 25000 times
User avatar
MyCo
Posts: 718
Joined: Tue Jul 13, 2010 12:33 pm
Location: Germany
Contact:

Re: Ruby constants

Post by MyCo »

yeah, you can't change an object, but you can just re-assign it... doesn't make sense at all ;)
User avatar
digitalwhitebyte
Posts: 106
Joined: Sat Jul 31, 2010 10:20 am

Re: Ruby constants

Post 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 :)
User avatar
MyCo
Posts: 718
Joined: Tue Jul 13, 2010 12:33 pm
Location: Germany
Contact:

Re: Ruby constants

Post by MyCo »

that's the same way, that I've mentioned earlier, but why do you use self?
User avatar
digitalwhitebyte
Posts: 106
Joined: Sat Jul 31, 2010 10:20 am

Re: Ruby constants

Post 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.
Post Reply