【问题标题】:Ruby on Rails Popen output conversionRuby on Rails Popen 输出转换
【发布时间】:2015-12-01 20:10:10
【问题描述】:

我一直在尝试将数组转换为字符串。最终,我需要做的是:

  1. 将数组转换为字符串
  2. 删除\n 字符
  3. 去掉引号
  4. 将每一行放在视图中的换行符上

我还应该说,我正在尝试这样做而不创建要从中读取的临时文件。

tools_controller.rb

def ping_host(host)
  f = IO.Popen("ping -c 3 #{host}")
  @output = f.readlines
  return @output
end

views/tools/ping.html.erb

<%= @output %>

这很好用,但是,视图中显示的输出还有很多不足之处:

["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"]

我正在想办法把它变成这样的格式:

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

去除所有引号和换行符,并以控制台的方式呈现给用户,而是显示在网页上。

【问题讨论】:

    标签: ruby-on-rails ruby popen


    【解决方案1】:

    可能是这样的吗?

    def ping_host(host)
      f = IO.Popen("ping -c 3 #{host}")
      @output = f.readlines
      return "<pre>#{@output.join}</pre>".html_safe
    end
    

    【讨论】:

    • 时髦!正如我擅长的那样......我让它太复杂了!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多