Results for: "module_function"

Returns a JSON string representing self:

require 'json/add/ostruct'
puts OpenStruct.new('name' => 'Rowdy', :age => nil).to_json

Output:

{"json_class":"OpenStruct","t":{'name':'Rowdy',"age":null}}

See as_json.

Methods Range#as_json and Range.json_create may be used to serialize and deserialize a Range object; see Marshal.

Method Range#as_json serializes self, returning a 2-element hash representing self:

require 'json/add/range'
x = (1..4).as_json     # => {"json_class"=>"Range", "a"=>[1, 4, false]}
y = (1...4).as_json    # => {"json_class"=>"Range", "a"=>[1, 4, true]}
z = ('a'..'d').as_json # => {"json_class"=>"Range", "a"=>["a", "d", false]}

Method JSON.create deserializes such a hash, returning a Range object:

Range.json_create(x) # => 1..4
Range.json_create(y) # => 1...4
Range.json_create(z) # => "a".."d"

Returns a JSON string representing self:

require 'json/add/range'
puts (1..4).to_json
puts (1...4).to_json
puts ('a'..'d').to_json

Output:

{"json_class":"Range","a":[1,4,false]}
{"json_class":"Range","a":[1,4,true]}
{"json_class":"Range","a":["a","d",false]}

See as_json.

Methods Regexp#as_json and Regexp.json_create may be used to serialize and deserialize a Regexp object; see Marshal.

Method Regexp#as_json serializes self, returning a 2-element hash representing self:

require 'json/add/regexp'
x = /foo/.as_json
# => {"json_class"=>"Regexp", "o"=>0, "s"=>"foo"}

Method JSON.create deserializes such a hash, returning a Regexp object:

Regexp.json_create(x) # => /foo/

Returns a JSON string representing self:

require 'json/add/regexp'
puts /foo/.to_json

Output:

{"json_class":"Regexp","o":0,"s":"foo"}

Returns object if it is a regexp:

Regexp.try_convert(/re/) # => /re/

Otherwise if object responds to :to_regexp, calls object.to_regexp and returns the result.

Returns nil if object does not respond to :to_regexp.

Regexp.try_convert('re') # => nil

Raises an exception unless object.to_regexp returns a regexp.

Returns true if matching against re can be done in linear time to the input string.

Regexp.linear_time?(/re/) # => true

Note that this is a property of the ruby interpreter, not of the argument regular expression. Identical regexp can or cannot run in linear time depending on your ruby binary. Neither forward nor backward compatibility is guaranteed about the return value of this method. Our current algorithm is (*1) but this is subject to change in the future. Alternative implementations can also behave differently. They might always return false for everything.

(*1): doi.org/10.1109/SP40001.2021.00032

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"]}

See as_json.

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}}

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"}

See as_json.

Returns the object for which the receiver is the singleton class.

Raises an TypeError if the class is not a singleton class.

class Foo; end

Foo.singleton_class.attached_object        #=> Foo
Foo.attached_object                        #=> TypeError: `Foo' is not a singleton class
Foo.new.singleton_class.attached_object    #=> #<Foo:0x000000010491a370>
TrueClass.attached_object                  #=> TypeError: `TrueClass' is not a singleton class
NilClass.attached_object                   #=> TypeError: `NilClass' is not a singleton class
No documentation available

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.

Parameters

Example

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

See

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.

Example

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

See

Packs path as an AF_UNIX sockaddr string.

Socket.sockaddr_un("/tmp/sock") #=> "\x01\x00/tmp/sock\x00\x00..."

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.

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 result, mesg, is the data received.

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.

Parameters

Example

serv = TCPServer.new("127.0.0.1", 0)
af, port, host, addr = serv.addr
c = TCPSocket.new(addr, port)
s = serv.accept
c.send "aaa", 0
begin # emulate blocking recv.
  p s.recv_nonblock(10) #=> "aaa"
rescue IO::WaitReadable
  IO.select([s])
  retry
end

Refer to Socket#recvfrom for the exceptions that may be thrown if the call to recv_nonblock fails.

BasicSocket#recv_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 recv_nonblock.

By specifying a keyword argument exception to false, you can indicate that recv_nonblock should not raise an IO::WaitReadable exception, but return the symbol :wait_readable instead.

See

Search took: 9ms  ·  Total Results: 4789