Skip to content

Instantly share code, notes, and snippets.

@maraigue
Created April 17, 2010 15:46
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maraigue/369632 to your computer and use it in GitHub Desktop.
Save maraigue/369632 to your computer and use it in GitHub Desktop.
Ruby implementation of null file (like /dev/null on Un*x, NUL on Windows) The newest version is available by "gem install devnull". See also: http://github.com/maraigue/devnull
# NOTE:
# This version is out-of-date.
# Please access http://github.com/maraigue/devnull for the versions under development.
# Ruby implementation of null file (like /dev/null on Un*x, NUL on Windows)
# (C) 2010- H.Hiro(Maraigue) main@hhiro.net
#
# DevNull works like an IO object. For example:
# dn = DevNull.new
# dn.puts "foo" # => nil (do nothing)
# dn.gets # => nil
# dn.read # => ""
require "enumerator"
class DevNull
def initialize
# do nothing
end
# methods that do nothing
def <<(obj); end
def close; end
def close_read; end
def close_write; end
def fileno; end
def print(*args); end
def printf(*args); end
def putc(*args); end
def puts(*args); end
def seek(*args); end
def syswrite(*args); end
def ungetbyte(*args); end
def ungetc(*args); end
def write(*args); end
# methods that do nothing and returns something
def getc; nil; end
def getbyte; nil; end
def gets(*args); nil; end
def path; nil; end
def pid; nil; end
def binmode; self; end
def flush; self; end
def reopen(*args); self; end
def set_encoding(*args); self; end
def eof; true; end
def eof?; true; end
def sync; true; end
def closed_read?; false; end
def closed_write?; false; end
def isatty; false; end
def tty?; false; end
def fsync; 0; end
def pos; 0; end
def tell; 0; end
def rewind; 0; end
def readlines(*args); []; end
def sync=(arg); arg; end
def truncate(arg); arg; end
def each(*args); (block_given? ? self : [].to_enum); end
alias :each_line :each
alias :each_byte :each
def external_encoding; "US-ASCII"; end
def internal_encoding; "US-ASCII"; end
def fcntl; raise NotImplementedError; end
def readchar; raise EOFError; end
def readbyte; raise EOFError; end
def readline(*args); raise EOFError; end
def read(len = 0, outbuf = nil)
outbuf.replace("") if outbuf != nil
(len.to_i == 0 ? "" : nil)
end
alias :sysread :read
alias :readpartial :read
end
@ujihisa
Copy link

ujihisa commented Apr 19, 2010

This script is what I have needed!

@maraigue
Copy link
Author

Thanks!

There remains some problem, e.g. this DevNull object can be used even if "close"d.

Copy link

ghost commented Dec 30, 2011

Writing after close is only an issue if you're testing to make sure your app acts sanely. Any code that does so is broken.

From what i hear, though, << should return self so that insertions can be chained. Otherwise, stream << "Oops\n" << "this breaks".

@maraigue
Copy link
Author

The first point, file closing, is just because I could not imagine its importance you have indicated.
I will try implementing it.

The second point, << method, is fixed in newly created repository (ordinary github rather than Gist).
http://github.com/maraigue/devnull
I'm sorry I have not lead you to the page of the new version in this Gist page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment