我在Command
rails console
表格
Command(id: integer, created_at: datetime, updated_at: datetime, timeout:
integer, name: string, command: text, action: string, reboot_action:
string, num_retries: integer, partnum: string, path: string,
firmware: string)
表格中的最后一个命令如下所示:
<Command id: 1, created_at: "2017-02-09 17:44:10",
updated_at: "2017-02-09 17:44:10", timeout: 60,
name: "test", command: "home/app",
action: "up">
那么如何将命令对象传递给方法,以便我将获得有关运行什么命令以及如何处理故障的必需信息。
答案 0 :(得分:0)
如果方法在另一个对象上,您可以将其作为变量传递。如果要在命令对象上调用方法,则已经拥有实例中所需的信息。
def some_method(command)
#do stuff with command
end
command = Command.find 1486662250106112 #get your command instance somewhere
some_method(command) #passing the command instance to the method
答案 1 :(得分:0)
如果你试图在命令记录的命令列中调用shell命令,(旁注:你可能想重新考虑其中一些名称,因为名称“命令”可以引用很多东西......你可以这样做:
command = Command.last # get the command
system(command.command) # invoke the command
实际上,有几种方法可以在ruby中调用shell命令,这只是其中一种常见的方法。有关ruby中shell命令的更多详细信息,请参阅this answer。
最后,如果您担心清理输入,(例如,如果您担心用户插入自定义恶意代码),请查看Shellwords的文档。