Returns the remote address of the socket as a sockaddr string.
TCPServer.open("127.0.0.1", 1440) {|serv| c = TCPSocket.new("127.0.0.1", 1440) s = serv.accept p s.getpeername #=> "\x02\x00\x82u\x7F\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00" }
If Addrinfo
object is preferred over the binary string, use BasicSocket#remote_address
.
Returns the user and group on the peer of the UNIX socket. The result is a two element array which contains the effective uid and the effective gid.
Socket.unix_server_loop("/tmp/sock") {|s| begin euid, egid = s.getpeereid # Check the connected client is myself or not. next if euid != Process.uid # do something about my resource. ensure s.close end }
Runs the early binding method to get property. The 1st argument specifies dispatch ID, the 2nd argument specifies the array of arguments, the 3rd argument specifies the array of the type of arguments.
excel = WIN32OLE.new('Excel.Application') puts excel._getproperty(558, [], []) # same effect as puts excel.visible
Adds the contents of other_hash to hsh. If no block is specified, entries with duplicate keys are overwritten with the values from other_hash, otherwise the value of each duplicate key is determined by calling the block with the key, its value in hsh and its value in other_hash.
h1 = { "a" => 100, "b" => 200 } h2 = { "b" => 254, "c" => 300 } h1.merge!(h2) #=> {"a"=>100, "b"=>254, "c"=>300} h1 = { "a" => 100, "b" => 200 } h2 = { "b" => 254, "c" => 300 } h1.merge!(h2) { |key, v1, v2| v1 } #=> {"a"=>100, "b"=>200, "c"=>300}
Returns a new hash containing the contents of other_hash and the contents of hsh. If no block is specified, the value for entries with duplicate keys will be that of other_hash. Otherwise the value for each duplicate key is determined by calling the block with the key, its value in hsh and its value in other_hash.
h1 = { "a" => 100, "b" => 200 } h2 = { "b" => 254, "c" => 300 } h1.merge(h2) #=> {"a"=>100, "b"=>254, "c"=>300} h1.merge(h2){|key, oldval, newval| newval - oldval} #=> {"a"=>100, "b"=>54, "c"=>300} h1 #=> {"a"=>100, "b"=>200}
This method wraps a String you provide, or an empty default String, in a CSV
object which is passed to the provided block. You can use the block to append CSV
rows to the String and when the block exits, the final String will be returned.
Note that a passed String is modified by this method. Call dup() before passing if you need a new String.
The options
parameter can be anything CSV::new()
understands. This method understands an additional :encoding
parameter when not passed a String to set the base Encoding
for the output. CSV
needs this hint if you plan to output non-ASCII compatible data.
Returns the current list of converters in effect. See CSV::new
for details. Built-in converters will be returned by name, while others will be returned as is.
Since int
is already an Integer
, this always returns true
.
Merges the elements of the given enumerable object to the set and returns self.
Converts arg to a Fixnum
or Bignum
. Numeric
types are converted directly (with floating point numbers being truncated). base (0, or between 2 and 36) is a base for integer string representation. If arg is a String
, when base is omitted or equals zero, radix indicators (0
, 0b
, and 0x
) are honored. In any case, strings should be strictly conformed to numeric representation. This behavior is different from that of String#to_i
. Non string values will be converted by first trying to_int
, then to_i
. Passing nil
raises a TypeError
.
Integer(123.999) #=> 123 Integer("0x1a") #=> 26 Integer(Time.new) #=> 1204973019 Integer("0930", 10) #=> 930 Integer("111", 2) #=> 7 Integer(nil) #=> TypeError
Computes the natural logarithm of decimal
to the specified number of digits of precision, numeric
.
If decimal
is zero or negative, raises Math::DomainError
.
If decimal
is positive infinity, returns Infinity.
If decimal
is NaN, returns NaN.
Generate a JSON
document from the Ruby data structure obj and return it. state is * a JSON::State object,
or a Hash
like object (responding to to_hash),
an object convertible into a hash by a to_h method,
that is used as or to configure a State object.
It defaults to a state object, that creates the shortest possible JSON
text in one line, checks for circular data structures and doesn’t allow NaN
, Infinity
, and -Infinity.
A state hash can have the following keys:
indent: a string used to indent levels (default: ”),
space: a string that is put after, a : or , delimiter (default: ”),
space_before: a string that is put before a : pair delimiter (default: ”),
object_nl: a string that is put at the end of a JSON
object (default: ”),
array_nl: a string that is put at the end of a JSON
array (default: ”),
allow_nan: true if NaN
, Infinity
, and -Infinity should be generated, otherwise an exception is thrown if these values are encountered. This options defaults to false.
max_nesting: The maximum depth of nesting allowed in the data structures from which JSON
is to be generated. Disable depth checking with :max_nesting => false, it defaults to 100.
See also the fast_generate
for the fastest creation method with the least amount of sanity checks, and the pretty_generate
method for some defaults for pretty output.
Log a message with the specified priority. Example:
Syslog.log(Syslog::LOG_CRIT, "Out of disk space") Syslog.log(Syslog::LOG_CRIT, "User %s logged in", ENV['USER'])
The priority levels, in descending order, are:
System is unusable
Action needs to be taken immediately
A critical condition has occurred
An error occurred
Warning of a possible problem
A normal but significant condition occurred
Informational message
Debugging information
Each priority level also has a shortcut method that logs with it’s named priority. As an example, the two following statements would produce the same result:
Syslog.log(Syslog::LOG_ALERT, "Out of memory") Syslog.alert("Out of memory")
Format strings are as for printf/sprintf, except that in addition %m is replaced with the error message string that would be returned by strerror(errno).
Returns the natural logarithm of Complex
. If a second argument is given, it will be the base of logarithm.
CMath.log(1 + 4i) #=> (1.416606672028108+1.3258176636680326i) CMath.log(1 + 4i, 10) #=> (0.6152244606891369+0.5757952953408879i)
Returns the natural logarithm of Complex
. If a second argument is given, it will be the base of logarithm.
CMath.log(1 + 4i) #=> (1.416606672028108+1.3258176636680326i) CMath.log(1 + 4i, 10) #=> (0.6152244606891369+0.5757952953408879i)
Returns the logarithm of x
. If additional second argument is given, it will be the base of logarithm. Otherwise it is e
(for the natural logarithm).
Domain: (0, INFINITY)
Codomain: (-INFINITY, INFINITY)
Math.log(0) #=> -Infinity Math.log(1) #=> 0.0 Math.log(Math::E) #=> 1.0 Math.log(Math::E**3) #=> 3.0 Math.log(12, 3) #=> 2.2618595071429146
Returns the base 2 logarithm of x
.
Domain: (0, INFINITY)
Codomain: (-INFINITY, INFINITY)
Math.log2(1) #=> 0.0 Math.log2(2) #=> 1.0 Math.log2(32768) #=> 15.0 Math.log2(65536) #=> 16.0
Returns the base 10 logarithm of x
.
Domain: (0, INFINITY)
Codomain: (-INFINITY, INFINITY)
Math.log10(1) #=> 0.0 Math.log10(10) #=> 1.0 Math.log10(10**100) #=> 100.0
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 }