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 Help
5 posts
• Page 1 of 1
Ruby Help
I can't figure this one out so any help is appreciated.
What I want to do is reference Ruby inputs via var assignments to me the code shorter. For example, say I have inputs named....
value
bg1
font1
border2
bg2
font2
border1
And I want to reference them all in the same block without repetitive coding like below where I'd need separate code blocks for items 1,2,3,4,5,etc...
If value == "1 then
output "myoutput", @bg1
end
I need some way above to replace" @bg1" with an object var that can be assigned in real time to reference that input directly, a way to build a var or string that is recognized as an input object by combing the "1" from the value with something else to make an object reference on the fly that's recognized as being @bg1. Is that possible?
In VB you can do that with strings like Controls.Find("bg" & value), and it would locate the object and allow assigning an object var to it to reference it on the fly. Does Ruby have anything similar?
Thanks.
What I want to do is reference Ruby inputs via var assignments to me the code shorter. For example, say I have inputs named....
value
bg1
font1
border2
bg2
font2
border1
And I want to reference them all in the same block without repetitive coding like below where I'd need separate code blocks for items 1,2,3,4,5,etc...
If value == "1 then
output "myoutput", @bg1
end
I need some way above to replace" @bg1" with an object var that can be assigned in real time to reference that input directly, a way to build a var or string that is recognized as an input object by combing the "1" from the value with something else to make an object reference on the fly that's recognized as being @bg1. Is that possible?
In VB you can do that with strings like Controls.Find("bg" & value), and it would locate the object and allow assigning an object var to it to reference it on the fly. Does Ruby have anything similar?
Thanks.
- S1User
- Posts: 58
- Joined: Thu Sep 17, 2015 4:05 pm
Re: Ruby Help
- Code: Select all
def event i,v,t
# usage:
# @var = get_input_by_case("input_label")
# @var = get_input_by_case("input_label", value)
watch @b = get_input_by_case("bg",@value)
output @b
end
def get_input_by_case(label,value=nil)
get_ = value ? "@" + label + value.to_s : "@" + label
instance_variable_get(get_)
end
- Tronic
- Posts: 539
- Joined: Wed Dec 21, 2011 12:59 pm
Re: Ruby Help
Thanks a lot Tronic. Much appreciated.
On another note, I'm a VB guy and Ruby is new to me so I'm learning. How does this code below look to you? It works but I wonder if it could be done better or smaller. Thanks.
Edit: Holy crap, that previous code I posted was a mess.
On another note, I'm a VB guy and Ruby is new to me so I'm learning. How does this code below look to you? It works but I wonder if it could be done better or smaller. Thanks.
Edit: Holy crap, that previous code I posted was a mess.
- Code: Select all
def event i,v
if i.name == "trigger" then
if File.exists?(@folder) # if the client folder is a valid folder
$SesCount = 0
$Hours = 0
$Total = 0
# look at all files in the current client's folder
Dir.foreach(@folder) do |item|
if item == '.' or item == '..' or item ==''
# skip these nonsense listings
else
# update the session file count
$SesCount = $SesCount + 1
# Read each file into an array to get the data
sesAry = []
IO.foreach(@folder + item) do |line|
sesAry.push(line.chomp())
end
# update the running totals
$Hours = $Hours.to_f + sesAry[3].to_f
$Total = $Total.to_f + sesAry[7].to_f
end
end
# display the totals to the user
output "sessions", $SesCount.to_s
output "hours", $Hours.to_s
output "total", $Total.to_s
end
end
end
- S1User
- Posts: 58
- Joined: Thu Sep 17, 2015 4:05 pm
Re: Ruby Help
Some things to keep in mind is the difference in how you assign the variables.
Class variable (@@a_variable): Available from the class definition and any sub-classes. Not available from anywhere outside.
Instance variable (@a_variable): Available only within a specific object, across all methods in a class instance. Not available directly from class definitions.
Global variable ($a_variable): Available everywhere within your Ruby script.
Local variable (a_variable): It depends on the scope. You’ll be working with these most and thus encounter the most problems, because their scope depends on various things.
continue here... http://www.sitepoint.com/understanding-scope-in-ruby/
below my version
Class variable (@@a_variable): Available from the class definition and any sub-classes. Not available from anywhere outside.
Instance variable (@a_variable): Available only within a specific object, across all methods in a class instance. Not available directly from class definitions.
Global variable ($a_variable): Available everywhere within your Ruby script.
Local variable (a_variable): It depends on the scope. You’ll be working with these most and thus encounter the most problems, because their scope depends on various things.
continue here... http://www.sitepoint.com/understanding-scope-in-ruby/
below my version
- Code: Select all
def event i,v
if i == "trigger"
if File.exists?(@folder) # if the client folder is a valid folder
sesCount = 0
hours = 0
total = 0
# look at all files in the current client's folder
Dir.foreach(@folder) do |item|
# advance if not is an directory
next if File.directory? item
# update the session file count
sesCount += 1
# update the running totals
hours += File.new(@folder+item).readlines[3].to_f
total += File.new(@folder+item).readlines[7].to_f
end
end
# display the totals to the user
output "sessions", sesCount.to_s
output "hours", hours.to_s
output "total", total.to_s
# comment this if not need to debug
watch "debug", [sesCount.to_s,hours.to_s,total.to_s]
end
end
- Tronic
- Posts: 539
- Joined: Wed Dec 21, 2011 12:59 pm
5 posts
• Page 1 of 1
Who is online
Users browsing this forum: No registered users and 76 guests