Returns a JSON
string representing self
:
require 'json/add/regexp' puts /foo/.to_json
Output:
{"json_class":"Regexp","o":0,"s":"foo"}
See as_json
.
Methods Set#as_json
and Set.json_create
may be used to serialize and deserialize a Set object; see Marshal
.
Method Set#as_json
serializes self
, returning a 2-element hash representing self
:
require 'json/add/set' x = Set.new(%w/foo bar baz/).as_json # => {"json_class"=>"Set", "a"=>["foo", "bar", "baz"]}
Method JSON.create
deserializes such a hash, returning a Set object:
Set.json_create(x) # => #<Set: {"foo", "bar", "baz"}>
Returns a JSON
string representing self
:
require 'json/add/set' puts Set.new(%w/foo bar baz/).to_json
Output:
{"json_class":"Set","a":["foo","bar","baz"]}
Methods Struct#as_json
and Struct.json_create
may be used to serialize and deserialize a Struct object; see Marshal
.
Method Struct#as_json
serializes self
, returning a 2-element hash representing self
:
require 'json/add/struct' Customer = Struct.new('Customer', :name, :address, :zip) x = Struct::Customer.new.as_json # => {"json_class"=>"Struct::Customer", "v"=>[nil, nil, nil]}
Method JSON.create
deserializes such a hash, returning a Struct object:
Struct::Customer.json_create(x) # => #<struct Struct::Customer name=nil, address=nil, zip=nil>
Returns a JSON
string representing self
:
require 'json/add/struct' Customer = Struct.new('Customer', :name, :address, :zip) puts Struct::Customer.new.to_json
Output:
{"json_class":"Struct","t":{'name':'Rowdy',"age":null}}
Returns true
if the class was initialized with keyword_init: true
. Otherwise returns nil
or false
.
Examples:
Foo = Struct.new(:a) Foo.keyword_init? # => nil Bar = Struct.new(:a, keyword_init: true) Bar.keyword_init? # => true Baz = Struct.new(:a, keyword_init: false) Baz.keyword_init? # => false
Calls the given block with each member name/value pair; returns self
:
Customer = Struct.new(:name, :address, :zip) # => Customer joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345) joe.each_pair {|(name, value)| p "#{name} => #{value}" }
Output:
"name => Joe Smith" "address => 123 Maple, Anytown NC" "zip => 12345"
Returns an Enumerator
if no block is given.
Related: each
.
Returns a hash of the name/value pairs for the given member names.
Customer = Struct.new(:name, :address, :zip) joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345) h = joe.deconstruct_keys([:zip, :address]) h # => {:zip=>12345, :address=>"123 Maple, Anytown NC"}
Returns all names and values if array_of_names
is nil
:
h = joe.deconstruct_keys(nil) h # => {:name=>"Joseph Smith, Jr.", :address=>"123 Maple, Anytown NC", :zip=>12345}
Methods Symbol#as_json
and Symbol.json_create
may be used to serialize and deserialize a Symbol object; see Marshal
.
Method Symbol#as_json
serializes self
, returning a 2-element hash representing self
:
require 'json/add/symbol' x = :foo.as_json # => {"json_class"=>"Symbol", "s"=>"foo"}
Method JSON.create
deserializes such a hash, returning a Symbol object:
Symbol.json_create(x) # => :foo
Returns a JSON
string representing self
:
require 'json/add/symbol' puts :foo.to_json
Output:
# {"json_class":"Symbol","s":"foo"}
Equivalent to self.to_s.end_with?
; see String#end_with?
.
Returns true if this class can be used to create an instance from a serialised JSON
string. The class has to implement a class method json_create that expects a hash as first parameter. The hash should include the required data.
Creates a new MonitorMixin::ConditionVariable
associated with the Monitor
object.
Return the path as a String
.
to_path
is implemented so Pathname
objects are usable with File.open
, etc.
Receives up to maxlen bytes from socket
using recvfrom(2) after O_NONBLOCK is set for the underlying file descriptor. flags is zero or more of the MSG_
options. The first element of the results, mesg, is the data received. The second element, sender_addrinfo, contains protocol-specific address information of the sender.
When recvfrom(2) returns 0, Socket#recv_nonblock
returns nil. In most cases it means the connection was closed, but for UDP connections it may mean an empty packet was received, as the underlying API makes it impossible to distinguish these two cases.
maxlen
- the maximum number of bytes to receive from the socket
flags
- zero or more of the MSG_
options
outbuf
- destination String
buffer
opts
- keyword hash, supporting ‘exception: false`
# In one file, start this first require 'socket' include Socket::Constants socket = Socket.new(AF_INET, SOCK_STREAM, 0) sockaddr = Socket.sockaddr_in(2200, 'localhost') socket.bind(sockaddr) socket.listen(5) client, client_addrinfo = socket.accept begin # emulate blocking recvfrom pair = client.recvfrom_nonblock(20) rescue IO::WaitReadable IO.select([client]) retry end data = pair[0].chomp puts "I only received 20 bytes '#{data}'" sleep 1 socket.close # In another file, start this second require 'socket' include Socket::Constants socket = Socket.new(AF_INET, SOCK_STREAM, 0) sockaddr = Socket.sockaddr_in(2200, 'localhost') socket.connect(sockaddr) socket.puts "Watch this get cut short!" socket.close
Refer to Socket#recvfrom
for the exceptions that may be thrown if the call to recvfrom_nonblock fails.
Socket#recvfrom_nonblock
may raise any error corresponding to recvfrom(2) failure, including Errno::EWOULDBLOCK.
If the exception is Errno::EWOULDBLOCK or Errno::EAGAIN, it is extended by IO::WaitReadable
. So IO::WaitReadable
can be used to rescue the exceptions for retrying recvfrom_nonblock.
By specifying a keyword argument exception to false
, you can indicate that recvfrom_nonblock
should not raise an IO::WaitReadable
exception, but return the symbol :wait_readable
instead.
Accepts an incoming connection using accept(2) after O_NONBLOCK is set for the underlying file descriptor. It returns an array containing the accepted socket for the incoming connection, client_socket, and an Addrinfo
, client_addrinfo.
# In one script, start this first require 'socket' include Socket::Constants socket = Socket.new(AF_INET, SOCK_STREAM, 0) sockaddr = Socket.sockaddr_in(2200, 'localhost') socket.bind(sockaddr) socket.listen(5) begin # emulate blocking accept client_socket, client_addrinfo = socket.accept_nonblock rescue IO::WaitReadable, Errno::EINTR IO.select([socket]) retry end puts "The client said, '#{client_socket.readline.chomp}'" client_socket.puts "Hello from script one!" socket.close # In another script, start this second require 'socket' include Socket::Constants socket = Socket.new(AF_INET, SOCK_STREAM, 0) sockaddr = Socket.sockaddr_in(2200, 'localhost') socket.connect(sockaddr) socket.puts "Hello from script 2." puts "The server said, '#{socket.readline.chomp}'" socket.close
Refer to Socket#accept
for the exceptions that may be thrown if the call to accept_nonblock fails.
Socket#accept_nonblock
may raise any error corresponding to accept(2) failure, including Errno::EWOULDBLOCK.
If the exception is Errno::EWOULDBLOCK, Errno::EAGAIN, Errno::ECONNABORTED or Errno::EPROTO, it is extended by IO::WaitReadable
. So IO::WaitReadable
can be used to rescue the exceptions for retrying accept_nonblock.
By specifying a keyword argument exception to false
, you can indicate that accept_nonblock
should not raise an IO::WaitReadable
exception, but return the symbol :wait_readable
instead.
Disallows further write using shutdown system call.
UNIXSocket.pair {|s1, s2| s1.print "ping" s1.close_write p s2.read #=> "ping" s2.print "pong" s2.close p s1.read #=> "pong" }
Returns an address of the socket suitable for connect in the local machine.
This method returns self.local_address, except following condition.
IPv4 unspecified address (0.0.0.0) is replaced by IPv4 loopback address (127.0.0.1).
IPv6 unspecified address (::) is replaced by IPv6 loopback address (::1).
If the local address is not suitable for connect, SocketError
is raised. IPv4 and IPv6 address which port is 0 is not suitable for connect. Unix domain socket which has no path is not suitable for connect.
Addrinfo.tcp("0.0.0.0", 0).listen {|serv| p serv.connect_address #=> #<Addrinfo: 127.0.0.1:53660 TCP> serv.connect_address.connect {|c| s, _ = serv.accept p [c, s] #=> [#<Socket:fd 4>, #<Socket:fd 6>] } }
sendmsg_nonblock
sends a message using sendmsg(2) system call in non-blocking manner.
It is similar to BasicSocket#sendmsg
but the non-blocking flag is set before the system call and it doesn’t retry the system call.
By specifying a keyword argument exception to false
, you can indicate that sendmsg_nonblock
should not raise an IO::WaitWritable
exception, but return the symbol :wait_writable
instead.