Newbie programming question:

Hello,

I am trying to pass a variable to the backticks `` function, but it is
always interpreted as a string. How can I get it to read the value,
instead? And once I do that, how can I get the results into an array?
Here's the example code (this probably looks horribly wrong to some
of you...):

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

puts 'What host would you like to ping?'
  host = gets.chomp
  ping_host host

thanks for any help!

Dan

DiesIrae wrote:

Hello,

I am trying to pass a variable to the backticks `` function, but it is
always interpreted as a string. How can I get it to read the value,
instead? And once I do that, how can I get the results into an array?
Here's the example code (this probably looks horribly wrong to some
of you...):

  command[0]=`ping host`

Do this instead:

   command[0]=`ping #{host}`

The #{} syntax can be used to interpose not just a variable,
but any arbitrary expression.

Solvet saeclum in favilla...

Hal

Hello,

I am trying to pass a variable to the backticks `` function, but it is
always interpreted as a string. How can I get it to read the value,
instead? And once I do that, how can I get the results into an array?
Here's the example code (this probably looks horribly wrong to some
of you...):

def ping_host host
command=Array.new

command[0]=`ping host`

this line should be
command[0]=`ping #{host}`

···

On Sat, 21 Aug 2004 03:15:53 +0900, DiesIrae <danb@mojolin.com> wrote:

puts
puts command
end

puts 'What host would you like to ping?'
host = gets.chomp
ping_host host

thanks for any help!

Dan

--
Cristi BALAN

Hi --

Hello,

I am trying to pass a variable to the backticks `` function, but it is
always interpreted as a string. How can I get it to read the value,
instead? And once I do that, how can I get the results into an array?
Here's the example code (this probably looks horribly wrong to some
of you...):

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

puts 'What host would you like to ping?'
  host = gets.chomp
  ping_host host

You can interpolate your variable using the #{...} construct, and
if you want the results in an array, one line per element, you
can use #to_a:

  def ping_host(host)
    puts
    puts `ping -c2 #{host}`.to_a # 2 pings only
  end

  puts 'What host would you like to ping?'
  ping_host(gets.chomp)

David

···

On Sat, 21 Aug 2004, DiesIrae wrote:

--
David A. Black
dblack@wobblini.net

Hi,

DiesIrae wrote:

I am trying to pass a variable to the backticks `` function, but it is
always interpreted as a string. How can I get it to read the value,
instead? And once I do that, how can I get the results into an array?
Here's the example code (this probably looks horribly wrong to some
of you...):

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

   command[0]=`ping #{host}`

  puts
  puts command
end

puts 'What host would you like to ping?'
  host = gets.chomp
  ping_host host

Note the #{...} notation in the backticks above.

Happy rubying

Stephan

···

--
Stephan Kämper/IT-Beratung http://www.stephankaemper.de
Qaulity assurance / Software Test / Data analysis

"DiesIrae" <danb@mojolin.com> wrote in message

I am trying to pass a variable to the backticks `` function, but it is
always interpreted as a string. How can I get it to read the value,
instead? And once I do that, how can I get the results into an array?
Here's the example code (this probably looks horribly wrong to some
of you...):

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

                            ^^^^^^^^
try this:

     command[0] = `ping #{host}`

···

  puts
  puts command
end

puts 'What host would you like to ping?'
  host = gets.chomp
  ping_host host

thanks for any help!

Dan