Converting String to Tempfile for file interfaces

Usage

result = Tempfile.string_as_file("id,name\n1,foo\n2,bar") do |file|
  FasterCSV.foreach(file) do |row|
    ....
  end
end

Code

class Tempfile
  def self.string_as_file(string)
    result = nil
    Tempfile.open('foo') do |f|
      f.print string
      f.close
      result = yield(f.path)
    end
    result
  end
end

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s