Is there something like a null-stream in Ruby?
Asked Answered
D

5

33

I could use:

File.open('/dev/null', 'w')

on Unix systems, but if there is a Ruby way to achieve this, I'd like to use it. I am just looking for an I/O stream, that immediately "trashes" all writes, kind of like a null-object.

Denominate answered 30/12, 2011 at 16:54 Comment(4)
If there isn't one already, it'd be insanely easy to make one. Just have a class with write methods that do nothing.Benfield
That should work fine on Windows, Linux and Mac OS.Quandary
just found this gist gist.github.com/369632Taryn
@tinman sure about that?Denominate
C
5

No, I don't believe there is anything like a null stream in Ruby, at least in earlier versions. In that case, you must make one yourself. Depending on the methods that it will call, you will need to write stub methods on the null stream class, like this:

class NullStream
   def <<(o); self; end
end

The above example is by no means complete. For example, some streams may require calling the write, puts or other methods. Moreover, some methods should be implemented by returning self in their methods, like <<, others not.

Colburn answered 30/12, 2011 at 17:3 Comment(2)
That needs to be def <<(o); self end to work properly (otherwise null_stream << foo << bar will break).Harvard
This nullifies output only on application or user level (virtual or fake streams, string IOs, etc.), not on system level (real streams, fd, device files, etc.). Marc-André Lafortune's answer is probably more appropriate.Prosper
S
84

If you want the full behavior of streams, the best is probably to use:

File.open(File::NULL, "w")

Note that File::NULL is new to Ruby 1.9.3; you can use my backports gem:

require 'backports/1.9.3/file/null' # => Won't do anything in 1.9.3+
File.open(File::NULL, "w")          # => works even in Ruby 1.8.6

You could also copy the relevant code if you prefer.

Sciolism answered 30/12, 2011 at 17:10 Comment(1)
That's excellent. This is how I use it: STDNULL = File.open(File::NULL, 'w') then I use STDNULL every whereForgery
C
11

There's stringIO, which I find useful when I want to introduce a dummy filestream:

require "stringio"
f = StringIO.new
f.gets # => nil

And here's some code from heckle that finds the bit bucket for both Unix and Windows, slightly modified:

# Is this platform MS Windows-like?
# Actually, I suspect the following line is not very reliable.
WINDOWS = RUBY_PLATFORM =~ /mswin/
# Path to the bit bucket.
NULL_PATH = WINDOWS ? 'NUL:' : '/dev/null'
Carotid answered 10/4, 2012 at 4:42 Comment(2)
The StringIO.new trick gets my +1 because it doesn't seem to open a file descriptor, so perhaps doesn't require explicit closing: #4795947Ophthalmology
It doesn't trash input though, so has the potential to cause a memory leak depending on your use case.Phipps
C
5

No, I don't believe there is anything like a null stream in Ruby, at least in earlier versions. In that case, you must make one yourself. Depending on the methods that it will call, you will need to write stub methods on the null stream class, like this:

class NullStream
   def <<(o); self; end
end

The above example is by no means complete. For example, some streams may require calling the write, puts or other methods. Moreover, some methods should be implemented by returning self in their methods, like <<, others not.

Colburn answered 30/12, 2011 at 17:3 Comment(2)
That needs to be def <<(o); self end to work properly (otherwise null_stream << foo << bar will break).Harvard
This nullifies output only on application or user level (virtual or fake streams, string IOs, etc.), not on system level (real streams, fd, device files, etc.). Marc-André Lafortune's answer is probably more appropriate.Prosper
A
2

Logger.new("/dev/null") does the trick

Androcles answered 5/9, 2019 at 15:49 Comment(0)
C
1

There's a gem called devnull

Ruby implementation of null file (like /dev/null on Un*x, NUL on Windows)

It doesn't interact with the null file, but instead has dummy methods for all the methods that IO objects implement.

Carotid answered 21/5, 2014 at 23:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.