Module: Nuggets::File::ReplaceMixin
- Included in:
- File
- Defined in:
- lib/nuggets/file/replace_mixin.rb
Instance Method Summary collapse
-
#replace(name, create_if_missing = false, &block) ⇒ Object
call-seq: File.replace(name[, create_if_missing]) { … } => aString File.replace(name[, create_if_missing]) { |content| … } => aString.
Instance Method Details
#replace(name, create_if_missing = false, &block) ⇒ Object
call-seq:
File.replace(name[, create_if_missing]) { ... } => aString
File.replace(name[, create_if_missing]) { |content| ... } => aString
Replaces the contents of file name
with the result of the block. Yields the file’s contents to the block if requested. Returns the new content.
If create_if_missing
is true
and the file does not exist, it will be created.
40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/nuggets/file/replace_mixin.rb', line 40 def replace(name, create_if_missing = false, &block) open(name, create_if_missing && !exist?(name) ? 'w+' : 'r+') { |f| content = block.arity != 0 ? yield(f.read) : yield f.truncate(0) f.rewind f.print content content } end |