Sha256: eb3aa1093bd859233bcafef3f7473a7a6fe8ea286acb6be07311399b3c378df9

Contents?: true

Size: 981 Bytes

Versions: 4

Compression:

Stored size: 981 Bytes

Contents

# CREDIT George Moschovitis 

# Creates a new file, or overwrites an existing file,
# and writes a string into it. Can also take a block
# just like File#open, which is yielded _after_ the
# string is writ.
#
#   str = 'The content for the file'
#   File.create('myfile.txt', str)
#
def File.create(path, str='', &blk)
  File.open(path, 'w') { |f|
    f << str
    blk.call(f) if blk
  }
end


#  _____         _
# |_   _|__  ___| |_
#   | |/ _ \/ __| __|
#   | |  __/\__ \ |_
#   |_|\___||___/\__|
#
=begin test

  require 'test/unit'

  # mock file

  class File
    def self.open( fname, mode, &blk )
      blk.call( self )
    end
    def self.read( fname )
      @mock_content
    end
    def self.<<( str )
      (@mock_content ||="") << str
    end
  end

  class TC_CREATE < Test::Unit::TestCase

    def test_create
      f = "not a reasl file"
      t = 'This is a test'
      File.create( f, t )
      s = File.read( f )
      assert_equal( t, s )
    end

  end

=end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
facets-1.8.20 lib/facets/core/file/self/create.rb
facets-1.8.49 lib/facets/core/file/self/create.rb
facets-1.8.51 lib/facets/core/file/self/create.rb
facets-1.8.54 lib/facets/core/file/self/create.rb