Returns a Ruby lighter-weight code representation of this specification, used for indexing only.
See to_ruby
.
Raised when an invalid operation is attempted on a Fiber
, in particular when attempting to call/resume a dead fiber, attempting to yield from the root fiber, or calling a fiber across threads.
fiber = Fiber.new{} fiber.resume #=> nil fiber.resume #=> FiberError: dead fiber called
The most standard error types are subclasses of StandardError
. A rescue clause without an explicit Exception
class will rescue all StandardErrors (and only those).
def foo raise "Oups" end foo rescue "Hello" #=> "Hello"
On the other hand:
require 'does/not/exist' rescue "Hi"
raises the exception:
LoadError: no such file to load -- does/not/exist
Raised when encountering an object that is not of the expected type.
[1, 2, 3].first("two")
raises the exception:
TypeError: no implicit conversion of String into Integer
Raised when the arguments are wrong and there isn’t a more specific Exception
class.
Ex: passing the wrong number of arguments
[1, 2, 3].first(4, 5)
raises the exception:
ArgumentError: wrong number of arguments (given 2, expected 1)
Ex: passing an argument that is not acceptable:
[1, 2, 3].first(-4)
raises the exception:
ArgumentError: negative array size
Raised when the given index is invalid.
a = [:foo, :bar] a.fetch(0) #=> :foo a[4] #=> nil a.fetch(4) #=> IndexError: index 4 outside of array bounds: -2...2
Raised when the specified key is not found. It is a subclass of IndexError
.
h = {"foo" => :bar} h.fetch("foo") #=> :bar h.fetch("baz") #=> KeyError: key not found: "baz"
Raised when a given numerical value is out of range.
[1, 2, 3].drop(1 << 100)
raises the exception:
RangeError: bignum too big to convert into `long'
ScriptError
is the superclass for errors raised when a script can not be executed because of a LoadError
, NotImplementedError
or a SyntaxError
. Note these type of ScriptErrors
are not StandardError
and will not be rescued unless it is specified explicitly (or its ancestor Exception
).
Raised when encountering Ruby code with an invalid syntax.
eval("1+1=2")
raises the exception:
SyntaxError: (eval):1: syntax error, unexpected '=', expecting $end
Raised when a file required (a Ruby script, extension library, …) fails to load.
require 'this/file/does/not/exist'
raises the exception:
LoadError: no such file to load -- this/file/does/not/exist
Raised when a feature is not implemented on the current platform. For example, methods depending on the fsync
or fork
system calls may raise this exception if the underlying operating system or Ruby runtime does not support them.
Note that if fork
raises a NotImplementedError
, then respond_to?(:fork)
returns false
.
Raised when a given name is invalid or undefined.
puts foo
raises the exception:
NameError: undefined local variable or method `foo' for main:Object
Since constant names must start with a capital:
Integer.const_set :answer, 42
raises the exception:
NameError: wrong constant name answer
Raised when a method is called on a receiver which doesn’t have it defined and also fails to respond with method_missing
.
"hello".to_ary
raises the exception:
NoMethodError: undefined method `to_ary' for "hello":String
A generic error class raised when an invalid operation is attempted.
[1, 2, 3].freeze << 4
raises the exception:
RuntimeError: can't modify frozen Array
Kernel.raise
will raise a RuntimeError
if no Exception
class is specified.
raise "ouch"
raises the exception:
RuntimeError: ouch
Raised when attempting a potential unsafe operation, typically when the $SAFE level is raised above 0.
foo = "bar" proc = Proc.new do $SAFE = 3 foo.untaint end proc.call
raises the exception:
SecurityError: Insecure: Insecure operation `untaint' at level 3
Raised when memory allocation fails.
EncodingError
is the base class for encoding errors.
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
Class
Socket
provides access to the underlying operating system socket implementations. It can be used to provide more operating system specific functionality than the protocol-specific socket classes.
The constants defined under Socket::Constants
are also defined under Socket
. For example, Socket::AF_INET
is usable as well as Socket::Constants::AF_INET. See Socket::Constants
for the list of constants.
Sockets are endpoints of a bidirectional communication channel. Sockets can communicate within a process, between processes on the same machine or between different machines. There are many types of socket: TCPSocket
, UDPSocket
or UNIXSocket
for example.
Sockets have their own vocabulary:
domain: The family of protocols:
type: The type of communications between the two endpoints, typically
protocol: Typically zero. This may be used to identify a variant of a protocol.
hostname: The identifier of a network interface:
a string (hostname, IPv4 or IPv6 address or broadcast
which specifies a broadcast address)
a zero-length string which specifies INADDR_ANY
an integer (interpreted as binary address in host byte order).
Many of the classes, such as TCPSocket
, UDPSocket
or UNIXSocket
, ease the use of sockets comparatively to the equivalent C programming interface.
Let’s create an internet socket using the IPv4 protocol in a C-like manner:
require 'socket' s = Socket.new Socket::AF_INET, Socket::SOCK_STREAM s.connect Socket.pack_sockaddr_in(80, 'example.com')
You could also use the TCPSocket
class:
s = TCPSocket.new 'example.com', 80
A simple server might look like this:
require 'socket' server = TCPServer.new 2000 # Server bound 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 simple client may look like this:
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
Exception
Handling Ruby’s Socket
implementation raises exceptions based on the error generated by the system dependent implementation. This is why the methods are documented in a way that isolate Unix-based system exceptions from Windows based exceptions. If more information on a particular exception is needed, please refer to the Unix manual pages or the Windows WinSock reference.
Although the general way to create socket is Socket.new
, there are several methods of socket creation for most cases.
Zach Dennis
Sam Roberts
Programming Ruby from The Pragmatic Bookshelf.
Much material in this documentation is taken with permission from Programming Ruby from The Pragmatic Bookshelf.
BasicSocket
is the super class for all the Socket
classes.