lib/core/facets/file/rewrite.rb in facets-2.8.4 vs lib/core/facets/file/rewrite.rb in facets-2.9.0.pre.1
- old
+ new
@@ -11,16 +11,18 @@
#
# Mode can either be "b" or "+" and specifies to open the file in
# binary mode (no mapping of the plattform's newlines to "\n" is
# done) or to append to it.
#
- # # Reverse contents of "message"
- # File.rewrite("message") { |str| str.reverse }
+ # Assuming we had a file 'message.txt' and had a binary file 'binary.dat'.
#
- # # Replace "foo" by "bar" in "binary"
- # File.rewrite("binary", "b") { |str| str.gsub("foo", "bar") }
+ # # Reverse contents of "message.txt"
+ # File.rewrite("tmp/message.txt") { |str| str.reverse }
#
+ # # Replace "foo" by "bar" in "binary.dat".
+ # File.rewrite("tmp/binary.dat", "b") { |str| str.gsub("foo", "bar") }
+ #
# IMPORTANT: The old version of this method required in place modification
# of the file string. The new version will write whatever the block
# returns instead!!!
#
# CREDIT: George Moschovitis
@@ -52,14 +54,14 @@
# In place version of #rewrite. This version of method requires that the
# string be modified in place within the block.
#
# # Reverse contents of "message"
- # File.rewrite("message") { |str| str.reverse! }
+ # File.rewrite("tmp/message.txt") { |str| str.reverse! }
#
# # Replace "foo" by "bar" in "binary"
- # File.rewrite("binary", "b") { |str| str.gsub!("foo", "bar") }
+ # File.rewrite("tmp/binary.dat", "b") { |str| str.gsub!("foo", "bar") }
#
def self.rewrite!(name, mode = "") #:yield:
unless block_given?
raise(ArgumentError, "Need to supply block to File.rewrite")
end
@@ -77,10 +79,10 @@
begin
yield(new_str)
ensure
if old_str != new_str
- open(name, "w#{mode}") { |file| file.write(str) }
+ open(name, "w#{mode}") { |file| file.write(new_str) }
end
end
end
end