Returns a String containing the generated JSON data.
See also JSON.fast_generate
, JSON.pretty_generate
.
Argument obj
is the Ruby object to be converted to JSON.
Argument opts
, if given, contains a Hash of options for the generation. See Generating Options.
When obj
is an Array, returns a String containing a JSON array:
obj = ["foo", 1.0, true, false, nil] json = JSON.generate(obj) json # => '["foo",1.0,true,false,null]'
When obj
is a Hash, returns a String containing a JSON object:
obj = {foo: 0, bar: 's', baz: :bat} json = JSON.generate(obj) json # => '{"foo":0,"bar":"s","baz":"bat"}'
For examples of generating from other Ruby objects, see Generating JSON from Other Objects.
Raises an exception if any formatting option is not a String.
Raises an exception if obj
contains circular references:
a = []; b = []; a.push(b); b.push(a) # Raises JSON::NestingError (nesting of 100 is too deep): JSON.generate(a)
Returns the base base
logarithm of x
.
Domain: [0, INFINITY]
.
Range: [-INFINITY, INFINITY)]
.
Examples:
log(0.0) # => -Infinity log(1.0) # => 0.0 log(E) # => 1.0 log(INFINITY) # => Infinity log(0.0, 2.0) # => -Infinity log(1.0, 2.0) # => 0.0 log(2.0, 2.0) # => 1.0 log(0.0, 10.0) # => -Infinity log(1.0, 10.0) # => 0.0 log(10.0, 10.0) # => 1.0
Returns the base 2 logarithm of x
.
Domain: [0, INFINITY]
.
Range: [-INFINITY, INFINITY]
.
Examples:
log2(0.0) # => -Infinity log2(1.0) # => 0.0 log2(2.0) # => 1.0 log2(INFINITY) # => Infinity
Returns the base 10 logarithm of x
.
Domain: [0, INFINITY]
.
Range: [-INFINITY, INFINITY]
.
Examples:
log10(0.0) # => -Infinity log10(1.0) # => 0.0 log10(10.0) # => 1.0 log10(INFINITY) # => Infinity
creates a TCP/IP server on port and calls the block for each connection accepted. The block is called with a socket and a client_address as an Addrinfo
object.
If host is specified, it is used with port to determine the server addresses.
The socket is not closed when the block returns. So application should close it explicitly.
This method calls the block sequentially. It means that the next connection is not accepted until the block returns. So concurrent mechanism, thread for example, should be used to service multiple clients at a time.
Note that Addrinfo.getaddrinfo
is used to determine the server socket addresses. When Addrinfo.getaddrinfo
returns two or more addresses, IPv4 and IPv6 address for example, all of them are used. Socket.tcp_server_loop
succeeds if one socket can be used at least.
# Sequential echo server. # It services only one client at a time. Socket.tcp_server_loop(16807) {|sock, client_addrinfo| begin IO.copy_stream(sock, sock) ensure sock.close end } # Threaded echo server # It services multiple clients at a time. # Note that it may accept connections too much. Socket.tcp_server_loop(16807) {|sock, client_addrinfo| Thread.new { begin IO.copy_stream(sock, sock) ensure sock.close end } }
creates a UDP/IP server on port and calls the block for each message arrived. The block is called with the message and its source information.
This method allocates sockets internally using port. If host is specified, it is used conjunction with port to determine the server addresses.
The msg is a string.
The msg_src is a Socket::UDPSource
object. It is used for reply.
# UDP/IP echo server. Socket.udp_server_loop(9261) {|msg, msg_src| msg_src.reply msg }
creates a UNIX socket server on path. It calls the block for each socket accepted.
If host is specified, it is used with port to determine the server ports.
The socket is not closed when the block returns. So application should close it.
This method deletes the socket file pointed by path at first if the file is a socket file and it is owned by the user of the application. This is safe only if the directory of path is not changed by a malicious user. So don’t use /tmp/malicious-users-directory/socket. Note that /tmp/socket and /tmp/your-private-directory/socket is safe assuming that /tmp has sticky bit.
# Sequential echo server. # It services only one client at a time. Socket.unix_server_loop("/tmp/sock") {|sock, client_addrinfo| begin IO.copy_stream(sock, sock) ensure sock.close end }
Generates new parameters for the algorithm. algo_name is a String
that represents the algorithm. The optional argument options is a Hash
that specifies the options specific to the algorithm. The order of the options can be important.
A block can be passed optionally. The meaning of the arguments passed to the block varies depending on the implementation of the algorithm. The block may be called once or multiple times, or may not even be called.
For the supported options, see the documentation for the ‘openssl genpkey’ utility command.
pkey = OpenSSL::PKey.generate_parameters("DSA", "dsa_paramgen_bits" => 2048) p pkey.p.num_bits #=> 2048
Run UDP/IP server loop on the given sockets.
The return value of Socket.udp_server_sockets
is appropriate for the argument.
It calls the block for each message received.
Returns true if the set is a proper superset of the given set.
Returns the current execution stack—an array containing backtrace location objects.
See Thread::Backtrace::Location
for more information.
The optional start parameter determines the number of initial stack entries to omit from the top of the stack.
A second optional length
parameter can be used to limit how many entries are returned from the stack.
Returns nil
if start is greater than the size of current execution stack.
Optionally you can pass a range, which will return an array containing the entries within the specified range.
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
.
A Gem::Version
for the currently running RubyGems
Logs src
Returns a clock time as determined by POSIX function clock_gettime():
Process.clock_gettime(:CLOCK_PROCESS_CPUTIME_ID) # => 198.650379677
Argument clock_id
should be a symbol or a constant that specifies the clock whose time is to be returned; see below.
Optional argument unit
should be a symbol that specifies the unit to be used in the returned clock time; see below.
Argument clock_id
Argument clock_id
specifies the clock whose time is to be returned; it may be a constant such as Process::CLOCK_REALTIME
, or a symbol shorthand such as :CLOCK_REALTIME
.
The supported clocks depend on the underlying operating system; this method supports the following clocks on the indicated platforms (raises Errno::EINVAL if called with an unsupported clock):
:CLOCK_BOOTTIME
: Linux 2.6.39.
:CLOCK_BOOTTIME_ALARM
: Linux 3.0.
:CLOCK_MONOTONIC
: SUSv3 to 4, Linux 2.5.63, FreeBSD 3.0, NetBSD 2.0, OpenBSD 3.4, macOS 10.12, Windows-2000.
:CLOCK_MONOTONIC_COARSE
: Linux 2.6.32.
:CLOCK_MONOTONIC_FAST
: FreeBSD 8.1.
:CLOCK_MONOTONIC_PRECISE
: FreeBSD 8.1.
:CLOCK_MONOTONIC_RAW
: Linux 2.6.28, macOS 10.12.
:CLOCK_MONOTONIC_RAW_APPROX
: macOS 10.12.
:CLOCK_PROCESS_CPUTIME_ID
: SUSv3 to 4, Linux 2.5.63, FreeBSD 9.3, OpenBSD 5.4, macOS 10.12.
:CLOCK_PROF
: FreeBSD 3.0, OpenBSD 2.1.
: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. Time.now
is recommended over +:CLOCK_REALTIME:.
:CLOCK_REALTIME_ALARM
: Linux 3.0.
:CLOCK_REALTIME_COARSE
: Linux 2.6.32.
:CLOCK_REALTIME_FAST
: FreeBSD 8.1.
:CLOCK_REALTIME_PRECISE
: FreeBSD 8.1.
:CLOCK_SECOND
: FreeBSD 8.1.
:CLOCK_TAI
: Linux 3.10.
:CLOCK_THREAD_CPUTIME_ID
: SUSv3 to 4, Linux 2.5.63, FreeBSD 7.1, OpenBSD 5.4, macOS 10.12.
:CLOCK_UPTIME
: FreeBSD 7.0, OpenBSD 5.5.
:CLOCK_UPTIME_FAST
: FreeBSD 8.1.
:CLOCK_UPTIME_PRECISE
: FreeBSD 8.1.
:CLOCK_UPTIME_RAW
: macOS 10.12.
:CLOCK_UPTIME_RAW_APPROX
: macOS 10.12.
:CLOCK_VIRTUAL
: FreeBSD 3.0, OpenBSD 2.1.
Note that SUS stands for Single Unix Specification. SUS contains POSIX and clock_gettime
is defined in the POSIX part. SUS defines :CLOCK_REALTIME
as mandatory but :CLOCK_MONOTONIC
, :CLOCK_PROCESS_CPUTIME_ID
, and :CLOCK_THREAD_CPUTIME_ID
are optional.
Certain emulations are used when the given clock_id
is not supported directly:
Emulations for :CLOCK_REALTIME
:
:GETTIMEOFDAY_BASED_CLOCK_REALTIME
: Use gettimeofday() defined by SUS (deprecated in SUSv4). The resolution is 1 microsecond.
:TIME_BASED_CLOCK_REALTIME
: Use time() defined by ISO C. The resolution is 1 second.
Emulations for :CLOCK_MONOTONIC
:
:MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC
: Use mach_absolute_time(), available on Darwin. The resolution is CPU dependent.
:TIMES_BASED_CLOCK_MONOTONIC
: Use the result value of times() defined by POSIX, thus:
Upon successful completion, 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_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
:
:GETRUSAGE_BASED_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.
:TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID
: 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.
:CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID
: 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
as 1000000; other systems may define it differently. If CLOCKS_PER_SEC
is 1000000 (as in SUS), the resolution is 1 microsecond. If CLOCKS_PER_SEC
is 1000000 and clock_t is a 32-bit integer type, it cannot represent over 72 minutes.
Argument unit
Optional argument unit
(default :float_second
) specifies the unit for the returned value.
:float_microsecond
: Number of microseconds as a float.
:float_millisecond
: Number of milliseconds as a float.
:float_second
: Number of seconds as a float.
:microsecond
: Number of microseconds as an integer.
:millisecond
: Number of milliseconds as an integer.
:nanosecond
: Number of nanoseconds as an integer.
::second
: Number of seconds as an integer.
Examples:
Process.clock_gettime(:CLOCK_PROCESS_CPUTIME_ID, :float_microsecond) # => 203605054.825 Process.clock_gettime(:CLOCK_PROCESS_CPUTIME_ID, :float_millisecond) # => 203643.696848 Process.clock_gettime(:CLOCK_PROCESS_CPUTIME_ID, :float_second) # => 203.762181929 Process.clock_gettime(:CLOCK_PROCESS_CPUTIME_ID, :microsecond) # => 204123212 Process.clock_gettime(:CLOCK_PROCESS_CPUTIME_ID, :millisecond) # => 204298 Process.clock_gettime(:CLOCK_PROCESS_CPUTIME_ID, :nanosecond) # => 204602286036 Process.clock_gettime(:CLOCK_PROCESS_CPUTIME_ID, :second) # => 204
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 (time zero) of the returned value is system-dependent, and may be, 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
; some systems count leap seconds and others don’t, so the result may vary across systems.
Returns a clock resolution as determined by POSIX function clock_getres():
Process.clock_getres(:CLOCK_REALTIME) # => 1.0e-09
See Process.clock_gettime
for the values of clock_id
and unit
.
Examples:
Process.clock_getres(:CLOCK_PROCESS_CPUTIME_ID, :float_microsecond) # => 0.001 Process.clock_getres(:CLOCK_PROCESS_CPUTIME_ID, :float_millisecond) # => 1.0e-06 Process.clock_getres(:CLOCK_PROCESS_CPUTIME_ID, :float_second) # => 1.0e-09 Process.clock_getres(:CLOCK_PROCESS_CPUTIME_ID, :microsecond) # => 0 Process.clock_getres(:CLOCK_PROCESS_CPUTIME_ID, :millisecond) # => 0 Process.clock_getres(:CLOCK_PROCESS_CPUTIME_ID, :nanosecond) # => 1 Process.clock_getres(:CLOCK_PROCESS_CPUTIME_ID, :second) # => 0
In addition to the values for unit
supported in Process.clock_gettime
, this method supports :hertz
, the integer number of clock ticks per second (which is the reciprocal of :float_second
):
Process.clock_getres(:TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID, :hertz) # => 100.0 Process.clock_getres(:TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID, :float_second) # => 0.01
Accuracy: Note that the returned resolution may be inaccurate on some platforms due to underlying bugs. Inaccurate resolutions have been reported for various clocks including :CLOCK_MONOTONIC
and :CLOCK_MONOTONIC_RAW
on Linux, macOS, BSD or AIX platforms, when using ARM processors, or when using virtualization.
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]