Returns a hash, that will be turned into a JSON
object and represent this object.
Yields the name and value of each struct member in order. If no block is given an enumerator is returned.
Customer = Struct.new(:name, :address, :zip) joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345) joe.each_pair {|name, value| puts("#{name} => #{value}") }
Produces:
name => Joe Smith address => 123 Maple, Anytown NC zip => 12345
Waits until IO
is readable without blocking and returns self
, or nil
when times out. Returns true
immediately when buffered data is available.
Calls the given block once for each character in ios, passing the character as an argument. The stream must be opened for reading or an IOError
will be raised.
If no block is given, an enumerator is returned instead.
f = File.new("testfile") f.each_char {|c| print c, ' ' } #=> #<File:testfile>
Returns ios.
Closes the write end of a duplex I/O stream (i.e., one that contains both a read and a write stream, such as a pipe). Will raise an IOError
if the stream is not duplexed.
f = IO.popen("/bin/sh","r+") f.close_write f.print "nowhere"
produces:
prog.rb:3:in `write': not opened for writing (IOError) from prog.rb:3:in `print' from prog.rb:3
Calling this method on closed IO
object is just ignored since Ruby 2.3.
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.
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
.
Executes block for each key in the database, passing the key and the corresponding value as a parameter.
Returns a hash, that will be turned into a JSON
object and represent this object.
Stores class name (OpenStruct
) with this struct’s values v
as a JSON
string.
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"]]
Provides marshalling support for use by the Marshal
library.
Provides marshalling support for use by the Marshal
library.
Returns a hash, that will be turned into a JSON
object and represent this object.
Stores class name (Range
) with JSON
array of arguments a
which include first
(integer), last
(integer), and exclude_end?
(boolean) as JSON
string.
Deserializes JSON
string by constructing new Regexp
object with source s
(Regexp
or String) and options o
serialized by to_json
Returns a hash, that will be turned into a JSON
object and represent this object.
Returns a hash, that will be turned into a JSON
object and represent this object.
Returns true if this class can be used to create an instance from a serialised JSON
string. The class has to implement a class method json_create that expects a hash as first parameter. The hash should include the required data.