From: Robert Klemme Date: 2013-04-23T23:46:43+09:00 Subject: Re: Problem with array variable --20cf3074d320dd0a1304db084028 Content-Type: text/plain; charset=ISO-8859-1 On Tue, Apr 23, 2013 at 11:06 AM, fox foxmaster wrote: > That was a sweet bit of code, I have scripted some in bash and thats > maybe why the looks like it does :) . > > But I have applied the "ruby" style to the code as you suggested and it > works good for this part: > cmd.each do |c| > command1 = IO.popen(["plink.exe", "-P", port.to_s, "-pw", pw, > "#{user}@#{ip}", c]) {|io| io.each {|line|}} > end > > so beautiful simple. > > but when I get to the part where I want to download the files I get > stuck: > output.each do |f| > download = IO.popen(["pscp.exe", "-P", port.to_s, "-pw", pw, > "#{user}@#{ip}:f"]) {|io| io.each {|line|}} > puts f > puts download > end > > I have modified it a bit to see what values I have in the variables, but > the output is abit strange: > If1.cap > # > If2.cap > # > > I store output in a array, so I wounder is a array item always > surrounded by [item]? Is it possible to show the download in another > format not IO:0x..... more like the actual command? > Well, you can use {|io| io.read} instead of {|io| io.each {|line|}} but that will only give you the output from pscp. And apparently that contains [] because it's an error message. You probably want to look at the other variants of popen to properly evaluate the result code of the external command and react appropriately. http://rdoc.info/stdlib/open3/1.9.3/Open3#popen3-class_method Cheers robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/ --20cf3074d320dd0a1304db084028 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable



On Tue, Apr 23, 2013 at 11:06 AM, fox foxmaster &= lt;lists@ruby-for= um.com> wrote:
That was a sweet bit of code, I have scripted some in bash= and thats
maybe why the looks like it does :) .

But I have applied the "ruby" style to the code as you suggested = and it
works good for this part:
cmd.each do |c|
=A0 command1 =3D IO.popen(["plink.exe", "-= P", port.to_s, "-pw", pw,
"#{user}@#{ip}", c]) {|io| io.each {|line= |}}
end

so beautiful simple.

but when I get to the part where I want to download the files I get
stuck:
output.each do |f|
=A0 download =3D IO.popen(["pscp.exe", "-P", port.to_s,= "-pw", pw,
"#{user}@#{ip}:f"]) {|io| io.each {|line|}}
=A0 puts f
=A0 puts download
end

I have modified it a bit to see what values I have in the variables, but the output is abit strange:
If1.cap
#<IO:0x2845d70>
If2.cap
#<IO:0x28456f8>

I store output in a array, so I wounder is a array item always
surrounded by [item]? Is it possible to show the download in another
format not IO:0x..... more like the actual command?
<= div class=3D"gmail_extra">
Well, you can use

{|io| io.read}

instead of

{|io| io.each {|line|}}

but that will o= nly give you the output from pscp. =A0And apparently that contains [] becau= se it's an error message. =A0You probably want to look at the other var= iants of popen to properly evaluate the result code of the external command= and react appropriately.

Cheers

robert


--
remember.guy do |as, oft= en| as.you_can - without end
http://blog.rubybestpractices.com/
--20cf3074d320dd0a1304db084028--