Top Level Namespace

Defined Under Namespace

Modules: CV, Enumerable, Merge, Mspire, Obo Classes: Array, IO, Numeric

Instance Method Summary collapse

Instance Method Details

#openany(arg, &block) ⇒ Object

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.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 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) ⇒ Object

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



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 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