# Blocks question

**URL:** <https://www.ruby-forum.com/t/blocks-question/100135>\
**Category:** Ruby\
**Created:** [July 27, 2006, 10:46am UTC](https://www.ruby-forum.com/t/blocks-question/100135 "2006-07-27T10:46:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![turnip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b77776/32.png) [@turnip](https://www.ruby-forum.com/u/turnip)\
**Post date:** [July 27, 2006, 10:46am UTC](https://www.ruby-forum.com/t/blocks-question/100135/1 "2006-07-27T10:46:29Z")

</div>

Hi,

I have written a script which sets up a new website on a server.  
Obviously this involves a number of steps so I have written a method  
which looks like so:

def step(msg)  
print msg + "… "  
yield  
puts “done”  
end

The idea being that you write something like:

step(“Creating site directory”) { [mkdir code here }

This all works fine, except for one thing. I would like the “Creating  
site directory…” to show up _before_ the yield takes place, and then  
the “done” would appear afterwards. Currently the whole “Creating site  
directory… done” appears only after the yield has completed, which  
might make the user think something has gone wrong with longer steps. I  
don’t understand why this happens, can anyone explain my options please?

Thanks very much

Jon

---

<div class="post-metadata">

**Author:** ![turnip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b77776/32.png) [@turnip](https://www.ruby-forum.com/u/turnip)\
**Post date:** [July 27, 2006, 11:02am UTC](https://www.ruby-forum.com/t/blocks-question/100135/2 "2006-07-27T11:02:49Z")

</div>

On Jul 27, 2006, at 9:46, Jon L. wrote:

> end  
> steps. I  
> don’t understand why this happens, can anyone explain my options  
> please?

I can’t reproduce this on my machine, but it sounds very much like  
it’s being caused by buffered output. This might work:

def step(msg)  
print msg + “…”  
STDOUT.flush  
yield  
puts “done”  
end

matthew smillie.

---

<div class="post-metadata">

**Author:** ![turnip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b77776/32.png) [@turnip](https://www.ruby-forum.com/u/turnip)\
**Post date:** [July 27, 2006, 11:52am UTC](https://www.ruby-forum.com/t/blocks-question/100135/3 "2006-07-27T11:52:02Z")

</div>

Matthew S. wrote:

> On Jul 27, 2006, at 9:46, Jon L. wrote:
> 
> > end  
> > steps. I  
> > don’t understand why this happens, can anyone explain my options  
> > please?
> 
> I can’t reproduce this on my machine, but it sounds very much like  
> it’s being caused by buffered output. This might work:
> 
> def step(msg)  
> print msg + “…”  
> STDOUT.flush  
> yield  
> puts “done”  
> end

Thanks, that works a treat. Now, another related question. Part of the  
process involves checking the code out from Subversion. As Subversion  
checks out, it writes lines to STDOUT saying what it is downloading. I  
would like this to be outputted by by script in real time, rather than  
when the whole command has finished running. Currently I am using a  
method like this:

def command(command)  
open("|#{command}", “r”) do |f|  
f.each do |line|  
puts line  
STDOUT.flush  
end  
end  
end

However, that doesn’t seem to work. The output still only comes when the  
command has fully completed. Any ideas of what I can do?

Thanks!

---

<div class="post-metadata">

**Author:** ![turnip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b77776/32.png) [@turnip](https://www.ruby-forum.com/u/turnip)\
**Post date:** [July 27, 2006, 2:52pm UTC](https://www.ruby-forum.com/t/blocks-question/100135/4 "2006-07-27T14:52:10Z")

</div>

If you don’t need to capture the svn output, try Kernel#system. Svn  
will write directly to STDOUT.

If you do need to capture, then 1. I don’t know 😉 2. it’d depend on  
whether you’re on windows or unix

J.

---

<div class="post-metadata">

**Author:** ![turnip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b77776/32.png) [@turnip](https://www.ruby-forum.com/u/turnip)\
**Post date:** [July 28, 2006, 11:00am UTC](https://www.ruby-forum.com/t/blocks-question/100135/5 "2006-07-28T11:00:08Z")

</div>

Jan S. wrote:

> If you don’t need to capture the svn output, try Kernel#system. Svn  
> will write directly to STDOUT.
> 
> If you do need to capture, then 1. I don’t know 😉 2. it’d depend on  
> whether you’re on windows or unix

I went with the Kernel#system option which worked well. Thanks.
