0

How can I suppress a dump to STDOUT whenever I write to a file? The closest I could get to is using quietly, but it still doesn't suppress output of the file's contents:

quietly do
  puts "shhhh" # (suppressed)
  File.write("filename.txt", "content") #=> "content"
end

EDIT: To make my question clearer, I'm not trying to suppress output to the file itself, but to the console. The File#write above is writing the "content" to the file but is also echoing it back to the console.

Update

Here's the code in context:

namespace :export do
  desc 'Export dictionary entries'
  task :entries => :environment do |task|    
    # fetch entries, iterate over objects, concatenate string representation to "out"

    filename = "entries.rb"
    puts "Exporting entries to #{filename}..." # prints 1st
    File.write(filename, out) # prints at the end
    puts "Exported entries." # prints 2nd
  end
end

I haven't overridden any IO method. But I have discovered that if I define another task like this (inspired from this answer):

task :call_entries => :environment do |task|
  quietly do
    Rake::Task['export:entries'].invoke
  end
end

it actually suppresses all console output.

Community
  • 1
  • 1
  • 1
    The file is not STDOUT. – sawa Jul 30 '15 at 17:16
  • 4
    [`IO::write`](http://ruby-doc.org/core-1.9.3/IO.html#method-c-write) does not output the content of the file it returns a `Fixnum` of the length written. So I am not sure what you are referring too. Please explain your actual issue here because your question cannot be reproduced because it's just not how that method functions. If you must you could just create a method that writes to the file and then return anything you want from the method but this seems contrived. – engineersmnky Jul 30 '15 at 17:29
  • 1
    `File::write` does not output anything or echo anything to the console. – Leo Correa Jul 30 '15 at 17:35
  • All right, could it be because I'm doing this inside a rake task? – Prashant Tiwari Jul 30 '15 at 17:42
  • Is this the last thing in your method? Could it just be dumping the content of it? If so does putting a return nil the line after your write hide it? – Jared Jul 30 '15 at 18:22
  • No, it seems to have nothing to do with the order of the `File#write` — a false or nil at the end makes no difference. – Prashant Tiwari Jul 30 '15 at 18:26
  • The only way `File::write` is *"echoing to the console"* is if you have some implementation specific patch that is causing this. e.g. you application has overwritten the method and implemented a print or puts statement. If this is in a rake task can you post the task itself maybe it has nothing to do with the method at all and something else in the task is causing this output. – engineersmnky Jul 30 '15 at 18:27

0 Answers0