Module: ConvertableToFile

Included in:
Object
Defined in:
lib/utility_belt/convertable_to_file.rb

Overview

This module adds a method, #to_file, which dumps the contents of self into a temp file and then returns the path of that file. This is particularly useful when calling out to shell commands which expect their input in the form of files.

Example: use UNIX ‘diff’ to compare two objects:

>> a = ["foo", "bar", "baz"].join("\n")
=> "foo\nbar\nbaz"
>> b = ["foo", "buz", "baz"].join("\n")
=> "foo\nbuz\nbaz"
>> puts `diff #{a.to_file} #{b.to_file}`
2c2
< bar
---
> buz
=> nil

Instance Method Summary collapse

Instance Method Details

#to_fileObject



22
23
24
25
26
27
28
29
# File 'lib/utility_belt/convertable_to_file.rb', line 22

def to_file
  path = nil
  Tempfile.open(object_id.to_s) do |tempfile|
    tempfile << self
    path = tempfile.path
  end
  path
end