Page 1 of 1

Ruby Help

Posted: Mon Mar 14, 2016 1:17 am
by S1User
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.

Re: Ruby Help

Posted: Mon Mar 14, 2016 1:15 pm
by Tronic

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

Re: Ruby Help

Posted: Mon Mar 14, 2016 3:42 pm
by Tronic
an small example
small_example_4_S1User.fsm
(679 Bytes) Downloaded 915 times

Re: Ruby Help

Posted: Tue Mar 15, 2016 6:16 pm
by S1User
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. :oops:

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

Re: Ruby Help

Posted: Wed Mar 16, 2016 2:40 am
by Tronic
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

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