Compile a ArrayPatternNode
node
Dispatch enter and leave events for ArrayPatternNode
nodes and continue walking the tree.
in [foo, bar, baz]
Returns a new String that is a copy of string
.
With no arguments, returns the empty string with the Encoding
ASCII-8BIT
:
s = String.new s # => "" s.encoding # => #<Encoding:ASCII-8BIT>
With optional argument string
and no keyword arguments, returns a copy of string
with the same encoding:
String.new('foo') # => "foo" String.new('тест') # => "тест" String.new('こんにちは') # => "こんにちは"
(Unlike String.new, a string literal like ''
or a string literal always has script encoding.)
With optional keyword argument encoding
, returns a copy of string
with the specified encoding; the encoding
may be an Encoding
object, an encoding name, or an encoding name alias:
String.new('foo', encoding: Encoding::US_ASCII).encoding # => #<Encoding:US-ASCII> String.new('foo', encoding: 'US-ASCII').encoding # => #<Encoding:US-ASCII> String.new('foo', encoding: 'ASCII').encoding # => #<Encoding:US-ASCII>
The given encoding need not be valid for the string’s content, and that validity is not checked:
s = String.new('こんにちは', encoding: 'ascii') s.valid_encoding? # => false
But the given encoding
itself is checked:
String.new('foo', encoding: 'bar') # Raises ArgumentError.
With optional keyword argument capacity
, returns a copy of string
(or an empty string, if string
is not given); the given capacity
is advisory only, and may or may not set the size of the internal buffer, which may in turn affect performance:
String.new(capacity: 1) String.new('foo', capacity: 4096)
The string
, encoding
, and capacity
arguments may all be used together:
String.new('hello', encoding: 'UTF-8', capacity: 25)
Creates new Fiber
. Initially, the fiber is not running and can be resumed with resume
. Arguments to the first resume
call will be passed to the block:
f = Fiber.new do |initial| current = initial loop do puts "current: #{current.inspect}" current = Fiber.yield end end f.resume(100) # prints: current: 100 f.resume(1, 2, 3) # prints: current: [1, 2, 3] f.resume # prints: current: nil # ... and so on ...
If blocking: false
is passed to Fiber.new
, and current thread has a Fiber.scheduler
defined, the Fiber
becomes non-blocking (see “Non-blocking Fibers” section in class docs).
If the storage
is unspecified, the default is to inherit a copy of the storage from the current fiber. This is the same as specifying storage: true
.
Fiber[:x] = 1 Fiber.new do Fiber[:x] # => 1 Fiber[:x] = 2 end.resume Fiber[:x] # => 1
If the given storage
is nil
, this function will lazy initialize the internal storage, which starts as an empty hash.
Fiber[:x] = "Hello World" Fiber.new(storage: nil) do Fiber[:x] # nil end
Otherwise, the given storage
is used as the new fiber’s storage, and it must be an instance of Hash
.
Explicitly using storage: true
is currently experimental and may change in the future.
Returns a new Dir object for the directory at dirpath
:
Dir.new('.') # => #<Dir:.>
The value given with optional keyword argument encoding
specifies the encoding for the directory entry names; if nil
(the default), the file system’s encoding is used:
Dir.new('.').read.encoding # => #<Encoding:UTF-8> Dir.new('.', encoding: 'US-ASCII').read.encoding # => #<Encoding:US-ASCII>
Opens the file at the given path
according to the given mode
; creates and returns a new File
object for that file.
The new File
object is buffered mode (or non-sync mode), unless filename
is a tty. See IO#flush
, IO#fsync
, IO#fdatasync
, and IO#sync=
.
Argument path
must be a valid file path:
f = File.new('/etc/fstab') f.close f = File.new('t.txt') f.close
Optional argument mode
(defaults to ‘r’) must specify a valid mode; see Access Modes:
f = File.new('t.tmp', 'w') f.close f = File.new('t.tmp', File::RDONLY) f.close
Optional argument perm
(defaults to 0666) must specify valid permissions see File Permissions:
f = File.new('t.tmp', File::CREAT, 0644) f.close f = File.new('t.tmp', File::CREAT, 0444) f.close
Optional keyword arguments opts
specify:
Encoding options.
Creates a new Enumerator
object, which can be used as an Enumerable
.
Iteration is defined by the given block, in which a “yielder” object, given as block parameter, can be used to yield a value by calling the yield
method (aliased as <<
):
fib = Enumerator.new do |y| a = b = 1 loop do y << a a, b = b, a + b end end fib.take(10) # => [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
The optional parameter can be used to specify how to calculate the size in a lazy fashion (see Enumerator#size
). It can either be a value or a callable object.
Construct a new Exception
object, optionally passing in a message.
Create a new SystemExit
exception with the given status and message. Status is true, false, or an integer. If status is not given, true is used.
Construct a new SignalException
object. sig_name
should be a known signal name.
Construct a new KeyError
exception with the given message, receiver and key.
Construct a SyntaxError
exception.
Construct a new NameError
exception. If given the name parameter may subsequently be examined using the NameError#name
method. receiver parameter allows to pass object in context of which the error happened. Example:
[1, 2, 3].method(:rject) # NameError with name "rject" and receiver: Array [1, 2, 3].singleton_method(:rject) # NameError with name "rject" and receiver: [1, 2, 3]
Construct a NoMethodError
exception for a method of the given name called with the given arguments. The name may be accessed using the name
method on the resulting object, and the arguments using the args
method.
If private argument were passed, it designates method was attempted to call in private context, and can be accessed with private_call?
method.
receiver argument stores an object whose method was called.
Construct a new FrozenError
exception. If given the receiver parameter may subsequently be examined using the FrozenError#receiver
method.
a = [].freeze raise FrozenError.new("can't modify frozen array", receiver: a)
Construct a new NoMatchingPatternKeyError
exception with the given message, matchee and key.
If errno corresponds to a known system error code, constructs the appropriate Errno
class for that error, otherwise constructs a generic SystemCallError
object. The error number is subsequently available via the errno
method.
Creates a new anonymous module. If a block is given, it is passed the module object, and the block is evaluated in the context of this module like module_eval
.
fred = Module.new do def meth1 "hello" end def meth2 "bye" end end a = "my string" a.extend(fred) #=> "my string" a.meth1 #=> "hello" a.meth2 #=> "bye"
Assign the module to a constant (name starting uppercase) if you want to treat it like a regular module.
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.
Returns a new Date object constructed from the given arguments:
Date.new(2022).to_s # => "2022-01-01" Date.new(2022, 2).to_s # => "2022-02-01" Date.new(2022, 2, 4).to_s # => "2022-02-04"
Argument month
should be in range (1..12) or range (-12..-1); when the argument is negative, counts backward from the end of the year:
Date.new(2022, -11, 4).to_s # => "2022-02-04"
Argument mday
should be in range (1..n) or range (-n..-1) where n
is the number of days in the month; when the argument is negative, counts backward from the end of the month.
See argument start.
Related: Date.jd
.
Same as DateTime.new
.
Returns a new Time
object based on the given arguments, by default in the local timezone.
With no positional arguments, returns the value of Time.now
:
Time.new # => 2021-04-24 17:27:46.0512465 -0500
With one string argument that represents a time, returns a new Time
object based on the given argument, in the local timezone.
Time.new('2000-12-31 23:59:59.5') # => 2000-12-31 23:59:59.5 -0600 Time.new('2000-12-31 23:59:59.5 +0900') # => 2000-12-31 23:59:59.5 +0900 Time.new('2000-12-31 23:59:59.5', in: '+0900') # => 2000-12-31 23:59:59.5 +0900 Time.new('2000-12-31 23:59:59.5') # => 2000-12-31 23:59:59.5 -0600 Time.new('2000-12-31 23:59:59.56789', precision: 3) # => 2000-12-31 23:59:59.567 -0600
With one to six arguments, returns a new Time
object based on the given arguments, in the local timezone.
Time.new(2000, 1, 2, 3, 4, 5) # => 2000-01-02 03:04:05 -0600
For the positional arguments (other than zone
):
year
: Year, with no range limits:
Time.new(999999999) # => 999999999-01-01 00:00:00 -0600 Time.new(-999999999) # => -999999999-01-01 00:00:00 -0600
month
: Month in range (1..12), or case-insensitive 3-letter month name:
Time.new(2000, 1) # => 2000-01-01 00:00:00 -0600 Time.new(2000, 12) # => 2000-12-01 00:00:00 -0600 Time.new(2000, 'jan') # => 2000-01-01 00:00:00 -0600 Time.new(2000, 'JAN') # => 2000-01-01 00:00:00 -0600
mday
: Month day in range(1..31):
Time.new(2000, 1, 1) # => 2000-01-01 00:00:00 -0600 Time.new(2000, 1, 31) # => 2000-01-31 00:00:00 -0600
hour
: Hour in range (0..23), or 24 if min
, sec
, and usec
are zero:
Time.new(2000, 1, 1, 0) # => 2000-01-01 00:00:00 -0600 Time.new(2000, 1, 1, 23) # => 2000-01-01 23:00:00 -0600 Time.new(2000, 1, 1, 24) # => 2000-01-02 00:00:00 -0600
min
: Minute in range (0..59):
Time.new(2000, 1, 1, 0, 0) # => 2000-01-01 00:00:00 -0600 Time.new(2000, 1, 1, 0, 59) # => 2000-01-01 00:59:00 -0600
sec
: Second in range (0…61):
Time.new(2000, 1, 1, 0, 0, 0) # => 2000-01-01 00:00:00 -0600 Time.new(2000, 1, 1, 0, 0, 59) # => 2000-01-01 00:00:59 -0600 Time.new(2000, 1, 1, 0, 0, 60) # => 2000-01-01 00:01:00 -0600
Time.new(2000, 1, 1, 0, 0, 59.5) # => 2000-12-31 23:59:59.5 +0900 Time.new(2000, 1, 1, 0, 0, 59.7r) # => 2000-12-31 23:59:59.7 +0900
These values may be:
Integers, as above.
Numerics convertible to integers:
Time.new(Float(0.0), Rational(1, 1), 1.0, 0.0, 0.0, 0.0) # => 0000-01-01 00:00:00 -0600
String
integers:
a = %w[0 1 1 0 0 0] # => ["0", "1", "1", "0", "0", "0"] Time.new(*a) # => 0000-01-01 00:00:00 -0600
When positional argument zone
or keyword argument in:
is given, the new Time
object is in the specified timezone. For the forms of argument zone
, see Timezone Specifiers:
Time.new(2000, 1, 1, 0, 0, 0, '+12:00') # => 2000-01-01 00:00:00 +1200 Time.new(2000, 1, 1, 0, 0, 0, in: '-12:00') # => 2000-01-01 00:00:00 -1200 Time.new(in: '-12:00') # => 2022-08-23 08:49:26.1941467 -1200
Since in:
keyword argument just provides the default, so if the first argument in single string form contains time zone information, this keyword argument will be silently ignored.
Time.new('2000-01-01 00:00:00 +0100', in: '-0500').utc_offset # => 3600
precision
: maximum effective digits in sub-second part, default is 9. More digits will be truncated, as other operations of Time
. Ignored unless the first argument is a string.