[#407177] Downloading a CSV using URI - mechanize — Rochit Sen <lists@...>

Hi All,

13 messages 2013/05/06

[#407178] %tempdir% location and folder clean up? — "Abinash Y." <lists@...>

Hi Guys,

12 messages 2013/05/06

[#407187] Gem Help - Newbie Question — Michael Tepfer <lists@...>

Hi,

17 messages 2013/05/07

[#407234] Problem with FileUtils move command. — Peter Bailey <lists@...>

Hello,

25 messages 2013/05/08
[#407235] Re: Problem with FileUtils move command. — Joel Pearson <lists@...> 2013/05/08

"no implicit conversion of nil into String" means you're probably

[#407236] Re: Problem with FileUtils move command. — Peter Bailey <lists@...> 2013/05/08

Joel Pearson wrote in post #1108209:

[#407238] Re: Problem with FileUtils move command. — Chris Hulan <chris.hulan@...> 2013/05/08

how is the script invoked?

[#407239] Re: Problem with FileUtils move command. — Peter Bailey <lists@...> 2013/05/08

Chris Hulan wrote in post #1108213:

[#407242] Re: Problem with FileUtils move command. — Daniel Stephens <danny.a.stephens@...> 2013/05/08

can you post some of the script?

[#407246] Re: Problem with FileUtils move command. — Love U Ruby <lists@...> 2013/05/08

Daniel Stephens wrote in post #1108224:

[#407250] Re: Problem with FileUtils move command. — Daniel Stephens <danny.a.stephens@...> 2013/05/08

What do you get if you puts "ARGV == #{ARGV[0]}" ?

[#407251] Re: Problem with FileUtils move command. — Peter Bailey <lists@...> 2013/05/08

Daniel Stephens wrote in post #1108238:

[#407252] Re: Problem with FileUtils move command. — Daniel Stephens <danny.a.stephens@...> 2013/05/08

So it's like the other guys mentioned, there appears to be no file(or

[#407255] Re: Problem with FileUtils move command. — Peter Bailey <lists@...> 2013/05/08

Daniel Stephens wrote in post #1108240:

[#407257] Re: Problem with FileUtils move command. — Hassan Schroeder <hassan.schroeder@...> 2013/05/08

On Wed, May 8, 2013 at 6:54 AM, Peter Bailey <lists@ruby-forum.com> wrote:

[#407261] Re: Problem with FileUtils move command. — Peter Bailey <lists@...> 2013/05/08

Hassan Schroeder wrote in post #1108253:

[#407263] Re: Problem with FileUtils move command. — Hassan Schroeder <hassan.schroeder@...> 2013/05/08

On Wed, May 8, 2013 at 8:22 AM, Peter Bailey <lists@ruby-forum.com> wrote:

[#407265] Re: Problem with FileUtils move command. — Peter Bailey <lists@...> 2013/05/08

Hassan Schroeder wrote in post #1108265:

[#407258] Connect to a switch with SSH — "Rudá G." <lists@...>

I need connect to a Cisco Switch with SSH and Ruby. The main problem is

17 messages 2013/05/08

[#407298] Trucking All Http Request from a web page — Ja Tse <lists@...>

Hi,

10 messages 2013/05/09

[#407383] Enum#each issue — Love U Ruby <lists@...>

I am not able to understand why I am getting the error for the method

26 messages 2013/05/13

[#407464] Is it 'safe' to upgrade to ruby 2.0 by now? — Panagiotis Atmatzidis <atma@...>

Hello,

11 messages 2013/05/16

[#407471] Could you give me some Website for Learning Ruby — haxuan lac <lists@...>

I'm learning Ruby and I search some Website for learning Ruby

10 messages 2013/05/16

[#407517] Ruby Programming Practice — "buzz k." <lists@...>

Hey guys have been learning the basics of ruby and practicing some

30 messages 2013/05/19
[#407725] Re: Ruby Programming Practice — "buzz k." <lists@...> 2013/05/29

@Chad Perrin

[#407726] Re: Ruby Programming Practice — Stu <stu@...> 2013/05/29

On Wed, May 29, 2013 at 2:37 AM, buzz k. <lists@ruby-forum.com> wrote:

[#407740] Re: Ruby Programming Practice — Chad Perrin <code@...> 2013/05/29

On Wed, May 29, 2013 at 05:26:44PM +0900, Stu wrote:

[#407742] Re: Ruby Programming Practice — Brandon Weaver <keystonelemur@...> 2013/05/29

Skip FreeBSD and put armored plating on that bike by going OpenBSD.

[#407743] Re: Ruby Programming Practice — Chad Perrin <code@...> 2013/05/29

On Thu, May 30, 2013 at 02:54:51AM +0900, Brandon Weaver wrote:

[#407523] new, with an idea, and not sure what to learn next — Wendy Randquist <lists@...>

Hi. I've been going through the CodeAcademy courses for Ruby over the

13 messages 2013/05/19

[#407527] How to run shoes built via gem — Ruby Student <ruby.student@...>

This is perhaps a silly question.

14 messages 2013/05/19

[#407565] RubyDNS - asynchronous DNS client and server for Ruby. — Samuel Williams <space.ship.traveller@...>

Hi,

15 messages 2013/05/21
[#407567] Re: [ANN] RubyDNS - asynchronous DNS client and server for Ruby. — botp <botpena@...> 2013/05/21

On Tue, May 21, 2013 at 9:05 PM, Samuel Williams <

[#407585] Re: [ANN] RubyDNS - asynchronous DNS client and server for Ruby. — Samuel Williams <space.ship.traveller@...> 2013/05/21

Hi botp, you can create any configuration of DNS you like. RubyDNS doesn't

[#407586] Re: [ANN] RubyDNS - asynchronous DNS client and server for Ruby. — botp <botpena@...> 2013/05/22

On Wed, May 22, 2013 at 7:02 AM, Samuel Williams <

[#407587] Re: [ANN] RubyDNS - asynchronous DNS client and server for Ruby. — Samuel Williams <space.ship.traveller@...> 2013/05/22

Hi, you can use geo-ip for finding the location, geographically speaking.

[#407621] Regular expression to find a break in a pattern — Joel Pearson <lists@...>

I have a large file which lots of gibberish in and I'm trying to find

10 messages 2013/05/23

[#407639] On Green Shoes, do we have a broken arrow? — Ruby Student <ruby.student@...>

Team,

12 messages 2013/05/24

[#407715] AAARRRRGH! s="#$" => SyntaxError: compile error — Tadeusz Bochan <lists@...>

Hi.

13 messages 2013/05/28

[#407738] sending a gmail using ruby — David Munyaka <lists@...>

please help analyzing this code and please give me some recommendations.

26 messages 2013/05/29

[#407763] I can't unsubscribe, please help — Matthew Westerberg <matthew@...>

Hi,

11 messages 2013/05/29

[#407820] Eloquent Ruby Kaprekar's Number — Alphonse 23 <lists@...>

Which way is the eloquent ruby way to write this algorithm?

18 messages 2013/05/30

Re: Slow reading output from Open3.popen3??

From: Robert Klemme <shortcutter@...>
Date: 2013-05-15 16:16:59 UTC
List: ruby-talk #407450
On Wed, May 15, 2013 at 5:11 AM, Jeremy Bopp <jeremy@bopp.net> wrote:

> On 05/14/2013 05:38 PM, Thomas Luedeke wrote:
> > I've never tried using this function before, but I am now using as part
> > of a code compilation build script (using 'make' on UNIX).
> >
> > It looks something like this:
> >
> > ==================================
> >
> > stdin, stdout, stderr = Open3.popen3( 'make codename' )
> > stdin.close
> >
> > puts "Reading STDOUT"
> > outfile.puts stdout.read
> > stdout.close
> >
> > ==================================
> >
> > I'm trying to get the output from the 'make' command (which is quite
> > large), so I can dump it to a file for later diagnostics.
>
> There are a couple ways to tackle what you want to do, but one big
> problem with what you're doing here is that you're trying to read *all*
> of the output from stdout into memory at once.  I'm not sure how big
> "quite large" is, but you should probably do this line by line or in
> limited block sizes instead.
>
> stdout.each_line do |line|
>   outfile.write(line)
> end
>

If you use #each_line then I would also use #puts for output because both
are line oriented.


> OR
>
> while (data = stdout.read(1024)).size > 0
>   outfile.write(data)
> end
>

That doesn't work because #read returns nil at EOF.  You just need

while data = stdout.read(1024)
  outfile.write(data)
end

OR, a tad more efficient

data = ""

while stdout.read(1024, data)
  outfile.write(data)
end


> > However, when it gets to the stdout.read, it seems to hang (or at least
> > take so long I've never seen it finish).
> >
> > Any thoughts on why this is happening?  Is there a better approach for
> > me to try?
>
> You never read from stderr in your example.  I'm wondering if the
> subprocess isn't actually blocking while trying to write something to
> stderr.  Because you never read from it, the buffer for the pipe
> connected to stderr may fill up.  If that were to happen while the
> subprocess was writing to stderr, the subprocess would block while
> trying to write and never get to close its end of the stdout pipe and
> exit.  Thus your read from stdout will block forever.
>

Right!

You need to read from both stdout and stderr in your script to avoid
> this problem; however, this can get complicated since you need to avoid
> blocking while reading from either one of them.  You could also skip
> stderr entirely and just use IO.popen, in which case the stderr output
> will go to the terminal or wherever your script's stderr was wired to
> go.  Finally, you could use IO.popen while redirecting the subprocess'
> stderr to its stdout so that you can read both streams at once:
>
> IO.popen('make codename 2>&1', 'r') do |pipe|
>   pipe.each_line do |line|
>     outfile.write(line)
>   end
> end
>

I wouldn't want to do that because then I cannot differenciate between
regular and error output.


> Given your stated goal of sending the output of make to a file for later
> diagnostics, you would probably be better off simply redirecting the
> output directly to a file and skipping ruby entirely:
>
> bash$ make codename >/path/to/make.log 2>&1
>

Agree.


> The above when run in the bash shell would send both the stdout and
> stderr into the file /path/to/make.log.  If you still wanted to see the
> output on screen at the same time as logging to the file, you can use
> the tee program:
>
> bash$ make codename 2>&1 | tee /path/to/make.log
>
> The same thing happens here as before but you'll also see the output in
> the terminal while make runs.


And here's a solution using open3:

require 'open3'

Open3.popen3( %w{make codename} ) do |stdin, stdout, stderr, t|
  stdin.close
  err_thr = Thread.new { IO.copy_stream(stderr, outfile) }
  puts "Reading STDOUT"
  IO.copy_stream(stdout, outfile)
  err_thr.join
end

Note: I also used an Array for the make command invocation because that
avoids parsing issues in the shell because it omits the shell altogether
(see Process.spawn).

You might also prefer a more line based approach

def copy_lines(str_in, str_out)
  str_in.each_line {|line| str_out.puts line}
end

Open3.popen3( 'make codename' ) do |stdin, stdout, stderr, t|
  stdin.close
  err_thr = Thread.new { copy_lines(stderr, $stderr) }
  puts "Reading STDOUT"
  copy_lines(stdout, $stdout)
  err_thr.join
end

Kind regards

robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

In This Thread