Sha256: 0cfa8adb6d2305c955917e36290983d0e4782b4a509a05f40025035792a10d57

Contents?: true

Size: 1.07 KB

Versions: 6

Compression:

Stored size: 1.07 KB

Contents

#--
# Copyright (c) 2005 George Moschovitis <gm@navel.gr>
#++

# 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'

  # FIND TEST DIRECTORY
  paths = File.expand_path(File.dirname(__FILE__)).split('/')
  paths.size.downto(1) do |i|
    f = (paths.slice(0..i)+['test']).join('/')
    $TESTDIR = File.join(f,'FIXTURE') if File.directory?(f)
  end
  raise unless $TESTDIR

  class TC_CREATE < Test::Unit::TestCase

    def test_create
      f = File.join( $TESTDIR, 'create_file' )
      t = 'This is a test'
      File.create( f, t )
      assert( File.exists?( f ) )
      s = File.read( f )
      assert_equal( t, s )
    end

  end

=end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
facets-1.0.0 lib/facet/file/self/create.rb
facets-0.9.0 lib/nano/file/self/create.rb
facets-1.0.3 packages/core/lib/facet/file/self/create.rb
facets-1.1.0 lib/facet/file/self/create.rb
facets-1.2.0 lib/facets/core/file/self/create.rb
facets-1.2.1 lib/facets/core/file/self/create.rb