SystemCallError
is the base class for all low-level platform-dependent errors.
The errors available on the current platform are subclasses of SystemCallError
and are defined in the Errno
module.
File.open("does/not/exist")
raises the exception:
Errno::ENOENT: No such file or directory - does/not/exist
A Symbol
object represents a named identifier inside the Ruby
interpreter.
You can create a Symbol
object explicitly with:
A symbol literal.
The same Symbol
object will be created for a given name or string for the duration of a program’s execution, regardless of the context or meaning of that name. Thus if Fred
is a constant in one context, a method in another, and a class in a third, the Symbol
:Fred
will be the same object in all three contexts.
module One class Fred end $f1 = :Fred end module Two Fred = 1 $f2 = :Fred end def Fred() end $f3 = :Fred $f1.object_id #=> 2514190 $f2.object_id #=> 2514190 $f3.object_id #=> 2514190
Constant, method, and variable names are returned as symbols:
module One Two = 2 def three; 3 end @four = 4 @@five = 5 $six = 6 end seven = 7 One.constants # => [:Two] One.instance_methods(true) # => [:three] One.instance_variables # => [:@four] One.class_variables # => [:@@five] global_variables.grep(/six/) # => [:$six] local_variables # => [:seven]
A Symbol
object differs from a String
object in that a Symbol
object represents an identifier, while a String
object represents text or data.
First, what’s elsewhere. Class
Symbol
:
Inherits from class Object.
Includes module Comparable.
Here, class Symbol
provides methods that are useful for:
::all_symbols
: Returns an array of the symbols currently in Ruby’s symbol table.
=~
: Returns the index of the first substring in symbol that matches a given Regexp
or other object; returns nil
if no match is found.
[]
, slice
: Returns a substring of symbol determined by a given index, start/length, or range, or string.
empty?
: Returns true
if self.length
is zero; false
otherwise.
encoding
: Returns the Encoding
object that represents the encoding of symbol.
end_with?
: Returns true
if symbol ends with any of the given strings.
match
: Returns a MatchData
object if symbol matches a given Regexp
; nil
otherwise.
match?
: Returns true
if symbol matches a given Regexp
; false
otherwise.
start_with?
: Returns true
if symbol starts with any of the given strings.
<=>
: Returns -1, 0, or 1 as a given symbol is smaller than, equal to, or larger than symbol.
==
, ===
: Returns true
if a given symbol has the same content and encoding.
casecmp
: Ignoring case, returns -1, 0, or 1 as a given symbol is smaller than, equal to, or larger than symbol.
casecmp?
: Returns true
if symbol is equal to a given symbol after Unicode case folding; false
otherwise.
capitalize
: Returns symbol with the first character upcased and all other characters downcased.
downcase
: Returns symbol with all characters downcased.
inspect
: Returns the string representation of self
as a symbol literal.
name
: Returns the frozen string corresponding to symbol.
succ
, next
: Returns the symbol that is the successor to symbol.
swapcase
: Returns symbol with all upcase characters downcased and all downcase characters upcased.
to_proc
: Returns a Proc
object which responds to the method named by symbol.
upcase
: Returns symbol with all characters upcased.
TCPServer
represents a TCP/IP server socket.
A simple TCP server may look like:
require 'socket' server = TCPServer.new 2000 # Server bind to port 2000 loop do client = server.accept # Wait for a client to connect client.puts "Hello !" client.puts "Time is #{Time.now}" client.close end
A more usable server (serving multiple clients):
require 'socket' server = TCPServer.new 2000 loop do Thread.start(server.accept) do |client| client.puts "Hello !" client.puts "Time is #{Time.now}" client.close end end
TCPSocket
represents a TCP/IP client socket.
A simple client may look like:
require 'socket' s = TCPSocket.new 'localhost', 2000 while line = s.gets # Read lines from socket puts line # and print them end s.close # close socket when done
MatchData
encapsulates the result of matching a Regexp
against string. It is returned by Regexp#match
and String#match
, and also stored in a global variable returned by Regexp.last_match
.
Usage:
url = 'https://docs.ruby-lang.org/en/2.5.0/MatchData.html' m = url.match(/(\d\.?)+/) # => #<MatchData "2.5.0" 1:"0"> m.string # => "https://docs.ruby-lang.org/en/2.5.0/MatchData.html" m.regexp # => /(\d\.?)+/ # entire matched substring: m[0] # => "2.5.0" # Working with unnamed captures m = url.match(%r{([^/]+)/([^/]+)\.html$}) m.captures # => ["2.5.0", "MatchData"] m[1] # => "2.5.0" m.values_at(1, 2) # => ["2.5.0", "MatchData"] # Working with named captures m = url.match(%r{(?<version>[^/]+)/(?<module>[^/]+)\.html$}) m.captures # => ["2.5.0", "MatchData"] m.named_captures # => {"version"=>"2.5.0", "module"=>"MatchData"} m[:version] # => "2.5.0" m.values_at(:version, :module) # => ["2.5.0", "MatchData"] # Numerical indexes are working, too m[1] # => "2.5.0" m.values_at(1, 2) # => ["2.5.0", "MatchData"]
Parts of last MatchData
(returned by Regexp.last_match
) are also aliased as global variables:
$~
is Regexp.last_match
;
$&
is Regexp.last_match
[ 0 ]
;
$1
, $2
, and so on are Regexp.last_match
[ i ]
(captures by number);
$`
is Regexp.last_match
.pre_match
;
$'
is Regexp.last_match
.post_match
;
$+
is Regexp.last_match
[ -1 ]
(the last capture).
See also Global Variables at Regexp
.
Raised in case of a stack overflow.
def me_myself_and_i me_myself_and_i end me_myself_and_i
raises the exception:
SystemStackError: stack level too deep
Load multiple documents given in yaml
. Returns the parsed documents as a list.
Example:
Psych.safe_load_stream("--- foo\n...\n--- bar\n...") # => ['foo', 'bar'] list = [] Psych.safe_load_stream("--- foo\n...\n--- bar\n...") do |ruby| list << ruby end list # => ['foo', 'bar']
Load the document contained in filename
. Returns the yaml contained in filename
as a Ruby
object, or if the file is empty, it returns the specified fallback
return value, which defaults to false
.
NOTE: This method *should not* be used to parse untrusted documents, such as YAML
documents that are supplied via user input. Instead, please use the safe_load_file
method.
Safely loads the document contained in filename
. Returns the yaml contained in filename
as a Ruby
object, or if the file is empty, it returns the specified fallback
return value, which defaults to nil
. See safe_load
for options.
Enumerator::Chain
is a subclass of Enumerator
, which represents a chain of enumerables that works as a single enumerator.
This type of objects can be created by Enumerable#chain
and Enumerator#+
.
Thrown when PTY::check
is called for a pid that represents a process that has exited.
UDP/IP address information used by Socket.udp_server_loop
.