Mysql Ruby:用存储过程清理我的代码?
这是我的第一个ruby脚本。我想把它清理干净。如何将以下所有Mysql Ruby:用存储过程清理我的代码?,mysql,ruby,Mysql,Ruby,这是我的第一个ruby脚本。我想把它清理干净。如何将以下所有nessus\u扫描。insert()参数存储到变量中,以减少代码中的重复行?我想将:Id=>“”、:scan\u title=>“”等全部存储到这个变量中,然后执行类似nessus\u scans.insert(myvariable)的操作。这个查询在代码中使用了大约5次,这就是为什么我要简化它。谢谢 nessus_scans.insert(:Id => "", :scan_title => "#{scan.t
nessus\u扫描。insert()
参数存储到变量中,以减少代码中的重复行?我想将:Id=>“”、:scan\u title=>“”
等全部存储到这个变量中,然后执行类似nessus\u scans.insert(myvariable)
的操作。这个查询在代码中使用了大约5次,这就是为什么我要简化它。谢谢
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?
这些都是等效的:
def method(arg1, arg2)
p arg1, arg2
end
method(10, a: 20, b:30)
--output:--
10
{:a=>20, :b=>30}
您不需要执行
“#{var}”
,只需使用var
。我建议您学习更多关于Ruby中约定的知识。如果对象(或方法的返回值)已经是字符串,则不需要对其进行插值。如果不是,比如说一个整数,您可以调用some.method.to\u i
。您不应该将布尔值存储为字符串,因为字符串是“truthy”值(运行false | | |“happy”
返回该字符串)。我在您的代码中发现了一个错误。如果您试图运行查询,它可能会失败,因为您没有正确地转义字符串中的双引号。这里是之前和之后:谢谢!!!我不知道括号的把戏。“这正是我要找的。”多布斯,在ruby中它被称为散列。在方法调用中,如果您将一组键/值对列为最后一个参数,那么ruby会自动从它们中创建一个散列,并将散列发送给该方法——您可以使用引用散列的变量显式执行此操作。@dobbs,请参阅其他示例。
def method(arg1, arg2)
p arg1, arg2
end
method(10, a: 20, b:30)
--output:--
10
{:a=>20, :b=>30}
method(10, {a: 20, b:30})
--output:--
10
{:a=>20, :b=>30}
data = {a: 20, b:30}
number = 10
method 10, data
--output:--
10
{:a=>20, :b=>30}