Returns a string representation of lex_state.
creates TCP/IP server sockets for host and port. host is optional.
If no block given, it returns an array of listening sockets.
If a block is given, the block is called with the sockets. The value of the block is returned. The socket is closed when this method returns.
If port is 0, actual port number is chosen dynamically. However all sockets in the result has same port number.
# tcp_server_sockets returns two sockets. sockets = Socket.tcp_server_sockets(1296) p sockets #=> [#<Socket:fd 3>, #<Socket:fd 4>] # The sockets contains IPv6 and IPv4 sockets. sockets.each {|s| p s.local_address } #=> #<Addrinfo: [::]:1296 TCP> # #<Addrinfo: 0.0.0.0:1296 TCP> # IPv6 and IPv4 socket has same port number, 53114, even if it is chosen dynamically. sockets = Socket.tcp_server_sockets(0) sockets.each {|s| p s.local_address } #=> #<Addrinfo: [::]:53114 TCP> # #<Addrinfo: 0.0.0.0:53114 TCP> # The block is called with the sockets. Socket.tcp_server_sockets(0) {|sockets| p sockets #=> [#<Socket:fd 3>, #<Socket:fd 4>] }
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 } }
Returns the limit for field size; used for parsing; see {Option max_field_size
}:
CSV.new('').max_field_size # => nil
Since 3.2.3.
Return the native thread ID which is used by the Ruby thread.
The ID depends on the OS. (not POSIX thread ID returned by pthread_self(3))
On Linux it is TID returned by gettid(2).
On macOS it is the system-wide unique integral ID of thread returned by pthread_threadid_np(3).
On FreeBSD it is the unique integral ID of the thread returned by pthread_getthreadid_np(3).
On Windows it is the thread identifier returned by GetThreadId().
On other platforms, it raises NotImplementedError
.
NOTE: If the thread is not associated yet or already deassociated with a native thread, it returns nil. If the Ruby implementation uses M:N thread model, the ID may change depending on the timing.
With a block given, calls the block with each element and its index; returns self
:
h = {} (1..4).each_with_index {|element, i| h[element] = i } # => 1..4 h # => {1=>0, 2=>1, 3=>2, 4=>3} h = {} %w[a b c d].each_with_index {|element, i| h[element] = i } # => ["a", "b", "c", "d"] h # => {"a"=>0, "b"=>1, "c"=>2, "d"=>3} a = [] h = {foo: 0, bar: 1, baz: 2} h.each_with_index {|element, i| a.push([i, element]) } # => {:foo=>0, :bar=>1, :baz=>2} a # => [[0, [:foo, 0]], [1, [:bar, 1]], [2, [:baz, 2]]]
With no block given, returns an Enumerator
.
Calls the block once for each element, passing both the element and the given object:
(1..4).each_with_object([]) {|i, a| a.push(i**2) } # => [1, 4, 9, 16] h.each_with_object({}) {|element, h| k, v = *element; h[v] = k } # => {0=>:foo, 1=>:bar, 2=>:baz}
With no block given, returns an Enumerator
.
Starts tracing object allocations from the ObjectSpace
extension module.
For example:
require 'objspace' class C include ObjectSpace def foo trace_object_allocations do obj = Object.new p "#{allocation_sourcefile(obj)}:#{allocation_sourceline(obj)}" end end end C.new.foo #=> "objtrace.rb:8"
This example has included the ObjectSpace
module to make it easier to read, but you can also use the ::trace_object_allocations
notation (recommended).
Note that this feature introduces a huge performance decrease and huge memory consumption.
Returns the method identifier for the given object
.
class A include ObjectSpace def foo trace_object_allocations do obj = Object.new p "#{allocation_class_path(obj)}##{allocation_method_id(obj)}" end end end A.new.foo #=> "Class#new"
See ::trace_object_allocations
for more information and examples.
Counts objects for each T_DATA
type.
This method is only for MRI developers interested in performance and memory usage of Ruby programs.
It returns a hash as:
{RubyVM::InstructionSequence=>504, :parser=>5, :barrier=>6, :mutex=>6, Proc=>60, RubyVM::Env=>57, Mutex=>1, Encoding=>99, ThreadGroup=>1, Binding=>1, Thread=>1, RubyVM=>1, :iseq=>1, Random=>1, ARGF.class=>1, Data=>1, :autoload=>3, Time=>2} # T_DATA objects existing at startup on r32276.
If the optional argument, result_hash, is given, it is overwritten and returned. This is intended to avoid probe effect.
The contents of the returned hash is implementation specific and may change in the future.
In this version, keys are Class
object or Symbol
object.
If object is kind of normal (accessible) object, the key is Class
object. If object is not a kind of normal (internal) object, the key is symbol name, registered by rb_data_type_struct.
This method is only expected to work with C Ruby.
Return all reachable objects from ‘obj’.
This method returns all reachable objects from ‘obj’.
If ‘obj’ has two or more references to the same object ‘x’, then returned array only includes one ‘x’ object.
If ‘obj’ is a non-markable (non-heap management) object such as true, false, nil, symbols and Fixnums (and Flonum) then it simply returns nil.
If ‘obj’ has references to an internal object, then it returns instances of ObjectSpace::InternalObjectWrapper
class. This object contains a reference to an internal object and you can check the type of internal object with ‘type’ method.
If ‘obj’ is instance of ObjectSpace::InternalObjectWrapper
class, then this method returns all reachable object from an internal object, which is pointed by ‘obj’.
With this method, you can find memory leaks.
This method is only expected to work except with C Ruby.
Example:
ObjectSpace.reachable_objects_from(['a', 'b', 'c']) #=> [Array, 'a', 'b', 'c'] ObjectSpace.reachable_objects_from(['a', 'a', 'a']) #=> [Array, 'a', 'a', 'a'] # all 'a' strings have different object id ObjectSpace.reachable_objects_from([v = 'a', v, v]) #=> [Array, 'a'] ObjectSpace.reachable_objects_from(1) #=> nil # 1 is not markable (heap managed) object
Calls CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON). Starts tracking memory allocations. See also OpenSSL.print_mem_leaks
.
This is available only when built with a capable OpenSSL
and –enable-debug configure option.
Specifies Emacs editing mode. The default is this mode. See the manual of GNU Readline
for details of Emacs editing mode.
Raises NotImplementedError
if the using readline library does not support.
Returns true if emacs mode is active. Returns false if not.
Raises NotImplementedError
if the using readline library does not support.
Specifies a character to be appended on completion. Nothing will be appended if an empty string (“”) or nil is specified.
For example:
require "readline" Readline.readline("> ", true) Readline.completion_append_character = " "
Result:
> Input "/var/li". > /var/li Press TAB key. > /var/lib Completes "b" and appends " ". So, you can continuously input "/usr". > /var/lib /usr
NOTE: Only one character can be specified. When “string” is specified, sets only “s” that is the first.
require "readline" Readline.completion_append_character = "string" p Readline.completion_append_character # => "s"
Raises NotImplementedError
if the using readline library does not support.
Returns a string containing a character to be appended on completion. The default is a space (“ ”).
Raises NotImplementedError
if the using readline library does not support.
When called during a completion (e.g. from within your completion_proc
), it will return a string containing the character used to quote the argument being completed, or nil if the argument is unquoted.
When called at other times, it will always return nil.
Note that Readline.completer_quote_characters
must be set, or this method will always return nil.
Sets a list of quote characters which can cause a word break.
Raises NotImplementedError
if the using readline library does not support.
Gets a list of quote characters which can cause a word break.
Raises NotImplementedError
if the using readline library does not support.
Sets a list of characters which can be used to quote a substring of the line. Completion occurs on the entire substring, and within the substring Readline.completer_word_break_characters
are treated as any other character, unless they also appear within this list.
Raises NotImplementedError
if the using readline library does not support.
Gets a list of characters which can be used to quote a substring of the line.
Raises NotImplementedError
if the using readline library does not support.
Sets a list of characters that cause a filename to be quoted by the completer when they appear in a completed filename. The default is nil.
Raises NotImplementedError
if the using readline library does not support.