class Object

Public Instance Methods

openany(arg, &block) click to toggle source

takes a filename or an io object, hands a rewinded io object to the reciever and then closes the file or places the io in the original position.

# File lib/openany.rb, line 5
def openany(arg, &block)
  io = 
    if arg.is_a?(String)  # filename
      File.open(arg)
    else
      orig_pos = arg.pos
      arg.rewind
      arg
    end
  reply = block.call(io)
  if arg.is_a?(String)  # filename
    io.close
  else
    arg.pos = orig_pos
  end
  reply
end
write_file_or_string(filename=nil, &block) click to toggle source

if given a filename, writes to the file (and returns the filename), otherwise, writes to a string. Yields an io object to write to.

# File lib/write_file_or_string.rb, line 4
def write_file_or_string(filename=nil, &block)
  out = 
    if filename
      File.open(filename,'w')
    else
      StringIO.new
    end
  block.call(out)
  if filename
    out.close
    filename
  else
    out.string
  end
end