Class: TTM::File

Inherits:
Object
  • Object
show all
Defined in:
lib/tatum.rb

Overview

Outputs to a file.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(p_path) ⇒ File

Initializes the object. Stores p_path into @path.

Parameters:

  • p_path (String)


907
908
909
# File 'lib/tatum.rb', line 907

def initialize(p_path)
	@path = p_path
end

Instance Attribute Details

#pathString (readonly)

The path to the file.

Returns:

  • (String)


903
904
905
# File 'lib/tatum.rb', line 903

def path
  @path
end

Instance Method Details

#clearNil

Deletes the ouput file.

Returns:

  • (Nil)


941
942
943
944
945
946
947
948
# File 'lib/tatum.rb', line 941

def clear
	begin
		File.delete @path
	rescue
	end
	
	return nil
end

#puts(str) ⇒ Nil

Outputs the given string to the file. Every call to #puts opens the file, locks it, and appends the string to the end of the file.

Parameters:

  • str (String)

Returns:

  • (Nil)


915
916
917
918
919
920
921
922
923
924
925
926
# File 'lib/tatum.rb', line 915

def puts(str)
	File.open(@path, 'a+') do |file_io|
		file_io.flock File::LOCK_EX
		file_io.seek 0, IO::SEEK_END
		
		begin
			file_io.puts str
		ensure
			file_io.flock File::LOCK_UN
		end
	end
end

#to_sString

Reads the file and returns its content. If the file doesn’t exist then an empty string is returned.

Returns:

  • (String)


931
932
933
934
935
936
937
# File 'lib/tatum.rb', line 931

def to_s
	if File.exist?(@path)
		return File.read(@path)
	else
		return ''
	end
end