Class: WriteFile

Inherits:
Object
  • Object
show all
Defined in:
lib/zipf/fileutil.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fn, encoding = 'utf-8') ⇒ WriteFile

Returns a new instance of WriteFile.



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/zipf/fileutil.rb', line 69

def initialize fn, encoding='utf-8'
  if fn.split('.').last == 'gz'
    @f = Zlib::GzipWriter.new(File.new(fn, 'wb+'), :external_encoding=>encoding)
  elsif fn == '-'
    @f = STDOUT
    STDOUT.set_encoding encoding
  else
    @f = File.new fn, 'w+'
    @f.set_encoding encoding
  end
end

Class Method Details

.write(s, fn, encoding = 'utf-8') ⇒ Object



85
86
87
88
89
# File 'lib/zipf/fileutil.rb', line 85

def self.write s, fn, encoding='utf-8'
  f = WriteFile.new fn, encoding
  f.write s
  f.close
end

Instance Method Details

#closeObject



91
92
93
# File 'lib/zipf/fileutil.rb', line 91

def close
  @f.close if @f!=STDIN
end

#write(s) ⇒ Object



81
82
83
# File 'lib/zipf/fileutil.rb', line 81

def write s
  @f.write s
end