這是我的第一個ruby腳本。我想清理它。我會如何將所有以下nessus_scans.insert()
參數存儲到變量中以減少代碼中的重複行數?我想將:Id => "", :scan_title => ""
等全部存儲到此變量中,然後執行類似nessus_scans.insert(myvariable)
的操作。這個查詢在代碼中使用了大約5次,這就是爲什麼我想簡化它的原因。謝謝!Ruby:清理我的代碼w /存儲過程?
nessus_scans.insert(:Id => "",
:scan_title => "#{scan.title}",
:hostname => "#{host.hostname}",
:host_ip => "#{host.ip}",
:mac_addr => "#{host.mac_addr}",
:netbios_name => "#{host.netbios_name}",
:open_ports => "#{host.ports.map(&:inspect).join(', ').tr('"', '')}",
:operating_system => "#{host.operating_system}",
:start_time => "#{host.start_time}",
:stop_time => "#{host.stop_time}",
:runtime => "#{host.runtime}",
:cve => "#{event.cve}",
:cvss_base_Score => "#{event.cvss_base_score}",
:description => "#{event.description}",
:family => "#{event.family}",
:plugin_id => "#{event.plugin_id}",
:output => "#{event.output}",
:event_name => "#{event.name}",
:patch_publication_date => "#{event.patch_publication_date}",
:plugin_version => "#{event.plugin_version}",
:event_port => "#{event.port.number}",
:risk => "#{event.risk}",
:see_also => "#{event.see_also}",
:severity => "#{event.severity.in_words}",
:solution => "#{event.solution}",
:synopsis => "#{event.synopsis}",
:xref => "#{event.xref}",
:bool_crit => "#{event.critical?}",
:bool_high => "#{event.high?}",
:bool_med => "#{event.medium?}",
:bool_low => "#{event.low?}",
:bool_info => "#{event.informational?}")
end # end event.informational?
你不東東要做''#{var}「',你可以使用'var'。 –
我建議學習更多關於Ruby慣例的知識。如果一個對象(或方法的返回值)已經是一個字符串,則不需要插入它。如果不是,並說一個整數,你可以調用'some.method.to_i'。你不應該把布爾值存儲爲字符串,因爲一個字符串是一個「真值」值(運行'false‖'happy''返回字符串)。 – onebree
我在代碼中發現錯誤。如果您試圖運行查詢,它可能會失敗,因爲您沒有正確地轉義字符串內的雙引號。這裏是之前和之後:http://pastebin.com/jRVNN0Gs – onebree