class Object

Public Instance Methods

getCount() click to toggle source
# File lib/ScreenTimer.rb, line 24
def getCount()
        ti = STDIN.gets        
        if ti[0] !~ /\d/ then
                raise("User input is invalid.")
        end

        ks = String.new
        hms = {ho: false,mi: false,se: false}
        counttime = 0
        0.upto(ti.size - 1) do |x|
                if ti[x] =~ /\d/ then
                        ks.concat(ti[x])
                else
                        if ti[x] =~ /[Hh]/  and hms[:ho] == false then
                                counttime += ks.to_i * 60 * 60
                                hms[:ho] = true
                                ks = ""
                        elsif ti[x] =~ /[Hh]/ and hms[:ho] == true then
                                raise("User input is invalid.")
                        end

                        if ti[x] =~ /[Mm]/  and hms[:mi] == false then
                                counttime += ks.to_i * 60
                                hms[:mi] = true
                                ks = ""
                        elsif ti[x] =~ /[Mm]/  and hms[:mi] == true then
                                raise("User input is invalid.")
                        end

                        if ti[x] =~ /[Ss]/ and hms[:se] == false then
                                counttime += ks.to_i
                                hms[:se] = true
                                ks = ""
                        elsif ti[x] =~ /[Ss]/ and hms[:se] == true then
                                raise("User input is invalid.")
                        end
                end
        end
        if hms[:ho] == false and hms[:mi] == false and hms[:se] == false then
                raise("User input is invalid.")
        end
        return counttime
end
secto(s) click to toggle source
# File lib/ScreenTimer.rb, line 3
def secto(s)
        r = {h: 0,m: 0,s: 0}
        loop do
                if s < 60 then
                        r[:s] = s
                        break
                end
                s = s - 60
                r[:m] += 1
        end

        loop do
                if r[:m] < 60 then
                        break
                end
                r[:m] -= 60
                r[:h] += 1
        end
        return r
end