Create a Pathname
object from the given String
(or String-like object). If path
contains a NULL character (\0
), an ArgumentError
is raised.
Create a new Ripper
object. src must be a String
, an IO
, or an Object
which has gets
method.
This method does not starts parsing. See also Ripper#parse
and Ripper.parse
.
Creates a new database handle by opening the given filename
. SDBM
actually uses two physical files, with extensions ‘.dir’ and ‘.pag’. These extensions will automatically be appended to the filename
.
If the file does not exist, a new file will be created using the given mode
, unless mode
is explicitly set to nil. In the latter case, no database will be created.
If the file exists, it will be opened in read/write mode. If this fails, it will be opened in read-only mode.
Creates a new socket object.
domain should be a communications domain such as: :INET, :INET6, :UNIX, etc.
socktype should be a socket type such as: :STREAM, :DGRAM, :RAW, etc.
protocol is optional and should be a protocol defined in the domain. If protocol is not given, 0 is used internally.
Socket.new(:INET, :STREAM) # TCP socket Socket.new(:INET, :DGRAM) # UDP socket Socket.new(:UNIX, :STREAM) # UNIX stream socket Socket.new(:UNIX, :DGRAM) # UNIX datagram socket
returns a new instance of Addrinfo
. The instance contains sockaddr, family, socktype, protocol. sockaddr means struct sockaddr which can be used for connect(2), etc. family, socktype and protocol are integers which is used for arguments of socket(2).
sockaddr is specified as an array or a string. The array should be compatible to the value of IPSocket#addr
or UNIXSocket#addr
. The string should be struct sockaddr as generated by Socket.sockaddr_in
or Socket.unpack_sockaddr_un
.
sockaddr examples:
Socket.sockaddr_in
(“smtp”, “2001:DB8::1”)
Socket.sockaddr_in
(80, “172.18.22.42”)
Socket.sockaddr_un
(“/tmp/sock”)
In an AF_INET/AF_INET6 sockaddr array, the 4th element, numeric IP address, is used to construct socket address in the Addrinfo
instance. If the 3rd element, textual host name, is non-nil, it is also recorded but used only for Addrinfo#inspect
.
family is specified as an integer to specify the protocol family such as Socket::PF_INET. It can be a symbol or a string which is the constant name with or without PF_ prefix such as :INET, :INET6, :UNIX, “PF_INET”, etc. If omitted, PF_UNSPEC is assumed.
socktype is specified as an integer to specify the socket type such as Socket::SOCK_STREAM. It can be a symbol or a string which is the constant name with or without SOCK_ prefix such as :STREAM, :DGRAM, :RAW, “SOCK_STREAM”, etc. If omitted, 0 is assumed.
protocol is specified as an integer to specify the protocol such as Socket::IPPROTO_TCP. It must be an integer, unlike family and socktype. If omitted, 0 is assumed. Note that 0 is reasonable value for most protocols, except raw socket.
Creates a new UDPSocket
object.
address_family should be an integer, a string or a symbol: Socket::AF_INET, “AF_INET”, :INET, etc.
require 'socket' UDPSocket.new #=> #<UDPSocket:fd 3> UDPSocket.new(Socket::AF_INET6) #=> #<UDPSocket:fd 4>
Creates a new server socket bound to port.
If hostname is given, the socket is bound to it.
serv = TCPServer.new("127.0.0.1", 28561) s = serv.accept s.puts Time.now s.close
Internally, TCPServer.new
calls getaddrinfo() function to obtain addresses. If getaddrinfo() returns multiple addresses, TCPServer.new
tries to create a server socket for each address and returns first one that is successful.
Creates a new UNIX server socket bound to path.
require 'socket' serv = UNIXServer.new("/tmp/sock") s = serv.accept p s.read
Opens a SOCKS connection to host
via the SOCKS server.
The SOCKS server configuration varies by implementation
When using the Dante libsocks/libsocksd implementation it is configured as SOCKS_SERVER env var.
See: manpages.debian.org/testing/dante-client/socksify.1.en.html for full env variable support.
Opens a TCP connection to remote_host
on remote_port
. If local_host
and local_port
are specified, then those parameters are used on the local end to establish the connection.
Creates a new UNIX client socket connected to path.
require 'socket' s = UNIXSocket.new("/tmp/sock") s.send "hello", 0
Creates new StringIO
instance from with string and mode.
Creates a new StringScanner
object to scan over the given string
.
If fixed_anchor
is true
, \A
always matches the beginning of the string. Otherwise, \A
always matches the current position.
dup
argument is obsolete and not used now.
Returns a new WIN32OLE
object(OLE Automation object). The first argument server specifies OLE Automation server. The first argument should be CLSID or PROGID. If second argument host specified, then returns OLE Automation object on host. If :license keyword argument is provided, IClassFactory2::CreateInstanceLic is used to create instance of licensed server.
WIN32OLE.new('Excel.Application') # => Excel OLE Automation WIN32OLE object. WIN32OLE.new('{00024500-0000-0000-C000-000000000046}') # => Excel OLE Automation WIN32OLE object.
Returns a new, empty hash. If this hash is subsequently accessed by a key that doesn’t correspond to a hash entry, the value returned depends on the style of new
used to create the hash. In the first form, the access returns nil
. If obj is specified, this single object will be used for all default values. If a block is specified, it will be called with the hash object and the key, and should return the default value. It is the block’s responsibility to store the value in the hash if required.
h = Hash.new("Go Fish") h["a"] = 100 h["b"] = 200 h["a"] #=> 100 h["c"] #=> "Go Fish" # The following alters the single default object h["c"].upcase! #=> "GO FISH" h["d"] #=> "GO FISH" h.keys #=> ["a", "b"] # While this creates a new default object each time h = Hash.new { |hash, key| hash[key] = "Go Fish: #{key}" } h["c"] #=> "Go Fish: c" h["c"].upcase! #=> "GO FISH: C" h["d"] #=> "Go Fish: d" h.keys #=> ["c", "d"]
Create a new CGI
instance.
tag_maker
This is the same as using the options_hash
form with the value { :tag_maker => tag_maker }
Note that it is recommended to use the options_hash
form, since it also allows you specify the charset you will accept.
options_hash
A Hash
that recognizes three options:
:accept_charset
specifies encoding of received query string. If omitted, @@accept_charset
is used. If the encoding is not valid, a CGI::InvalidEncoding
will be raised.
Example. Suppose @@accept_charset
is “UTF-8”
when not specified:
cgi=CGI.new # @accept_charset # => "UTF-8"
when specified as “EUC-JP”:
cgi=CGI.new(:accept_charset => "EUC-JP") # => "EUC-JP"
:tag_maker
String
that specifies which version of the HTML generation methods to use. If not specified, no HTML generation methods will be loaded.
The following values are supported:
HTML 3.x
HTML 4.0
HTML 4.0 Transitional
HTML 4.0 with Framesets
HTML 5
:max_multipart_length
Specifies maximum length of multipart data. Can be an Integer
scalar or a lambda, that will be evaluated when the request is parsed. This allows more complex logic to be set when determining whether to accept multipart data (e.g. consult a registered users upload allowance)
Default is 128 * 1024 * 1024 bytes
cgi=CGI.new(:max_multipart_length => 268435456) # simple scalar cgi=CGI.new(:max_multipart_length => -> {check_filesystem}) # lambda
block
If provided, the block is called when an invalid encoding is encountered. For example:
encoding_errors={} cgi=CGI.new(:accept_charset=>"EUC-JP") do |name,value| encoding_errors[name] = value end
Finally, if the CGI
object is not created in a standard CGI
call environment (that is, it can’t locate REQUEST_METHOD in its environment), then it will run in “offline” mode. In this mode, it reads its parameters from the command line or (failing that) from standard input. Otherwise, cookies and other parameters are parsed automatically from the standard CGI
locations, which varies according to the REQUEST_METHOD.
This constructor will wrap either a String
or IO
object passed in data
for reading and/or writing. In addition to the CSV
instance methods, several IO
methods are delegated. (See CSV::open()
for a complete list.) If you pass a String
for data
, you can later retrieve it (after writing to it, for example) with CSV.string().
Note that a wrapped String
will be positioned at the beginning (for reading). If you want it at the end (for writing), use CSV::generate()
. If you want any other positioning, pass a preset StringIO
object instead.
You may set any reading and/or writing preferences in the options
Hash
. Available options are:
:col_sep
The String
placed between each field. This String
will be transcoded into the data’s Encoding
before parsing.
:row_sep
The String
appended to the end of each row. This can be set to the special :auto
setting, which requests that CSV
automatically discover this from the data. Auto-discovery reads ahead in the data looking for the next "\r\n"
, "\n"
, or "\r"
sequence. A sequence will be selected even if it occurs in a quoted field, assuming that you would have the same line endings there. If none of those sequences is found, data
is ARGF
, STDIN
, STDOUT
, or STDERR
, or the stream is only available for output, the default $INPUT_RECORD_SEPARATOR
($/
) is used. Obviously, discovery takes a little time. Set
manually if speed is important. Also note that IO
objects should be opened in binary mode on Windows if this feature will be used as the line-ending translation can cause problems with resetting the document position to where it was before the read ahead. This String
will be transcoded into the data’s Encoding
before parsing.
:quote_char
The character used to quote fields. This has to be a single character String
. This is useful for application that incorrectly use '
as the quote character instead of the correct "
. CSV
will always consider a double sequence of this character to be an escaped quote. This String
will be transcoded into the data’s Encoding
before parsing.
:field_size_limit
This is a maximum size CSV
will read ahead looking for the closing quote for a field. (In truth, it reads to the first line ending beyond this size.) If a quote cannot be found within the limit CSV
will raise a MalformedCSVError
, assuming the data is faulty. You can use this limit to prevent what are effectively DoS attacks on the parser. However, this limit can cause a legitimate parse to fail and thus is set to nil
, or off, by default.
:converters
An Array
of names from the Converters
Hash
and/or lambdas that handle custom conversion. A single converter doesn’t have to be in an Array
. All built-in converters try to transcode fields to UTF-8 before converting. The conversion will fail if the data cannot be transcoded, leaving the field unchanged.
:unconverted_fields
If set to true
, an unconverted_fields() method will be added to all returned rows (Array
or CSV::Row
) that will return the fields as they were before conversion. Note that :headers
supplied by Array
or String
were not fields of the document and thus will have an empty Array
attached.
:headers
If set to :first_row
or true
, the initial row of the CSV
file will be treated as a row of headers. If set to an Array
, the contents will be used as the headers. If set to a String
, the String
is run through a call of CSV::parse_line()
with the same :col_sep
, :row_sep
, and :quote_char
as this instance to produce an Array
of headers. This setting causes CSV#shift()
to return rows as CSV::Row
objects instead of Arrays and CSV#read()
to return CSV::Table
objects instead of an Array
of Arrays.
:return_headers
When false
, header rows are silently swallowed. If set to true
, header rows are returned in a CSV::Row
object with identical headers and fields (save that the fields do not go through the converters).
:write_headers
When true
and :headers
is set, a header row will be added to the output.
:header_converters
Identical in functionality to :converters
save that the conversions are only made to header rows. All built-in converters try to transcode headers to UTF-8 before converting. The conversion will fail if the data cannot be transcoded, leaving the header unchanged.
:skip_blanks
When setting a true
value, CSV
will skip over any empty rows. Note that this setting will not skip rows that contain column separators, even if the rows contain no actual data. If you want to skip rows that contain separators but no content, consider using :skip_lines
, or inspecting fields.compact.empty? on each row.
:force_quotes
When setting a true
value, CSV
will quote all CSV
fields it creates.
:skip_lines
When setting an object responding to match
, every line matching it is considered a comment and ignored during parsing. When set to a String
, it is first converted to a Regexp
. When set to nil
no line is considered a comment. If the passed object does not respond to match
, ArgumentError
is thrown.
:liberal_parsing
When setting a true
value, CSV
will attempt to parse input not conformant with RFC 4180, such as double quotes in unquoted fields.
:nil_value
When set an object, any values of an empty field is replaced by the set object, not nil.
:empty_value
When setting an object, any values of a blank string field is replaced by the set object.
:quote_empty
When setting a true
value, CSV
will quote empty values with double quotes. When false
, CSV
will emit an empty string for an empty field value.
:write_converters
Converts values on each line with the specified Proc
object(s), which receive a String
value and return a String
or nil
value. When an array is specified, each converter will be applied in order.
:write_nil_value
When a String
value, nil
value(s) on each line will be replaced with the specified value.
:write_empty_value
When a String
or nil
value, empty value(s) on each line will be replaced with the specified value.
:strip
When setting a true
value, CSV
will strip “trnfv” around the values. If you specify a string instead of true
, CSV
will strip string. The length of the string must be 1.
See CSV::DEFAULT_OPTIONS
for the default settings.
Options cannot be overridden in the instance methods for performance reasons, so be sure to set what you want here.
Pass in the obj to delegate method calls to. All methods supported by obj will be delegated to.
Creates a new ACL
from list
with an evaluation order
of DENY_ALLOW
or ALLOW_DENY
.
An ACL
list
is an Array
of “allow” or “deny” and an address or address mask or “all” or “*” to match any address:
%w[
deny all
allow 192.0.2.2
allow 192.0.2.128/26
]
Constructs a new ERB
object with the template specified in str.
An ERB
object works by building a chunk of Ruby code that will output the completed template when run.
If trim_mode is passed a String
containing one or more of the following modifiers, ERB
will adjust its code generation as listed:
% enables Ruby code processing for lines beginning with % <> omit newline for lines starting with <% and ending in %> > omit newline for lines ending in %> - omit blank lines ending in -%>
eoutvar can be used to set the name of the variable ERB
will build up its output in. This is useful when you need to run multiple ERB
templates through the same binding and/or when you want to control where output ends up. Pass the name of the variable to be used inside a String
.
require "erb" # build data class class Listings PRODUCT = { :name => "Chicken Fried Steak", :desc => "A well messages pattie, breaded and fried.", :cost => 9.95 } attr_reader :product, :price def initialize( product = "", price = "" ) @product = product @price = price end def build b = binding # create and run templates, filling member data variables ERB.new(<<-'END_PRODUCT'.gsub(/^\s+/, ""), trim_mode: "", eoutvar: "@product").result b <%= PRODUCT[:name] %> <%= PRODUCT[:desc] %> END_PRODUCT ERB.new(<<-'END_PRICE'.gsub(/^\s+/, ""), trim_mode: "", eoutvar: "@price").result b <%= PRODUCT[:name] %> -- <%= PRODUCT[:cost] %> <%= PRODUCT[:desc] %> END_PRICE end end # setup template data listings = Listings.new listings.build puts listings.product + "\n" + listings.price
Generates
Chicken Fried Steak A well messages pattie, breaded and fried. Chicken Fried Steak -- 9.95 A well messages pattie, breaded and fried.