Attempts to convert object
into an IO object via method to_io
; returns the new IO object if successful, or nil
otherwise:
IO.try_convert(STDOUT) # => #<IO:<STDOUT>> IO.try_convert(ARGF) # => #<IO:<STDIN>> IO.try_convert('STDOUT') # => nil
Writes the given string to ios using the write(2) system call after O_NONBLOCK is set for the underlying file descriptor.
It returns the number of bytes written.
write_nonblock
just calls the write(2) system call. It causes all errors the write(2) system call causes: Errno::EWOULDBLOCK, Errno::EINTR, etc. The result may also be smaller than string.length (partial write). The caller should care such errors and partial write.
If the exception is Errno::EWOULDBLOCK or Errno::EAGAIN, it is extended by IO::WaitWritable
. So IO::WaitWritable
can be used to rescue the exceptions for retrying write_nonblock.
# Creates a pipe. r, w = IO.pipe # write_nonblock writes only 65536 bytes and return 65536. # (The pipe size is 65536 bytes on this environment.) s = "a" * 100000 p w.write_nonblock(s) #=> 65536 # write_nonblock cannot write a byte and raise EWOULDBLOCK (EAGAIN). p w.write_nonblock("b") # Resource temporarily unavailable (Errno::EAGAIN)
If the write buffer is not empty, it is flushed at first.
When write_nonblock
raises an exception kind of IO::WaitWritable
, write_nonblock
should not be called until io is writable for avoiding busy loop. This can be done as follows.
begin result = io.write_nonblock(string) rescue IO::WaitWritable, Errno::EINTR IO.select(nil, [io]) retry end
Note that this doesn’t guarantee to write all data in string. The length written is reported as result and it should be checked later.
On some platforms such as Windows, write_nonblock
is not supported according to the kind of the IO
object. In such cases, write_nonblock
raises Errno::EBADF
.
By specifying a keyword argument exception to false
, you can indicate that write_nonblock
should not raise an IO::WaitWritable
exception, but return the symbol :wait_writable
instead.
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.
Requests a connection to be made on the given remote_sockaddr
after O_NONBLOCK is set for the underlying file descriptor. Returns 0 if successful, otherwise an exception is raised.
# +remote_sockaddr+ - the +struct+ sockaddr contained in a string or Addrinfo object
# Pull down Google's web page require 'socket' include Socket::Constants socket = Socket.new(AF_INET, SOCK_STREAM, 0) sockaddr = Socket.sockaddr_in(80, 'www.google.com') begin # emulate blocking connect socket.connect_nonblock(sockaddr) rescue IO::WaitWritable IO.select(nil, [socket]) # wait 3-way handshake completion begin socket.connect_nonblock(sockaddr) # check connection failure rescue Errno::EISCONN end end socket.write("GET / HTTP/1.0\r\n\r\n") results = socket.read
Refer to Socket#connect
for the exceptions that may be thrown if the call to connect_nonblock fails.
Socket#connect_nonblock
may raise any error corresponding to connect(2) failure, including Errno::EINPROGRESS.
If the exception is Errno::EINPROGRESS, it is extended by IO::WaitWritable
. So IO::WaitWritable
can be used to rescue the exceptions for retrying connect_nonblock.
By specifying a keyword argument exception to false
, you can indicate that connect_nonblock
should not raise an IO::WaitWritable
exception, but return the symbol :wait_writable
instead.
# Socket#connect
Returns true for IPv4 multicast address (224.0.0.0/4). It returns false otherwise.
Returns true for IPv6 multicast address (ff00::/8). It returns false otherwise.
Attempts to [match] the given pattern
anywhere (at any [position]) in the [target substring]; does not modify the [positions].
If the match succeeds:
Sets all [match values].
Returns the matched substring, which extends from the current [position] to the end of the matched substring.
scanner = StringScanner.new('foobarbazbatbam') scanner.pos = 6 scanner.check_until(/bat/) # => "bazbat" put_match_values(scanner) # Basic match values: # matched?: true # matched_size: 3 # pre_match: "foobarbaz" # matched : "bat" # post_match: "bam" # Captured match values: # size: 1 # captures: [] # named_captures: {} # values_at: ["bat", nil] # []: # [0]: "bat" # [1]: nil put_situation(scanner) # Situation: # pos: 6 # charpos: 6 # rest: "bazbatbam" # rest_size: 9
If the match fails:
Clears all [match values].
Returns nil
.
scanner.check_until(/nope/) # => nil match_values_cleared?(scanner) # => true
If obj
is a Hash
object, returns obj
.
Otherwise if obj
responds to :to_hash
, calls obj.to_hash
and returns the result.
Returns nil
if obj
does not respond to :to_hash
Raises an exception unless obj.to_hash
returns a Hash
object.
Returns a string for DNS reverse lookup compatible with RFC3172.
Sets the date-time format.
Argument datetime_format
should be either of these:
A string suitable for use as a format for method Time#strftime
.
nil
: the logger uses '%Y-%m-%dT%H:%M:%S.%6N'
.
Returns the date-time format; see datetime_format=
.
Shows version string in packages if Version
is defined.
pkgs
package list
Ruby tries to load the library named string relative to the directory containing the requiring file. If the file does not exist a LoadError is raised. Returns true
if the file was loaded and false
if the file was already loaded before.
Leaves exclusive section.
Enters exclusive section and executes the block. Leaves the exclusive section automatically when the block exits. See example under MonitorMixin
.
Constant time memory comparison. Inputs are hashed using SHA-256 to mask the length of the secret. Returns true
if the strings are identical, false
otherwise.
Parse a file at filename
. Returns the Psych::Nodes::Document
.
Raises a Psych::SyntaxError
when a YAML
syntax error is detected.
Parse a YAML
string in yaml
. Returns the Psych::Nodes::Stream
. This method can handle multiple YAML
documents contained in yaml
. filename
is used in the exception message if a Psych::SyntaxError
is raised.
If a block is given, a Psych::Nodes::Document
node will be yielded to the block as it’s being parsed.
Raises a Psych::SyntaxError
when a YAML
syntax error is detected.
Example:
Psych.parse_stream("---\n - a\n - b") # => #<Psych::Nodes::Stream:0x00> Psych.parse_stream("--- a\n--- b") do |node| node # => #<Psych::Nodes::Document:0x00> end begin Psych.parse_stream("--- `", filename: "file.txt") rescue Psych::SyntaxError => ex ex.file # => 'file.txt' ex.message # => "(file.txt): found character that cannot start any token" end
Raises a TypeError
when NilClass
is passed.
See Psych::Nodes
for more information about YAML
AST.
Returns the version of libyaml being used
Returns the string which represents the version of zlib library.