Returns a DateTime object which denotes self.
Waits until IO is writable and returns a truthy value or a falsy value when times out.
You must require ‘io/wait’ to use this method.
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.
This method is defined for backward compatibility.
Scans the string until the pattern is matched. Returns the substring up to and including the end of the match, advancing the scan pointer to that location. If there is no match, nil is returned.
s = StringScanner.new("Fri Dec 12 1975 14:39") s.scan_until(/1/) # -> "Fri Dec 1" s.pre_match # -> "Fri Dec " s.scan_until(/XYZ/) # -> nil
Advances the scan pointer until pattern is matched and consumed. Returns the number of bytes advanced, or nil if no match was found.
Look ahead to match pattern, and advance the scan pointer to the end of the match. Return the number of characters advanced, or nil if the match was unsuccessful.
It’s similar to scan_until, but without returning the intervening string.
s = StringScanner.new("Fri Dec 12 1975 14:39") s.skip_until /12/ # -> 10 s #
This returns the value that scan_until would return, without advancing the scan pointer. The match register is affected, though.
s = StringScanner.new("Fri Dec 12 1975 14:39") s.check_until /12/ # -> "Fri Dec 12" s.pos # -> 0 s.matched # -> 12
Mnemonic: it “checks” to see whether a scan_until will return a value.
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 the data created by parsing the first line of string or io using the specified options.
Argument string should be a String object; it will be put into a new StringIO object positioned at the beginning.
Argument io should be an IO object that is:
Open for reading; on return, the IO object will be closed.
Positioned at the beginning. To position at the end, for appending, use method CSV.generate. For any other positioning, pass a preset StringIO object instead.
Argument options: see Options for Parsing
headers Without option headers, returns the first row as a new Array.
These examples assume prior execution of:
string = "foo,0\nbar,1\nbaz,2\n" path = 't.csv' File.write(path, string)
Parse the first line from a String object:
CSV.parse_line(string) # => ["foo", "0"]
Parse the first line from a File object:
File.open(path) do |file| CSV.parse_line(file) # => ["foo", "0"] end # => ["foo", "0"]
Returns nil if the argument is an empty String:
CSV.parse_line('') # => nil
headers With {option headers}, returns the first row as a CSV::Row object.
These examples assume prior execution of:
string = "Name,Count\nfoo,0\nbar,1\nbaz,2\n" path = 't.csv' File.write(path, string)
Parse the first line from a String object:
CSV.parse_line(string, headers: true) # => #<CSV::Row "Name":"foo" "Count":"0">
Parse the first line from a File object:
File.open(path) do |file| CSV.parse_line(file, headers: true) end # => #<CSV::Row "Name":"foo" "Count":"0">
Raises an exception if the argument is nil:
# Raises ArgumentError (Cannot parse nil as CSV): CSV.parse_line(nil)
Returns the value that determines whether unconverted fields are to be available; used for parsing; see {Option unconverted_fields}:
CSV.new('').unconverted_fields? # => nil
Returns an Array containing header converters; used for parsing; see Header Converters:
CSV.new('').header_converters # => []
Notes that you need to call +Ractor.make_shareable(CSV::HeaderConverters)+ on the main Ractor to use this method.
Returns the value that determines whether illegal input is to be handled; used for parsing; see {Option liberal_parsing}:
CSV.new('').liberal_parsing? # => false
The block need not return a String object:
csv = CSV.open(path, headers: true) csv.header_convert {|header, field_info| header.to_sym } table = csv.read table.headers # => [:Name, :Value]
If converter_name is given, the block is not called:
csv = CSV.open(path, headers: true) csv.header_convert(:downcase) {|header, field_info| fail 'Cannot happen' } table = csv.read table.headers # => ["name", "value"]
Raises a parse-time exception if converter_name is not the name of a built-in field converter:
csv = CSV.open(path, headers: true) csv.header_convert(:nosuch) # Raises NoMethodError (undefined method `arity' for nil:NilClass) csv.read
Processes fields with @converters, or @header_converters if headers is passed as true, returning the converted field set. Any converter that changes the field into something other than a String halts the pipeline of conversion for that field. This is primarily an efficiency shortcut.
Returns a string for DNS reverse lookup compatible with RFC3172.