Arguments obj
and opts
here are the same as arguments obj
and opts
in JSON.generate
.
By default, generates JSON data without checking for circular references in obj
(option max_nesting
set to false
, disabled).
Raises an exception if obj
contains circular references:
a = []; b = []; a.push(b); b.push(a) # Raises SystemStackError (stack level too deep): JSON.fast_generate(a)
Arguments obj
and opts
here are the same as arguments obj
and opts
in JSON.generate
.
Default options are:
{ indent: ' ', # Two spaces space: ' ', # One space array_nl: "\n", # Newline object_nl: "\n" # Newline }
Example:
obj = {foo: [:bar, :baz], bat: {bam: 0, bad: 1}} json = JSON.pretty_generate(obj) puts json
Output:
{ "foo": [ "bar", "baz" ], "bat": { "bam": 0, "bad": 1 } }
Returns the number of malloc() allocations.
Only available if ruby was built with CALC_EXACT_MALLOC_SIZE
.
Get the ‘current’ server.
In the context of execution taking place within the main thread of a dRuby server (typically, as a result of a remote call on the server or one of its objects), the current server is that server. Otherwise, the current server is the primary server.
If the above rule fails to find a server, a DRbServerNotFound
error is raised.
Get the ‘current’ server.
In the context of execution taking place within the main thread of a dRuby server (typically, as a result of a remote call on the server or one of its objects), the current server is that server. Otherwise, the current server is the primary server.
If the above rule fails to find a server, a DRbServerNotFound
error is raised.
Registers server
with DRb
.
This is called when a new DRb::DRbServer is created.
If there is no primary server then server
becomes the primary server.
Example:
require 'drb' s = DRb::DRbServer.new # automatically calls regist_server DRb.fetch_server s.uri #=> #<DRb::DRbServer:0x...>
Registers server
with DRb
.
This is called when a new DRb::DRbServer is created.
If there is no primary server then server
becomes the primary server.
Example:
require 'drb' s = DRb::DRbServer.new # automatically calls regist_server DRb.fetch_server s.uri #=> #<DRb::DRbServer:0x...>
Removes server
from the list of registered servers.
Removes server
from the list of registered servers.
Retrieves the server with the given uri
.
See also regist_server
and remove_server.
Add observer
as an observer on this object. So that it will receive notifications.
observer
the object that will be notified of changes.
func
Symbol
naming the method that will be called when this Observable
has changes.
This method must return true for observer.respond_to?
and will receive *arg
when notify_observers
is called, where *arg
is the value passed to notify_observers
by this Observable
Remove observer
as an observer on this object so that it will no longer receive notifications.
observer
An observer of this Observable
Remove all observers associated with this object.
Return the number of observers associated with this object.
Notify observers of a change in state if this object’s changed state is true
.
This will invoke the method named in add_observer
, passing *arg
. The changed state is then set to false
.
*arg
Any arguments to pass to the observers.
A Gem::Version
for the currently running RubyGems
Returns a time returned by POSIX clock_gettime
() function.
p Process.clock_gettime(Process::CLOCK_MONOTONIC) #=> 896053.968060096
clock_id
specifies a kind of clock. It is specified as a constant which begins with Process::CLOCK_
such as Process::CLOCK_REALTIME
and Process::CLOCK_MONOTONIC
.
The supported constants depends on OS and version. Ruby provides following types of clock_id
if available.
CLOCK_REALTIME
SUSv2 to 4, Linux 2.5.63, FreeBSD 3.0, NetBSD 2.0, OpenBSD 2.1, macOS 10.12, Windows-8/Server-2012
CLOCK_MONOTONIC
SUSv3 to 4, Linux 2.5.63, FreeBSD 3.0, NetBSD 2.0, OpenBSD 3.4, macOS 10.12, Windows-2000
CLOCK_PROCESS_CPUTIME_ID
SUSv3 to 4, Linux 2.5.63, FreeBSD 9.3, OpenBSD 5.4, macOS 10.12
CLOCK_THREAD_CPUTIME_ID
SUSv3 to 4, Linux 2.5.63, FreeBSD 7.1, OpenBSD 5.4, macOS 10.12
CLOCK_VIRTUAL
FreeBSD 3.0, OpenBSD 2.1
CLOCK_PROF
FreeBSD 3.0, OpenBSD 2.1
CLOCK_REALTIME_FAST
FreeBSD 8.1
CLOCK_REALTIME_PRECISE
FreeBSD 8.1
CLOCK_REALTIME_COARSE
Linux 2.6.32
CLOCK_REALTIME_ALARM
Linux 3.0
CLOCK_MONOTONIC_FAST
FreeBSD 8.1
CLOCK_MONOTONIC_PRECISE
FreeBSD 8.1
CLOCK_MONOTONIC_COARSE
Linux 2.6.32
CLOCK_MONOTONIC_RAW
Linux 2.6.28, macOS 10.12
CLOCK_MONOTONIC_RAW_APPROX
macOS 10.12
CLOCK_BOOTTIME
Linux 2.6.39
CLOCK_BOOTTIME_ALARM
Linux 3.0
CLOCK_UPTIME
FreeBSD 7.0, OpenBSD 5.5
CLOCK_UPTIME_FAST
FreeBSD 8.1
CLOCK_UPTIME_RAW
macOS 10.12
CLOCK_UPTIME_RAW_APPROX
macOS 10.12
CLOCK_UPTIME_PRECISE
FreeBSD 8.1
CLOCK_SECOND
FreeBSD 8.1
CLOCK_TAI
Linux 3.10
Note that SUS stands for Single Unix Specification. SUS contains POSIX and clock_gettime
is defined in the POSIX part. SUS defines CLOCK_REALTIME
mandatory but CLOCK_MONOTONIC
, CLOCK_PROCESS_CPUTIME_ID
and CLOCK_THREAD_CPUTIME_ID
are optional.
Also, several symbols are accepted as clock_id
. There are emulations for clock_gettime
().
For example, Process::CLOCK_REALTIME
is defined as :GETTIMEOFDAY_BASED_CLOCK_REALTIME
when clock_gettime
() is not available.
Emulations for CLOCK_REALTIME
:
Use gettimeofday() defined by SUS. (SUSv4 obsoleted it, though.) The resolution is 1 microsecond.
Use time() defined by ISO C. The resolution is 1 second.
Emulations for CLOCK_MONOTONIC
:
Use mach_absolute_time(), available on Darwin. The resolution is CPU dependent.
Use the result value of times() defined by POSIX. POSIX defines it as “times() shall return the elapsed real time, in clock ticks, since an arbitrary point in the past (for example, system start-up time)”. For example, GNU/Linux returns a value based on jiffies and it is monotonic. However, 4.4BSD uses gettimeofday() and it is not monotonic. (FreeBSD uses clock_gettime
(CLOCK_MONOTONIC
) instead, though.) The resolution is the clock tick. “getconf CLK_TCK” command shows the clock ticks per second. (The clock ticks per second is defined by HZ macro in older systems.) If it is 100 and clock_t is 32 bits integer type, the resolution is 10 millisecond and cannot represent over 497 days.
Emulations for CLOCK_PROCESS_CPUTIME_ID
:
Use getrusage() defined by SUS. getrusage() is used with RUSAGE_SELF to obtain the time only for the calling process (excluding the time for child processes). The result is addition of user time (ru_utime) and system time (ru_stime). The resolution is 1 microsecond.
Use times() defined by POSIX. The result is addition of user time (tms_utime) and system time (tms_stime). tms_cutime and tms_cstime are ignored to exclude the time for child processes. The resolution is the clock tick. “getconf CLK_TCK” command shows the clock ticks per second. (The clock ticks per second is defined by HZ macro in older systems.) If it is 100, the resolution is 10 millisecond.
Use clock() defined by ISO C. The resolution is 1/CLOCKS_PER_SEC. CLOCKS_PER_SEC is the C-level macro defined by time.h. SUS defines CLOCKS_PER_SEC is 1000000. Non-Unix systems may define it a different value, though. If CLOCKS_PER_SEC is 1000000 as SUS, the resolution is 1 microsecond. If CLOCKS_PER_SEC is 1000000 and clock_t is 32 bits integer type, it cannot represent over 72 minutes.
If the given clock_id
is not supported, Errno::EINVAL is raised.
unit
specifies a type of the return value.
number of seconds as a float (default)
number of milliseconds as a float
number of microseconds as a float
number of seconds as an integer
number of milliseconds as an integer
number of microseconds as an integer
number of nanoseconds as an integer
The underlying function, clock_gettime
(), returns a number of nanoseconds. Float
object (IEEE 754 double) is not enough to represent the return value for CLOCK_REALTIME
. If the exact nanoseconds value is required, use :nanosecond
as the unit
.
The origin (zero) of the returned value varies. For example, system start up time, process start up time, the Epoch, etc.
The origin in CLOCK_REALTIME
is defined as the Epoch (1970-01-01 00:00:00 UTC). But some systems count leap seconds and others doesn’t. So the result can be interpreted differently across systems. Time.now
is recommended over CLOCK_REALTIME
.
Returns an estimate of the resolution of a clock_id
using the POSIX clock_getres()
function.
Note the reported resolution is often inaccurate on most platforms due to underlying bugs for this function and therefore the reported resolution often differs from the actual resolution of the clock in practice. Inaccurate reported resolutions have been observed for various clocks including CLOCK_MONOTONIC
and CLOCK_MONOTONIC_RAW
when using Linux, macOS, BSD or AIX platforms, when using ARM processors, or when using virtualization.
clock_id
specifies a kind of clock. See the document of Process.clock_gettime
for details. clock_id
can be a symbol as for Process.clock_gettime
.
If the given clock_id
is not supported, Errno::EINVAL is raised.
unit
specifies the type of the return value. Process.clock_getres
accepts unit
as Process.clock_gettime
. The default value, :float_second
, is also the same as Process.clock_gettime
.
Process.clock_getres
also accepts :hertz
as unit
. :hertz
means the reciprocal of :float_second
.
:hertz
can be used to obtain the exact value of the clock ticks per second for the times() function and CLOCKS_PER_SEC for the clock() function.
Process.clock_getres(:TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID, :hertz)
returns the clock ticks per second.
Process.clock_getres(:CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID, :hertz)
returns CLOCKS_PER_SEC.
p Process.clock_getres(Process::CLOCK_MONOTONIC) #=> 1.0e-09
Returns a non-lazy Enumerator
converted from the lazy enumerator.
Creates a new Socket::Option
object for SOL_SOCKET/SO_LINGER.
onoff should be an integer or a boolean.
secs should be the number of seconds.
p Socket::Option.linger(true, 10) #=> #<Socket::Option: UNSPEC SOCKET LINGER on 10sec>
Returns the linger data in sockopt as a pair of boolean and integer.
sockopt = Socket::Option.linger(true, 10) p sockopt.linger => [true, 10]