如何在windows命令中使用变量从ruby执行

时间:2014-04-03 10:34:06

标签: ruby windows variables ping

我必须从ruby脚本执行ping命令。 如果我直接执行ping 8.8.8.8就行了。

如何使用变量代替IP?

我试过

dst_ip="8.8.8.8"
ping_string = "ping "+dst_ip
puts ping_string
ping = `ping_string` 

但它有效。

2 个答案:

答案 0 :(得分:0)

因为插值在``运算符中起作用,所以它应该起作用:

ping = `#{ping_string}`

答案 1 :(得分:0)

执行以下操作:

C:\>irb
irb(main):001:0> ip = '66.249.64.0'
=> "66.249.64.0"
irb(main):002:0> `ping #{ip}`
=> "\nPinging 66.249.64.0 with 32 bytes of data:\nRequest timed out.\nRequest ti
med out.\nRequest timed out.\nRequest timed out.\n\nPing statistics for 66.249.6
4.0:\n    Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),\n"
irb(main):003:0>

您不需要像这样进行字符串操作 - ping_string = "ping "+dst_ip