I'm stuck trying to convert an array to a string. Ultimately, what I need to do is:
- Convert the array to string
- Remove the
\ncharacters - Remove the quotes
- Place each line on a newline in the view
I should also say that I'm trying to do this without creating a temp file to read from.
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 %>
This works fine and dandy, however, the output presented in the view leaves much to be desired:
["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"]
I'm trying to figure out how to get it into a format like this:
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
Which strips out all of the quotes and newlines and present in console like fashion to the user, but on a webpage instead.