Class: TTM::File
- Inherits:
-
Object
- Object
- TTM::File
- Defined in:
- lib/tatum.rb
Overview
Outputs to a file.
Instance Attribute Summary collapse
-
#path ⇒ String
readonly
The path to the file.
Instance Method Summary collapse
-
#clear ⇒ Nil
Deletes the ouput file.
-
#initialize(p_path) ⇒ File
constructor
Initializes the object.
-
#puts(str) ⇒ Nil
Outputs the given string to the file.
-
#to_s ⇒ String
Reads the file and returns its content.
Constructor Details
#initialize(p_path) ⇒ File
Initializes the object. Stores p_path into @path.
907 908 909 |
# File 'lib/tatum.rb', line 907 def initialize(p_path) @path = p_path end |
Instance Attribute Details
#path ⇒ String (readonly)
The path to the file.
903 904 905 |
# File 'lib/tatum.rb', line 903 def path @path end |
Instance Method Details
#clear ⇒ Nil
Deletes the ouput file.
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.
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 |