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}
Returns a new Time
object with the same value as self
; if self
is a Julian date, derives its Gregorian date for conversion to the Time object:
Date.new(2001, 2, 3).to_time # => 2001-02-03 00:00:00 -0600 Date.new(2001, 2, 3, Date::JULIAN).to_time # => 2001-02-16 00:00:00 -0600
Returns a DateTime
whose value is the same as self
:
Date.new(2001, 2, 3).to_datetime # => #<DateTime: 2001-02-03T00:00:00+00:00>
See as_json
.
Methods Date#as_json
and Date.json_create
may be used to serialize and deserialize a Date object; see Marshal
.
Method Date#as_json
serializes self
, returning a 2-element hash representing self
:
require 'json/add/date' x = Date.today.as_json # => {"json_class"=>"Date", "y"=>2023, "m"=>11, "d"=>21, "sg"=>2299161.0}
Method JSON.create
deserializes such a hash, returning a Date object:
Date.json_create(x) # => #<Date: 2023-11-21 ((2460270j,0s,0n),+0s,2299161j)>
Returns a JSON
string representing self
:
require 'json/add/date' puts Date.today.to_json
Output:
{"json_class":"Date","y":2023,"m":11,"d":21,"sg":2299161.0}
Returns a Time
object which denotes self.
Returns self.
Methods DateTime#as_json
and DateTime.json_create
may be used to serialize and deserialize a DateTime object; see Marshal
.
Method DateTime#as_json
serializes self
, returning a 2-element hash representing self
:
require 'json/add/datetime' x = DateTime.now.as_json # => {"json_class"=>"DateTime", "y"=>2023, "m"=>11, "d"=>21, "sg"=>2299161.0}
Method JSON.create
deserializes such a hash, returning a DateTime object:
DateTime.json_create(x) # BUG? Raises Date::Error "invalid date"
Returns a JSON
string representing self
:
require 'json/add/datetime' puts DateTime.now.to_json
Output:
{"json_class":"DateTime","y":2023,"m":11,"d":21,"sg":2299161.0}
Returns self.
Returns a DateTime
object which denotes self.
See as_json
.
Methods Time#as_json
and Time.json_create
may be used to serialize and deserialize a Time object; see Marshal
.
Method Time#as_json
serializes self
, returning a 2-element hash representing self
:
require 'json/add/time' x = Time.now.as_json # => {"json_class"=>"Time", "s"=>1700931656, "n"=>472846644}
Method JSON.create
deserializes such a hash, returning a Time object:
Time.json_create(x) # => 2023-11-25 11:00:56.472846644 -0600
Returns a JSON
string representing self
:
require 'json/add/time' puts Time.now.to_json
Output:
{"json_class":"Time","s":1700931678,"n":980650786}
Return the number of seconds the specified time zone differs from UTC.
Numeric
time zones that include minutes, such as -10:00
or +1330
will work, as will simpler hour-only time zones like -10
or +13
.
Textual time zones listed in ZoneOffset are also supported.
If the time zone does not match any of the above, zone_offset
will check if the local time zone (both with and without potential Daylight Saving Time changes being in effect) matches zone
. Specifying a value for year
will change the year used to find the local time zone.
If zone_offset
is unable to determine the offset, nil will be returned.
require 'time' Time.zone_offset("EST") #=> -18000
You must require ‘time’ to use this method.
Waits until IO
is priority and returns a truthy value or a falsy value when times out. Priority data is sent and received using the Socket::MSG_OOB flag and is typically limited to streams.
You must require ‘io/wait’ to use this method.
Attempts to convert object
into an IO object via method to_io
; returns the new IO object if successful, or nil
otherwise:
IO.try_convert(STDOUT) # => #<IO:<STDOUT>> IO.try_convert(ARGF) # => #<IO:<STDIN>> IO.try_convert('STDOUT') # => nil
Returns self
.
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
.
Writes the given string to ios using the write(2) system call after O_NONBLOCK is set for the underlying file descriptor.
It returns the number of bytes written.
write_nonblock
just calls the write(2) system call. It causes all errors the write(2) system call causes: Errno::EWOULDBLOCK, Errno::EINTR, etc. The result may also be smaller than string.length (partial write). The caller should care such errors and partial write.
If the exception is Errno::EWOULDBLOCK or Errno::EAGAIN, it is extended by IO::WaitWritable
. So IO::WaitWritable
can be used to rescue the exceptions for retrying write_nonblock.
# Creates a pipe. r, w = IO.pipe # write_nonblock writes only 65536 bytes and return 65536. # (The pipe size is 65536 bytes on this environment.) s = "a" * 100000 p w.write_nonblock(s) #=> 65536 # write_nonblock cannot write a byte and raise EWOULDBLOCK (EAGAIN). p w.write_nonblock("b") # Resource temporarily unavailable (Errno::EAGAIN)
If the write buffer is not empty, it is flushed at first.
When write_nonblock
raises an exception kind of IO::WaitWritable
, write_nonblock
should not be called until io is writable for avoiding busy loop. This can be done as follows.
begin result = io.write_nonblock(string) rescue IO::WaitWritable, Errno::EINTR IO.select(nil, [io]) retry end
Note that this doesn’t guarantee to write all data in string. The length written is reported as result and it should be checked later.
On some platforms such as Windows, write_nonblock
is not supported according to the kind of the IO
object. In such cases, write_nonblock
raises Errno::EBADF
.
By specifying a keyword argument exception to false
, you can indicate that write_nonblock
should not raise an IO::WaitWritable
exception, but return the symbol :wait_writable
instead.
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>