class method
IO.write
(::String path, ::_ToS data, ?::Integer offset, ?external_encoding: ::String | ::Encoding | nil, ?internal_encoding: ::String | ::Encoding | nil, ?encoding: ::String | ::Encoding | nil, ?textmode: ::boolish, ?binmode: ::boolish, ?autoclose: ::boolish, ?mode: ::String) -> ::Integer
IO.write(path, data, offset = 0, **opts) → integer
io.c
View on GitHub ↗
static VALUE
rb_io_s_write(int argc, VALUE *argv, VALUE io)
{
return io_s_write(argc, argv, io, 0);
}
Opens the stream, writes the given data to it, and closes the stream; returns the number of bytes written.
When called from class IO (but not subclasses of IO), this method has potential security vulnerabilities if called with untrusted input; see Command Injection.
The first argument must be a string that is the path to a file.
With only argument path given, writes the given data to the file at that path:
Example
IO.write('t.tmp', 'abc') # => 3 File.read('t.tmp') # => "abc"
If offset is zero (the default), the file is overwritten:
Example
IO.write('t.tmp', 'A') # => 1 File.read('t.tmp') # => "A"
If offset in within the file content, the file is partly overwritten:
Example
IO.write('t.tmp', 'abcdef') # => 3 File.read('t.tmp') # => "abcdef" # Offset within content. IO.write('t.tmp', '012', 2) # => 3 File.read('t.tmp') # => "ab012f"
If offset is outside the file content, the file is padded with null characters "\u0000":
Example
IO.write('t.tmp', 'xyz', 10) # => 3 File.read('t.tmp') # => "ab012f\u0000\u0000\u0000\u0000xyz"
Optional keyword arguments opts specify: