0

I'm stuck trying to convert an array to a string. Ultimately, what I need to do is:

  1. Convert the array to string
  2. Remove the \n characters
  3. Remove the quotes
  4. 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.

1 Answer 1

1

Something like this maybe?

def ping_host(host)
  f = IO.Popen("ping -c 3 #{host}")
  @output = f.readlines
  return "<pre>#{@output.join}</pre>".html_safe
end
Sign up to request clarification or add additional context in comments.

1 Comment

Snazzy! As I'm good at doing... I was making it too complicated!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.