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
24 posts
• Page 1 of 3 • 1, 2, 3
Ruby constants
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
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
-
MyCo - Posts: 718
- Joined: Tue Jul 13, 2010 12:33 pm
- Location: Germany
Re: Ruby constants
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...
Clunky, but I can't think of anything else to suggest.
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!
Don't stagnate, mutate to create!
-
trogluddite - Posts: 1730
- Joined: Fri Oct 22, 2010 12:46 am
- Location: Yorkshire, UK
Re: Ruby constants
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.
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 1081 times
-
MyCo - Posts: 718
- Joined: Tue Jul 13, 2010 12:33 pm
- Location: Germany
Re: Ruby constants
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?
a = [1]
a.freeze
.....
might work?
- Tronic
- Posts: 539
- Joined: Wed Dec 21, 2011 12:59 pm
Re: Ruby constants
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
-
MyCo - Posts: 718
- Joined: Tue Jul 13, 2010 12:33 pm
- Location: Germany
Re: Ruby constants
yeah, you can't change an object, but you can just re-assign it... doesn't make sense at all
-
MyCo - Posts: 718
- Joined: Tue Jul 13, 2010 12:33 pm
- Location: Germany
Re: Ruby constants
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 :)
-
digitalwhitebyte - Posts: 106
- Joined: Sat Jul 31, 2010 10:20 am
Re: Ruby constants
that's the same way, that I've mentioned earlier, but why do you use self?
-
MyCo - Posts: 718
- Joined: Tue Jul 13, 2010 12:33 pm
- Location: Germany
Re: Ruby constants
MyCo wrote:that's the same way, that I've mentioned earlier, but why do you use self?
simple but comprehensive answer here.
-
digitalwhitebyte - Posts: 106
- Joined: Sat Jul 31, 2010 10:20 am
24 posts
• Page 1 of 3 • 1, 2, 3
Who is online
Users browsing this forum: No registered users and 23 guests