Sha256: 9bd22dfb435b8f9982b1653852ec24d8aa08360c2f7200e2566f08edbc61fbd9

Contents?: true

Size: 1.46 KB

Versions: 10

Compression:

Stored size: 1.46 KB

Contents

# TITLE:
#
#   File Class Read Extensions
#
# DESCRIPTION:
#
#   Class extensions to File for reading
#   and writing.
#
# AUTHORS:
#
#   CREDIT Thomas Sawyer
#   CREDIT George Moschovitis
#   CREDIT Jeffrey Schwab

#
class File

  # Read in a file as binary data.

  def self.read_binary(fname)
    open(fname, 'rb') {|f|
      return f.read
    }
  end

  # Reads in a file, removes blank lines and remarks
  # (lines starting with '#') and then returns
  # an array of all the remaining lines.

  def self.read_list(filepath, chomp_string='')
    farr = nil
    farr = read(filepath).split("\n")
    farr.collect! { |line|
      l = line.strip.chomp(chomp_string)
      (l.empty? or l[0,1] == '#') ? nil : l
    }
    farr.compact
  end

end


#  _____         _
# |_   _|__  ___| |_
#   | |/ _ \/ __| __|
#   | |  __/\__ \ |_
#   |_|\___||___/\__|
#
# TODO This isn't right. I'm not to clear on how to do File class-level Mocks.

=begin #test

  require 'test/unit'

  class TestFileRead < Test::Unit::TestCase

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

    File = MockFile.new

    def test_read_list
      f = File.write("A\nB\nC")
      s = File.read_list( f )
      r = ['A','B','C']
      assert_equal( r, s )
    end

  end

=end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
facets-2.0.0 lib/core/facets/file/read.rb
facets-2.0.1 lib/core/facets/file/read.rb
facets-2.0.2 lib/core/facets/file/read.rb
facets-2.0.4 lib/core/facets/file/read.rb
facets-2.1.1 lib/core/facets/file/read.rb
facets-2.1.2 lib/core/facets/file/read.rb
facets-2.1.0 lib/core/facets/file/read.rb
facets-2.0.5 lib/core/facets/file/read.rb
facets-2.0.3 lib/core/facets/file/read.rb
facets-2.1.3 lib/core/facets/file/read.rb