Creates a pair of sockets connected to each other.
type should be a socket type such as: :STREAM, :DGRAM, :RAW, etc.
protocol should be a protocol defined in the domain. 0 is default protocol for the domain.
s1, s2 = UNIXSocket.pair s1.send "a", 0 s1.send "b", 0 p s2.recv(10) #=> "ab"
Finds and returns the object in nested objects that is specified by key
and identifiers
. The nested objects may be instances of various classes. See Dig Methods.
Nested Hashes:
h = {foo: {bar: {baz: 2}}} h.dig(:foo) # => {:bar=>{:baz=>2}} h.dig(:foo, :bar) # => {:baz=>2} h.dig(:foo, :bar, :baz) # => 2 h.dig(:foo, :bar, :BAZ) # => nil
Nested Hashes and Arrays:
h = {foo: {bar: [:a, :b, :c]}} h.dig(:foo, :bar, 2) # => :c
This method will use the default values for keys that are not present:
h = {foo: {bar: [:a, :b, :c]}} h.dig(:hello) # => nil h.default_proc = -> (hash, _key) { hash } h.dig(:hello, :world) # => h h.dig(:hello, :world, :foo, :bar, 2) # => :c
Opens an IRB
session where binding.irb
is called which allows for interactive debugging. You can call any methods or variables available in the current scope, and mutate state if you need to.
Given a Ruby file called potato.rb
containing the following code:
class Potato def initialize @cooked = false binding.irb puts "Cooked potato: #{@cooked}" end end Potato.new
Running ruby potato.rb
will open an IRB
session where binding.irb
is called, and you will see the following:
$ ruby potato.rb From: potato.rb @ line 4 : 1: class Potato 2: def initialize 3: @cooked = false => 4: binding.irb 5: puts "Cooked potato: #{@cooked}" 6: end 7: end 8: 9: Potato.new irb(#<Potato:0x00007feea1916670>):001:0>
You can type any valid Ruby code and it will be evaluated in the current context. This allows you to debug without having to run your code repeatedly:
irb(#<Potato:0x00007feea1916670>):001:0> @cooked => false irb(#<Potato:0x00007feea1916670>):002:0> self.class => Potato irb(#<Potato:0x00007feea1916670>):003:0> caller.first => ".../2.5.1/lib/ruby/2.5.0/irb/workspace.rb:85:in `eval'" irb(#<Potato:0x00007feea1916670>):004:0> @cooked = true => true
You can exit the IRB
session with the exit
command. Note that exiting will resume execution where binding.irb
had paused it, as you can see from the output printed to standard output in this example:
irb(#<Potato:0x00007feea1916670>):005:0> exit Cooked potato: true
See IRB
for more information.
Return candidates for word
.
Parses environment variable env
or its uppercase with splitting like a shell.
env
defaults to the basename of the program.
Returns the binding associated with prc.
def fred(param) proc {} end b = fred(99) eval("param", b.binding) #=> 99
Deactivates the trace
Return true if trace was enabled. Return false if trace was disabled.
trace.enabled? #=> true trace.disable #=> true (previous status) trace.enabled? #=> false trace.disable #=> false
If a block is given, the trace will only be disable within the scope of the block.
trace.enabled? #=> true trace.disable do trace.enabled? # only disabled for this block end trace.enabled? #=> true
Note: You cannot access event hooks within the block.
trace.disable { p tp.lineno } #=> RuntimeError: access from outside
Return the generated binding object from event.
Note that for :c_call
and :c_return
events, the method will return nil
, since C methods themselves do not have bindings.
When RubyGems is required, Kernel#require
is replaced with our own which is capable of loading gems on demand.
When you call require 'x'
, this is what happens:
If the file can be loaded from the existing Ruby loadpath, it is.
Otherwise, installed gems are searched for a file that matches. If it’s found in gem ‘y’, that gem is activated (added to the loadpath).
The normal require
functionality of returning false if that file has already been loaded is preserved.
Returns a Binding
object, describing the variable and method bindings at the point of call. This object can be used when calling Binding#eval
to execute the evaluated command in this environment, or extracting its local variables.
class User def initialize(name, position) @name = name @position = position end def get_binding binding end end user = User.new('Joan', 'manager') template = '{name: @name, position: @position}' # evaluate template in context of the object eval(template, user.get_binding) #=> {:name=>"Joan", :position=>"manager"}
Binding#local_variable_get
can be used to access the variables whose names are reserved Ruby keywords:
# This is valid parameter declaration, but `if` parameter can't # be accessed by name, because it is a reserved word. def validate(field, validation, if: nil) condition = binding.local_variable_get('if') return unless condition # ...Some implementation ... end validate(:name, :empty?, if: false) # skips validation validate(:name, :empty?, if: true) # performs validation
Returns the first element or elements.
With no argument, returns the first element, or nil
if there is none:
(1..4).first # => 1 %w[a b c].first # => "a" {foo: 1, bar: 1, baz: 2}.first # => [:foo, 1] [].first # => nil
With integer argument n
, returns an array containing the first n
elements that exist:
(1..4).first(2) # => [1, 2] %w[a b c d].first(3) # => ["a", "b", "c"] %w[a b c d].first(50) # => ["a", "b", "c", "d"] {foo: 1, bar: 1, baz: 2}.first(2) # => [[:foo, 1], [:bar, 1]] [].first(2) # => []
Returns a Digest
subclass by name
require 'openssl' OpenSSL::Digest("MD5") # => OpenSSL::Digest::MD5 Digest("Foo") # => NameError: wrong constant name Foo
Returns a Digest
subclass by name
require 'openssl' OpenSSL::Digest("MD5") # => OpenSSL::Digest::MD5 Digest("Foo") # => NameError: wrong constant name Foo
Disables garbage collection, returning true
if garbage collection was already disabled.
GC.disable #=> false GC.disable #=> true
Returns time stamp of the target
file if it exists and is newer than or equal to all of times
.
Returns a string usable in Dir.glob
to match all requirable paths for this spec.
Creates a new directory in the tar file name
with mode
Calls the given block once for each key
, value
pair in the database.
Returns self
.
Iterates over keys and values. Note that unlike other collections, each
without block isn’t supported.
@api private
Calls the given block with each successive character from self
; returns self
:
'hello'.each_char {|char| print char, ' ' } print "\n" 'тест'.each_char {|char| print char, ' ' } print "\n" 'こんにちは'.each_char {|char| print char, ' ' } print "\n"
Output:
h e l l o т е с т こ ん に ち は
Returns an enumerator if no block is given.
Changes the encoding of self
to encoding
, which may be a string encoding name or an Encoding
object; returns self:
s = 'łał' s.bytes # => [197, 130, 97, 197, 130] s.encoding # => #<Encoding:UTF-8> s.force_encoding('ascii') # => "\xC5\x82a\xC5\x82" s.encoding # => #<Encoding:US-ASCII>
Does not change the underlying bytes:
s.bytes # => [197, 130, 97, 197, 130]
Makes the change even if the given encoding
is invalid for self
(as is the change above):
s.valid_encoding? # => false s.force_encoding(Encoding::UTF_8) # => "łał" s.valid_encoding? # => true
Returns true
if self
is encoded correctly, false
otherwise:
"\xc2\xa1".force_encoding("UTF-8").valid_encoding? # => true "\xc2".force_encoding("UTF-8").valid_encoding? # => false "\x80".force_encoding("UTF-8").valid_encoding? # => false