Returns true
if the named protected method is defined mod. If inherit is set, the lookup will also search mod’s ancestors. String
arguments are converted to symbols.
module A def method1() end end class B protected def method2() end end class C < B include A def method3() end end A.method_defined? :method1 #=> true C.protected_method_defined? "method1" #=> false C.protected_method_defined? "method2" #=> true C.protected_method_defined? "method2", true #=> true C.protected_method_defined? "method2", false #=> false C.method_defined? "method2" #=> true
Makes existing class methods private. Often used to hide the default constructor new
.
String
arguments are converted to symbols. An Array
of Symbols and/or Strings is also accepted.
class SimpleSingleton # Not thread safe private_class_method :new def SimpleSingleton.create(*args, &block) @me = new(*args, &block) if ! @me @me end end
for compatibility
Packs port and host as an AF_INET/AF_INET6 sockaddr string.
Socket.sockaddr_in(80, "127.0.0.1") #=> "\x02\x00\x00P\x7F\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00" Socket.sockaddr_in(80, "::1") #=> "\n\x00\x00P\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00"
Unpacks sockaddr into port and ip_address.
sockaddr should be a string or an addrinfo for AF_INET/AF_INET6.
sockaddr = Socket.sockaddr_in(80, "127.0.0.1") p sockaddr #=> "\x02\x00\x00P\x7F\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00" p Socket.unpack_sockaddr_in(sockaddr) #=> [80, "127.0.0.1"]
Returns local IP addresses as an array.
The array contains Addrinfo
objects.
pp Socket.ip_address_list #=> [#<Addrinfo: 127.0.0.1>, #<Addrinfo: 192.168.0.128>, #<Addrinfo: ::1>, ...]
Returns whether Happy Eyeballs Version 2 (RFC 8305), which is provided starting from Ruby
3.4 when using TCPSocket.new
and Socket.tcp
, is enabled or disabled.
If true, it is enabled for TCPSocket.new
and Socket.tcp
. (Note: Happy Eyeballs Version 2 is not provided when using TCPSocket.new
on Windows.)
If false, Happy Eyeballs Version 2 is disabled.
For details on Happy Eyeballs Version 2, see Socket.tcp_fast_fallback=
.
Enable or disable Happy Eyeballs Version 2 (RFC 8305) globally, which is provided starting from Ruby
3.4 when using TCPSocket.new
and Socket.tcp
.
When set to true, the feature is enabled for both ‘TCPSocket.new` and `Socket.tcp`. (Note: This feature is not available when using TCPSocket.new
on Windows.)
When set to false, the behavior reverts to that of Ruby
3.3 or earlier.
The default value is true if no value is explicitly set by calling this method. However, when the environment variable RUBY_TCP_NO_FAST_FALLBACK=1 is set, the default is false.
To control the setting on a per-method basis, use the fast_fallback keyword argument for each method.
Happy Eyeballs Version 2 (RFC 8305) is an algorithm designed to improve client socket connectivity.
It aims for more reliable and efficient connections by performing hostname resolution and connection attempts in parallel, instead of serially.
Starting from Ruby
3.4, this method operates as follows with this algorithm:
Start resolving both IPv6 and IPv4 addresses concurrently.
Start connecting to the one of the addresses that are obtained first.
If IPv4 addresses are obtained first, the method waits 50 ms for IPv6 name resolution to prioritize IPv6 connections.
After starting a connection attempt, wait 250 ms for the connection to be established.
If no connection is established within this time, a new connection is started every 250 ms
until a connection is established or there are no more candidate addresses.
(Although RFC 8305 strictly specifies sorting addresses,
this method only alternates between IPv6 / IPv4 addresses due to the performance concerns)
Once a connection is established, all remaining connection attempts are canceled.
Returns true for IPv6 multicast link-local scope address. It returns false otherwise.
Returns IO
instance tied to ARGF for writing if inplace mode is enabled.
Creates an option from the given parameters params
. See Parameters for New Options.
The block, if given, is the handler for the created option. When the option is encountered during command-line parsing, the block is called with the argument given for the option, if any. See Option Handlers.
Defines options which set in to options for keyword parameters of method.
Parameters for each keywords are given as elements of params.
Breaks the buffer into lines that are shorter than maxwidth
Returns the value of the local variable symbol
.
def foo a = 1 binding.local_variable_get(:a) #=> 1 binding.local_variable_get(:b) #=> NameError end
This method is the short version of the following code:
binding.eval("#{symbol}")
Set
local variable named symbol
as obj
.
def foo a = 1 bind = binding bind.local_variable_set(:a, 2) # set existing local variable `a' bind.local_variable_set(:b, 3) # create new local variable `b' # `b' exists only in binding p bind.local_variable_get(:a) #=> 2 p bind.local_variable_get(:b) #=> 3 p a #=> 2 p b #=> NameError end
This method behaves similarly to the following code:
binding.eval("#{symbol} = #{obj}")
if obj
can be dumped in Ruby
code.
If the correponding value is not set, yield a value with init_block and store the value in thread-safe manner. This method returns corresponding stored value.
(1..10).map{ Thread.new(it){|i| Ractor.store_if_absent(:s){ f(); i } #=> return stored value of key :s } }.map(&:value).uniq.size #=> 1 and f() is called only once
Returns the value of a thread local variable that has been set. Note that these are different than fiber local values. For fiber local values, please see Thread#[]
and Thread#[]=
.
Thread
local values are carried along with threads, and do not respect fibers. For example:
Thread.new { Thread.current.thread_variable_set("foo", "bar") # set a thread local Thread.current["foo"] = "bar" # set a fiber local Fiber.new { Fiber.yield [ Thread.current.thread_variable_get("foo"), # get the thread local Thread.current["foo"], # get the fiber local ] }.resume }.join.value # => ['bar', nil]
The value “bar” is returned for the thread local, where nil is returned for the fiber local. The fiber is executed in the same thread, so the thread local values are available.
Sets a thread local with key
to value
. Note that these are local to threads, and not to fibers. Please see Thread#thread_variable_get
and Thread#[]
for more information.
Establishes proc on thr as the handler for tracing, or disables tracing if the parameter is nil
.
Adds proc as a handler for tracing.