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 hash of the name/value pairs, to use in pattern matching. Possible keys are: :year
, :month
, :day
, :wday
, :yday
, :hour
, :min
, :sec
, :sec_fraction
, :zone
.
Possible usages:
dt = DateTime.new(2022, 10, 5, 13, 30) if d in wday: 1..5, hour: 10..18 # uses deconstruct_keys underneath puts "Working time" end #=> prints "Working time" case dt in year: ...2022 puts "too old" in month: ..9 puts "quarter 1-3" in wday: 1..5, month: puts "working day in month #{month}" end #=> prints "working day in month 10"
Note that deconstruction by pattern can also be combined with class check:
if d in DateTime(wday: 1..5, hour: 10..18, day: ..7) puts "Working time, first week of the month" end
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}
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.
Returns a hash of the name/value pairs, to use in pattern matching. Possible keys are: :year
, :month
, :day
, :yday
, :wday
, :hour
, :min
, :sec
, :subsec
, :dst
, :zone
.
Possible usages:
t = Time.utc(2022, 10, 5, 21, 25, 30) if t in wday: 3, day: ..7 # uses deconstruct_keys underneath puts "first Wednesday of the month" end #=> prints "first Wednesday of the month" case t in year: ...2022 puts "too old" in month: ..9 puts "quarter 1-3" in wday: 1..5, month: puts "working day in month #{month}" end #=> prints "working day in month 10"
Note that deconstruction by pattern can also be combined with class check:
if t in Time(wday: 3, day: ..7) puts "first Wednesday of the month" end
Returns a data represents the current console mode.
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 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>