Newbie programming question:

Dan,

I am trying to pass a variable to the backticks ``
function, but it is always interpreted as a string.

def ping_host host
  command=Array.new
  command[0]=`ping host`
  puts
  puts command
end

    First off, it is unclear what you are trying to do with the
"command=Array.new" line. You are currently creating an array, and then
setting the first element of the array to the result of "`ping host`".
Unless you plan to do something with the rest of the array, you can get
rid of this line and change the next line to "command = `ping host`".

    Now, as to your real question, what you're looking for is:

    command = `ping #{host}`

And once I do that, how can I get the results into an array?

    It depends on how you want to split the resulting string into an
array. If you want the array to contain one "line" per element, you can
split the string using String#split:

    command = `ping #{host}`.split($/)

    Note that you might need to deal with CR ("\r") characters at the
end of the lines too.

    I hope this helps.

    - Warren Brown

Hi --

    command = `ping #{host}`.split($/)

I've been messing with the unary * a lot today, and thought I'd try it
here:

  *command = *`ping -c2 localhost`

and sure enough, it puts an array of lines in command :slight_smile: (Without
stripping newlines, obviously.) I haven't yet examined exactly *why*
it works, in terms of my dissertation on * earlier today.

(Dan, this is an arcane side-line on your question -- don't worry if
it's obscure :slight_smile:

David

···

On Sat, 21 Aug 2004, Warren Brown wrote:

--
David A. Black
dblack@wobblini.net