On Fri, 2 May 2003, Robert wrote:

> Err, why are you surprised?  This is exactly what I would have expected
> since $defout, $stdout and STDOUT initially point to the same instance.  So
> it doesn't really matter on which of the instances you do issue 'reopen',
> they "all" get redirected.

i'm suprised since STDOUT has the semantics of being a constant (i realize
that the object it points to is not constant).  i would have *thought* that
changing $stdout (a *variable*) would not of changed STDOUT.  all i'm saying
is of what value (in terms of POLS) is it to provied the constant STDOUT *and*
the variable $stdout, when in fact *both* are 'variable'.  i'm completely
understand what's going on, but think it violated POLS for most people.

> Redirecting should be done by assignment:
>
> File.open("output","w") do |f|
>   old_defout = $defout
>
>   begin
>     puts "to the screen"
>
>     # redirect
>     $defout = f
>
>     puts "to the file"
>   ensure
>     $defout = old_defout
>   end
> end
>
> This is much simpler than your solution and works without adding code to IO
> classes:

it's simpler in *this* case, but i would maintain that adding to class IO is
good because

* IO objects *should* (IMHO) have a redirect method - it is a common
  operation.

* adding code to class IO is more *generic* that the above:

  #!/usr/bin/env ruby
  require 'redirect.rb'

  bitbucket = open '/dev/null', 'w'
  $stderr.redirected(bitbucket) do
    system 'ls non-existent-file'
  end

  it is often the case that > 1 fd needs redirected

* easier to read (see above)

* safer - ensure cannot be forgetton by programmer

* class derived from IO will also have this behavior

* it is more flexible:

  log = open 'log', 'w'

  $stdout.redirected(log) do
    ...
    special_log = open 'special', 'w'
    $stdout.redirected(special_log) do
      ...
      extra_special_log = open 'special', 'w'
      $stdout.redirected(extra_special_log) do
	...
      end
    end
  end

* it is 10 lines of code!


anyhow - i can appreciate your opinion but i'm using it in my scripts ;-)  but
then again i *love* the entire

  Resource.aquire do |resource|
    ...
  end

paradigm and the way it makes code read - to me it feels somehow more
ruby-ish.

-a


--
  ====================================
  | Ara Howard
  | NOAA Forecast Systems Laboratory
  | Information and Technology Services
  | Data Systems Group
  | R/FST 325 Broadway
  | Boulder, CO 80305-3328
  | Email: ara.t.howard / fsl.noaa.gov
  | Phone:  303-497-7238
  | Fax:    303-497-7259
  ====================================