Forum: Ruby wirte bellow code in smart way ( need some advise)

7bb2a94f867841b58214be09992831d3?d=identicon&s=25 Fosiul Alam (fosiul)
on 2014-03-26 15:18
I need a help to compact my bellow code or make it write a smart way as
i follow this style , for 100 record i will have to do this 100 times!!

Basically, i am executing a sql command its giving me data like bellow.

now i need to present that in our monitoring system in a table and need
to set threshold for each variables

the monitoring system will generate the html from "msg"

Now the way i am doing, if i have 100 Records i will go MAD!! ,
so i need some advise how to make it more smart and compact.
by using some smart LOOP HASH..

Can any one please give me guide ..


bellow is my code :-


output=`come command`
its output like bellow :-

Field1          56         80         100
Field2          44         33          44
Field3          55         55          66
Field4          3          50          200

Currently i am doing this way

field1=Hash.new
field2=Hash.new
...............
...............


output.each_line do |line|

        if line =~ /Field1/
            cn = line.split(' ')
            field1["Total"]=cn[1]
              if field1["Total"] > 50
                  field1["Total_status"]="Red"
               else
                   field1["Total_status"]="Green"
              end
            field1["Fail"]=cn[2]
              if field2["Fail"] > 50
                  field1["Fail_status"]="Red"
              else
               field1["Fail_status"]="Green"
              end
            field1["Success"]=cn[3]
             if field1["Success"] > 50
                  field1["Success_status"]="Red"
             else
                 field1["Success_status"]="Green"
              end

        end

        if line =~ /Field2/
           ...............
        end
end

MSG="
     <table>
        <td> field1["Total"] field1["Total_status"]  <t/d>  <td>
field1["Fail"] field1["Fail_status"]<td></td> field1["Success"]
field1["Success_status"] </td>

     <td> field2["Total"] field2["Total_status"]  <t/d>  <td>
field2["Fail"] field2["Fail_status"]<td></td> field2["Success"]
field2["Success_status"] </td>

    ..............


      </table>
"
249c7fd851c5c5ac5a1abdb756472ae1?d=identicon&s=25 Arup Rakshit (my-ruby)
on 2014-03-27 17:03
Fosiul Alam wrote in post #1141073:
> I need a help to compact my bellow code or make it write a smart way as
> i follow this style , for 100 record i will have to do this 100 times!!
>
> Basically, i am executing a sql command its giving me data like bellow.
>

> output=`come command`
> its output like bellow :-
>
> Field1          56         80         100
> Field2          44         33          44
> Field3          55         55          66
> Field4          3          50          200
>

You can save it to a temp .txt file. Then open the file and read each
line and use Nokogiri gem to build the HTML.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.