newbie programming question:

D

DiesIrae

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
 
H

Hal Fulton

DiesIrae said:
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
 
S

Shashank Date

DiesIrae said:
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}`
 
C

Cristi BALAN

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}`
 
S

Stephan Kämper

Hi,
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
 
D

David A. Black

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,156
Messages
2,570,878
Members
47,406
Latest member
ElizabetMo

Latest Threads

Top