我试图将数组转换为字符串。最终,我需要做的是:
\n
个字符我还应该说我试图在不创建临时文件的情况下这样做。
tools_controller.rb
def ping_host(host)
f = IO.Popen("ping -c 3 #{host}")
@output = f.readlines
return @output
end

视图/工具/ ping.html.erb
<%= @output %>
&#13;
这很好用,但是,视图中显示的输出还有很多不足之处:
["PING 10.10.10.1 (10.10.10.1): 56 data bytes\n", "64 bytes from 10.10.10.1: icmp_seq=0 ttl=64 time=1.614 ms\n", "64 bytes from 10.10.10.1: icmp_seq=1 ttl=64 time=1.716 ms\n", "64 bytes from 10.10.10.1: icmp_seq=2 ttl=64 time=1.658 ms\n", "\n", "--- 10.10.10.1 ping statistics ---\n", "3 packets transmitted, 3 packets received, 0.0% packet loss\n", "round-trip min/avg/max/stddev = 1.614/1.663/1.716/0.042 ms\n"]
&#13;
我试图弄清楚如何将其变成这样的格式:
PING 10.10.10.1 (10.10.10.1): 56 data bytes
64 bytes from 10.10.10.1: icmp_seq=0 ttl=64 time=1.614 ms
64 bytes from 10.10.10.1: icmp_seq=1 ttl=64 time=1.716 ms
64 bytes from 10.10.10.1: icmp_seq=2 ttl=64 time=1.658 ms
--- 10.10.10.1 ping statistics ---
3 packets transmitted, 3 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 1.614/1.663/1.716/0.042 ms
它会删除所有引号和换行符,并以类似时尚的方式呈现给用户,而不是在网页上。
答案 0 :(得分:1)
这样的事可能吗?
def ping_host(host)
f = IO.Popen("ping -c 3 #{host}")
@output = f.readlines
return "<pre>#{@output.join}</pre>".html_safe
end