Class: IOHelpers::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/io_helpers/writer.rb

Class Method Summary collapse

Class Method Details

.file(path, data, options = {}) ⇒ Object



3
4
5
# File 'lib/io_helpers/writer.rb', line 3

def self.file(path, data, options = {})
  File.write(path, string(data, options))
end

.string(data, options = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
# File 'lib/io_helpers/writer.rb', line 7

def self.string(data, options = {})
  line_sep = options[:line_sep] || "\n"
  col_sep = options[:col_sep]
  mapper = options[:mapper] || ->(x) { x.to_s }
  if col_sep
    data.map { |line| line.map(&mapper).join(col_sep) }.join(line_sep)
  else
    data.map(&mapper).join(line_sep)
  end
end