J
Jos Backus
Another question... How do I access the File open mode ("w" in this case) on a
File object? Example:
class Foo
def initialize(file)
begin
yield self
ensure
# We only want to call finalize when the file was opened for writing;
# The following doesn't work, but how _do_ you do this?
finalize(file) if file.mode.writable? # file was opened with mode "w"
end
end
def finalize(file)
file.puts "END"
end
end
File.open("foo", "w") do |file|
a = Foo.new(file) do
# ... Use a ...
end
end
--
Jos Backus _/ _/_/_/ Sunnyvale, CA
_/ _/ _/
_/ _/_/_/
_/ _/ _/ _/
jos at catnook.com _/_/ _/_/_/ require 'std/disclaimer'
File object? Example:
class Foo
def initialize(file)
begin
yield self
ensure
# We only want to call finalize when the file was opened for writing;
# The following doesn't work, but how _do_ you do this?
finalize(file) if file.mode.writable? # file was opened with mode "w"
end
end
def finalize(file)
file.puts "END"
end
end
File.open("foo", "w") do |file|
a = Foo.new(file) do
# ... Use a ...
end
end
--
Jos Backus _/ _/_/_/ Sunnyvale, CA
_/ _/ _/
_/ _/_/_/
_/ _/ _/ _/
jos at catnook.com _/_/ _/_/_/ require 'std/disclaimer'