Sha256: 1c48781e68b56932685a8291749c348ac5bf15a35c07831859273df8d275cae3

Contents?: true

Size: 784 Bytes

Versions: 16

Compression:

Stored size: 784 Bytes

Contents

require 'tempfile'

# 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
#
module ConvertableToFile
  def to_file
    path = nil
    Tempfile.open(object_id.to_s) do |tempfile|
      tempfile << self
      path = tempfile.path
    end
    path
  end
end

class Object
  include ConvertableToFile
end

Version data entries

16 entries across 16 versions & 7 rubygems

Version Path
FreedomCoder-utility_belt-1.1.1.2 lib/utility_belt/convertable_to_file.rb
FreedomCoder-utility_belt-1.1.1 lib/utility_belt/convertable_to_file.rb
akitaonrails-utility_belt-1.0.10 lib/utility_belt/convertable_to_file.rb
akitaonrails-utility_belt-1.0.11 lib/utility_belt/convertable_to_file.rb
akitaonrails-utility_belt-1.0.12 lib/utility_belt/convertable_to_file.rb
mguterl-utility_belt-1.0.7 lib/utility_belt/convertable_to_file.rb
rwilcox-utility_belt-1.0.7 lib/utility_belt/convertable_to_file.rb
rwilcox-utility_belt-1.0.9 lib/utility_belt/convertable_to_file.rb
timocratic-utility_belt-1.0.7.1 lib/utility_belt/convertable_to_file.rb
timocratic-utility_belt-1.0.7.2 lib/utility_belt/convertable_to_file.rb
timocratic-utility_belt-1.0.7.3 lib/utility_belt/convertable_to_file.rb
flyrb-1.0.0.c lib/flyrb/convertable_to_file.rb
flyrb-1.0.0.b lib/flyrb/convertable_to_file.rb
flyrb-1.0.0.a lib/flyrb/convertable_to_file.rb
akitaonrails-utility_belt-1.0.13 lib/utility_belt/convertable_to_file.rb
utility_belt-1.1.0 lib/utility_belt/convertable_to_file.rb