Scrolls the entire scrolls forward n
lines.
You must require ‘io/console’ to use this method.
Scrolls the entire scrolls backward n
lines.
You must require ‘io/console’ to use this method.
Clears the entire screen and moves the cursor top-left corner.
You must require ‘io/console’ to use this method.
Waits until IO
is readable and returns a truthy value, or a falsy value when times out. Returns a truthy value immediately when buffered data is available.
You must require ‘io/wait’ to use this method.
Calls the given block with each character in the stream; returns self
. See Character IO.
f = File.new('t.rus') a = [] f.each_char {|c| a << c.ord } a # => [1090, 1077, 1089, 1090] f.close
Returns an Enumerator
if no block is given.
Related: IO#each_byte
, IO#each_codepoint
.
Returns self
.
Closes the stream for writing if open for writing; returns nil
. See Open and Closed Streams.
Flushes any buffered writes to the operating system before closing.
If the stream was opened by IO.popen
and is also closed for reading, sets global variable $?
(child exit status).
IO.popen('ruby', 'r+') do |pipe| puts pipe.closed? pipe.close_read puts pipe.closed? pipe.close_write puts $? puts pipe.closed? end
Output:
false false pid 15044 exit 0 true
Related: IO#close
, IO#close_read
, IO#closed?
.
Returns the path associated with the IO
, or nil
if there is no path associated with the IO
. It is not guaranteed that the path exists on the filesystem.
$stdin.path # => "<STDIN>" File.open("testfile") {|f| f.path} # => "testfile"
Reads at most maxlen bytes from ios using the read(2) system call after O_NONBLOCK is set for the underlying file descriptor.
If the optional outbuf argument is present, it must reference a String
, which will receive the data. The outbuf will contain only the received data after the method call even if it is not empty at the beginning.
read_nonblock
just calls the read(2) system call. It causes all errors the read(2) system call causes: Errno::EWOULDBLOCK, Errno::EINTR, etc. The caller should care such errors.
If the exception is Errno::EWOULDBLOCK or Errno::EAGAIN, it is extended by IO::WaitReadable
. So IO::WaitReadable
can be used to rescue the exceptions for retrying read_nonblock.
read_nonblock
causes EOFError
on EOF.
On some platforms, such as Windows, non-blocking mode is not supported on IO
objects other than sockets. In such cases, Errno::EBADF will be raised.
If the read byte buffer is not empty, read_nonblock
reads from the buffer like readpartial. In this case, the read(2) system call is not called.
When read_nonblock
raises an exception kind of IO::WaitReadable
, read_nonblock
should not be called until io is readable for avoiding busy loop. This can be done as follows.
# emulates blocking read (readpartial). begin result = io.read_nonblock(maxlen) rescue IO::WaitReadable IO.select([io]) retry end
Although IO#read_nonblock
doesn’t raise IO::WaitWritable
. OpenSSL::Buffering#read_nonblock
can raise IO::WaitWritable
. If IO
and SSL should be used polymorphically, IO::WaitWritable
should be rescued too. See the document of OpenSSL::Buffering#read_nonblock
for sample code.
Note that this method is identical to readpartial except the non-blocking flag is set.
By specifying a keyword argument exception to false
, you can indicate that read_nonblock
should not raise an IO::WaitReadable
exception, but return the symbol :wait_readable
instead. At EOF, it will return nil instead of raising EOFError
.
Methods BigDecimal#as_json
and BigDecimal.json_create
may be used to serialize and deserialize a BigDecimal object; see Marshal
.
Method BigDecimal#as_json
serializes self
, returning a 2-element hash representing self
:
require 'json/add/bigdecimal' x = BigDecimal(2).as_json # => {"json_class"=>"BigDecimal", "b"=>"27:0.2e1"} y = BigDecimal(2.0, 4).as_json # => {"json_class"=>"BigDecimal", "b"=>"36:0.2e1"} z = BigDecimal(Complex(2, 0)).as_json # => {"json_class"=>"BigDecimal", "b"=>"27:0.2e1"}
Method JSON.create
deserializes such a hash, returning a BigDecimal object:
BigDecimal.json_create(x) # => 0.2e1 BigDecimal.json_create(y) # => 0.2e1 BigDecimal.json_create(z) # => 0.2e1
Returns a JSON
string representing self
:
require 'json/add/bigdecimal' puts BigDecimal(2).to_json puts BigDecimal(2.0, 4).to_json puts BigDecimal(Complex(2, 0)).to_json
Output:
{"json_class":"BigDecimal","b":"27:0.2e1"} {"json_class":"BigDecimal","b":"36:0.2e1"} {"json_class":"BigDecimal","b":"27:0.2e1"}
Methods OpenStruct#as_json
and OpenStruct.json_create
may be used to serialize and deserialize a OpenStruct object; see Marshal
.
Method OpenStruct#as_json
serializes self
, returning a 2-element hash representing self
:
require 'json/add/ostruct' x = OpenStruct.new('name' => 'Rowdy', :age => nil).as_json # => {"json_class"=>"OpenStruct", "t"=>{:name=>'Rowdy', :age=>nil}}
Method JSON.create
deserializes such a hash, returning a OpenStruct object:
OpenStruct.json_create(x) # => #<OpenStruct name='Rowdy', age=nil>
Returns a JSON
string representing self
:
require 'json/add/ostruct' puts OpenStruct.new('name' => 'Rowdy', :age => nil).to_json
Output:
{"json_class":"OpenStruct","t":{'name':'Rowdy',"age":null}}
Yields all attributes (as symbols) along with the corresponding values or returns an enumerator if no block is given.
require "ostruct" data = OpenStruct.new("country" => "Australia", :capital => "Canberra") data.each_pair.to_a # => [[:country, "Australia"], [:capital, "Canberra"]]
Methods Range#as_json
and Range.json_create
may be used to serialize and deserialize a Range object; see Marshal
.
Method Range#as_json
serializes self
, returning a 2-element hash representing self
:
require 'json/add/range' x = (1..4).as_json # => {"json_class"=>"Range", "a"=>[1, 4, false]} y = (1...4).as_json # => {"json_class"=>"Range", "a"=>[1, 4, true]} z = ('a'..'d').as_json # => {"json_class"=>"Range", "a"=>["a", "d", false]}
Method JSON.create
deserializes such a hash, returning a Range object:
Range.json_create(x) # => 1..4 Range.json_create(y) # => 1...4 Range.json_create(z) # => "a".."d"
Returns a JSON
string representing self
:
require 'json/add/range' puts (1..4).to_json puts (1...4).to_json puts ('a'..'d').to_json
Output:
{"json_class":"Range","a":[1,4,false]} {"json_class":"Range","a":[1,4,true]} {"json_class":"Range","a":["a","d",false]}
Methods Rational#as_json
and Rational.json_create
may be used to serialize and deserialize a Rational object; see Marshal
.
Method Rational#as_json
serializes self
, returning a 2-element hash representing self
:
require 'json/add/rational' x = Rational(2, 3).as_json # => {"json_class"=>"Rational", "n"=>2, "d"=>3}
Method JSON.create
deserializes such a hash, returning a Rational object:
Rational.json_create(x) # => (2/3)
Returns a JSON
string representing self
:
require 'json/add/rational' puts Rational(2, 3).to_json
Output:
{"json_class":"Rational","n":2,"d":3}
Methods Regexp#as_json
and Regexp.json_create
may be used to serialize and deserialize a Regexp object; see Marshal
.
Method Regexp#as_json
serializes self
, returning a 2-element hash representing self
:
require 'json/add/regexp' x = /foo/.as_json # => {"json_class"=>"Regexp", "o"=>0, "s"=>"foo"}
Method JSON.create
deserializes such a hash, returning a Regexp object:
Regexp.json_create(x) # => /foo/