Results for: "String#[]"

__ENCODING__ ^^^^^^^^^^^^

Example:

x.foo += 42
 ^^^     (for foo)
x.foo += 42
      ^  (for +)
x.foo += 42
 ^^^^^^^ (for foo=)

Example:

x.foo += 42
         ^^

Same as Enumerator#with_index(0), i.e. there is no starting offset.

If no block is given, a new Enumerator is returned that includes the index.

Returns the Ruby source filename and line number containing the definition of the constant specified. If the named constant is not found, nil is returned. If the constant is found, but its source location can not be extracted (constant is defined in C code), empty array is returned.

inherit specifies whether to lookup in mod.ancestors (true by default).

# test.rb:
class A         # line 1
  C1 = 1
  C2 = 2
end

module M        # line 6
  C3 = 3
end

class B < A     # line 10
  include M
  C4 = 4
end

class A # continuation of A definition
  C2 = 8 # constant redefinition; warned yet allowed
end

p B.const_source_location('C4')           # => ["test.rb", 12]
p B.const_source_location('C3')           # => ["test.rb", 7]
p B.const_source_location('C1')           # => ["test.rb", 2]

p B.const_source_location('C3', false)    # => nil  -- don't lookup in ancestors

p A.const_source_location('C2')           # => ["test.rb", 16] -- actual (last) definition place

p Object.const_source_location('B')       # => ["test.rb", 10] -- top-level constant could be looked through Object
p Object.const_source_location('A')       # => ["test.rb", 1] -- class reopening is NOT considered new definition

p B.const_source_location('A')            # => ["test.rb", 1]  -- because Object is in ancestors
p M.const_source_location('A')            # => ["test.rb", 1]  -- Object is not ancestor, but additionally checked for modules

p Object.const_source_location('A::C1')   # => ["test.rb", 2]  -- nesting is supported
p Object.const_source_location('String')  # => []  -- constant is defined in C code

Removes the named class variable from the receiver, returning that variable’s value.

class Example
  @@var = 99
  puts remove_class_variable(:@@var)
  p(defined? @@var)
end

produces:

99
nil

Returns the value of the given class variable (or throws a NameError exception). The @@ part of the variable name should be included for regular class variables. String arguments are converted to symbols.

class Fred
  @@foo = 99
end
Fred.class_variable_get(:@@foo)     #=> 99

Sets the class variable named by symbol to the given object. If the class variable name is passed as a string, that string is converted to a symbol.

class Fred
  @@foo = 99
  def foo
    @@foo
  end
end
Fred.class_variable_set(:@@foo, 101)     #=> 101
Fred.new.foo                             #=> 101

Returns true if the named public method is defined by 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.public_method_defined? "method1"         #=> true
C.public_method_defined? "method1", true   #=> true
C.public_method_defined? "method1", false  #=> true
C.public_method_defined? "method2"         #=> false
C.method_defined? "method2"                #=> true

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

No documentation available

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

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:

  1. Start resolving both IPv6 and IPv4 addresses concurrently.

  2. 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.

  3. 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)



  4. Once a connection is established, all remaining connection attempts are canceled.

Returns true for IPv6 multicast link-local scope address. It returns false otherwise.

No documentation available
No documentation available
No documentation available
No documentation available
No documentation available

Returns IO instance tied to ARGF for writing if inplace mode is enabled.

Search took: 5ms  ·  Total Results: 2715