Support

If you have a problem or need to report a bug please email : support@dsprobotics.com

There are 3 sections to this support area:

DOWNLOADS: access to product manuals, support files and drivers

HELP & INFORMATION: tutorials and example files for learning or finding pre-made modules for your projects

USER FORUMS: meet with other users and exchange ideas, you can also get help and assistance here

NEW REGISTRATIONS - please contact us if you wish to register on the forum

Users are reminded of the forum rules they sign up to which prohibits any activity that violates any laws including posting material covered by copyright

Ruby constants

For general discussion related FlowStone

Ruby constants

Postby MyCo » Fri Feb 08, 2013 1:53 am

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

Re: Ruby constants

Postby trogluddite » Fri Feb 08, 2013 2:57 am

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

Re: Ruby constants

Postby MyCo » Fri Feb 08, 2013 3:51 am

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

Re: Ruby constants

Postby Tronic » Fri Feb 08, 2013 4:01 am

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

Re: Ruby constants

Postby MyCo » Fri Feb 08, 2013 4:11 am

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

Re: Ruby constants

Postby Tronic » Fri Feb 08, 2013 4:16 am

Here it work...
FS3_Ruby_OBJ_Freeze.JPG
FS3_Ruby_OBJ_Freeze.JPG (16.31 KiB) Viewed 22129 times
Tronic
 
Posts: 539
Joined: Wed Dec 21, 2011 12:59 pm

Re: Ruby constants

Postby MyCo » Fri Feb 08, 2013 4:38 am

yeah, you can't change an object, but you can just re-assign it... doesn't make sense at all ;)
User avatar
MyCo
 
Posts: 718
Joined: Tue Jul 13, 2010 12:33 pm
Location: Germany

Re: Ruby constants

Postby digitalwhitebyte » Fri Feb 08, 2013 6:23 am

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
digitalwhitebyte
 
Posts: 106
Joined: Sat Jul 31, 2010 10:20 am

Re: Ruby constants

Postby MyCo » Fri Feb 08, 2013 6:42 am

that's the same way, that I've mentioned earlier, but why do you use self?
User avatar
MyCo
 
Posts: 718
Joined: Tue Jul 13, 2010 12:33 pm
Location: Germany

Re: Ruby constants

Postby digitalwhitebyte » Fri Feb 08, 2013 7:01 am

MyCo wrote:that's the same way, that I've mentioned earlier, but why do you use self?


simple but comprehensive answer here.
User avatar
digitalwhitebyte
 
Posts: 106
Joined: Sat Jul 31, 2010 10:20 am

Next

Return to General

Who is online

Users browsing this forum: No registered users and 68 guests