Creates a DateTime
object denoting the given calendar date.
DateTime.new(2001,2,3) #=> #<DateTime: 2001-02-03T00:00:00+00:00 ...> DateTime.new(2001,2,3,4,5,6,'+7') #=> #<DateTime: 2001-02-03T04:05:06+07:00 ...> DateTime.new(2001,-11,-26,-20,-55,-54,'+7') #=> #<DateTime: 2001-02-03T04:05:06+07:00 ...>
Returns a Time
object.
It is initialized to the current system time if no argument is given.
Note: The new object will use the resolution available on your system clock, and may include fractional seconds.
If one or more arguments specified, the time is initialized to the specified time.
sec
may have fraction if it is a rational.
utc_offset
is the offset from UTC. It can be a string such as “+09:00” or a number of seconds such as 32400.
a = Time.new #=> 2007-11-19 07:50:02 -0600 b = Time.new #=> 2007-11-19 07:50:02 -0600 a == b #=> false "%.6f" % a.to_f #=> "1195480202.282373" "%.6f" % b.to_f #=> "1195480202.283415" Time.new(2008,6,21, 13,30,0, "+09:00") #=> 2008-06-21 13:30:00 +0900 # A trip for RubyConf 2007 t1 = Time.new(2007,11,1,15,25,0, "+09:00") # JST (Narita) t2 = Time.new(2007,11,1,12, 5,0, "-05:00") # CDT (Minneapolis) t3 = Time.new(2007,11,1,13,25,0, "-05:00") # CDT (Minneapolis) t4 = Time.new(2007,11,1,16,53,0, "-04:00") # EDT (Charlotte) t5 = Time.new(2007,11,5, 9,24,0, "-05:00") # EST (Charlotte) t6 = Time.new(2007,11,5,11,21,0, "-05:00") # EST (Detroit) t7 = Time.new(2007,11,5,13,45,0, "-05:00") # EST (Detroit) t8 = Time.new(2007,11,6,17,10,0, "+09:00") # JST (Narita) p((t2-t1)/3600.0) #=> 10.666666666666666 p((t4-t3)/3600.0) #=> 2.466666666666667 p((t6-t5)/3600.0) #=> 1.95 p((t8-t7)/3600.0) #=> 13.416666666666666
Open a dbm database with the specified name, which can include a directory path. Any file extensions needed will be supplied automatically by the dbm library. For example, Berkeley DB appends ‘.db’, and GNU gdbm uses two physical files with extensions ‘.dir’ and ‘.pag’.
The mode should be an integer, as for Unix chmod.
The first two forms are used to create a new Struct
subclass class_name
that can contain a value for each member_name
. This subclass can be used to create instances of the structure like any other Class
.
If the class_name
is omitted an anonymous structure class will be created. Otherwise, the name of this struct will appear as a constant in class Struct
, so it must be unique for all Structs in the system and must start with a capital letter. Assigning a structure class to a constant also gives the class the name of the constant.
# Create a structure with a name under Struct Struct.new("Customer", :name, :address) #=> Struct::Customer Struct::Customer.new("Dave", "123 Main") #=> #<struct Struct::Customer name="Dave", address="123 Main"> # Create a structure named by its constant Customer = Struct.new(:name, :address) #=> Customer Customer.new("Dave", "123 Main") #=> #<struct Customer name="Dave", address="123 Main">
If the optional keyword_init
keyword argument is set to true
, .new takes keyword arguments instead of normal arguments.
Customer = Struct.new(:name, :address, keyword_init: true) Customer.new(name: "Dave", address: "123 Main") #=> #<struct Customer name="Dave", address="123 Main">
If a block is given it will be evaluated in the context of StructClass
, passing the created class as a parameter:
Customer = Struct.new(:name, :address) do def greeting "Hello #{name}!" end end Customer.new("Dave", "123 Main").greeting #=> "Hello Dave!"
This is the recommended way to customize a struct. Subclassing an anonymous struct creates an extra anonymous class that will never be used.
The last two forms create a new instance of a struct subclass. The number of value
parameters must be less than or equal to the number of attributes defined for the structure. Unset parameters default to nil
. Passing more parameters than number of attributes will raise an ArgumentError
.
Customer = Struct.new(:name, :address) Customer.new("Dave", "123 Main") #=> #<struct Customer name="Dave", address="123 Main"> Customer["Dave"] #=> #<struct Customer name="Dave", address=nil>
Returns a new IO
object (a stream) for the given integer file descriptor fd
and mode
string. opt
may be used to specify parts of mode
in a more readable fashion. See also IO.sysopen
and IO.for_fd
.
IO.new
is called by various File
and IO
opening methods such as IO::open
, Kernel#open
, and File::open
.
When mode
is an integer it must be combination of the modes defined in File::Constants
(File::RDONLY
, File::WRONLY|File::CREAT
). See the open(2) man page for more information.
When mode
is a string it must be in one of the following forms:
fmode fmode ":" ext_enc fmode ":" ext_enc ":" int_enc fmode ":" "BOM|UTF-*"
fmode
is an IO
open mode string, ext_enc
is the external encoding for the IO
and int_enc
is the internal encoding.
IO
Open Mode Ruby allows the following open modes:
"r" Read-only, starts at beginning of file (default mode). "r+" Read-write, starts at beginning of file. "w" Write-only, truncates existing file to zero length or creates a new file for writing. "w+" Read-write, truncates existing file to zero length or creates a new file for reading and writing. "a" Write-only, each write call appends data at end of file. Creates a new file for writing if file does not exist. "a+" Read-write, each write call appends data at end of file. Creates a new file for reading and writing if file does not exist.
The following modes must be used separately, and along with one or more of the modes seen above.
"b" Binary file mode Suppresses EOL <-> CRLF conversion on Windows. And sets external encoding to ASCII-8BIT unless explicitly specified. "t" Text file mode
When the open mode of original IO
is read only, the mode cannot be changed to be writable. Similarly, the open mode cannot be changed from write only to readable.
When such a change is attempted the error is raised in different locations according to the platform.
IO
Encoding
When ext_enc
is specified, strings read will be tagged by the encoding when reading, and strings output will be converted to the specified encoding when writing.
When ext_enc
and int_enc
are specified read strings will be converted from ext_enc
to int_enc
upon input, and written strings will be converted from int_enc
to ext_enc
upon output. See Encoding
for further details of transcoding on input and output.
If “BOM|UTF-8”, “BOM|UTF-16LE” or “BOM|UTF16-BE” are used, Ruby checks for a Unicode BOM in the input document to help determine the encoding. For UTF-16 encodings the file open mode must be binary. When present, the BOM is stripped and the external encoding from the BOM is used. When the BOM is missing the given Unicode encoding is used as ext_enc
. (The BOM-set encoding option is case insensitive, so “bom|utf-8” is also valid.)
opt
can be used instead of mode
for improved readability. The following keys are supported:
Same as mode
parameter
Specifies file open flags as integer. If mode
parameter is given, this parameter will be bitwise-ORed.
External encoding for the IO
.
Internal encoding for the IO
. “-” is a synonym for the default internal encoding.
If the value is nil
no conversion occurs.
Specifies external and internal encodings as “extern:intern”.
If the value is truth value, same as “t” in argument mode
.
If the value is truth value, same as “b” in argument mode
.
If the value is false
, the fd
will be kept open after this IO
instance gets finalized.
Also, opt
can have same keys in String#encode
for controlling conversion between the external encoding and the internal encoding.
fd = IO.sysopen("/dev/tty", "w") a = IO.new(fd,"w") $stderr.puts "Hello" a.puts "World"
Produces:
Hello World
require 'fcntl' fd = STDERR.fcntl(Fcntl::F_DUPFD) io = IO.new(fd, mode: 'w:UTF-16LE', cr_newline: true) io.puts "Hello, World!" fd = STDERR.fcntl(Fcntl::F_DUPFD) io = IO.new(fd, mode: 'w', cr_newline: true, external_encoding: Encoding::UTF_16LE) io.puts "Hello, World!"
Both of above print “Hello, World!” in UTF-16LE to standard error output with converting EOL generated by puts
to CR.
Creates a new GDBM
instance by opening a gdbm file named filename. If the file does not exist, a new file with file mode mode will be created. flags may be one of the following:
READER - open as a reader
WRITER - open as a writer
WRCREAT - open as a writer; if the database does not exist, create a new one
NEWDB - open as a writer; overwrite any existing databases
The values WRITER, WRCREAT and NEWDB may be combined with the following values by bitwise or:
SYNC - cause all database operations to be synchronized to the disk
NOLOCK - do not lock the database file
If no flags are specified, the GDBM
object will try to open the database file as a writer and will create it if it does not already exist (cf. flag WRCREAT
). If this fails (for instance, if another process has already opened the database as a reader), it will try to open the database file as a reader (cf. flag READER
).
Creates a new OpenStruct
object. By default, the resulting OpenStruct
object will have no attributes.
The optional hash
, if given, will generate attributes and values (can be a Hash
, an OpenStruct
or a Struct
). For example:
require "ostruct" hash = { "country" => "Australia", :capital => "Canberra" } data = OpenStruct.new(hash) data # => #<OpenStruct country="Australia", capital="Canberra">
Constructs a range using the given begin
and end
. If the exclude_end
parameter is omitted or is false
, the rng
will include the end object; otherwise, it will be excluded.
Constructs a new regular expression from pattern
, which can be either a String or a Regexp
(in which case that regexp’s options are propagated), and new options may not be specified (a change as of Ruby 1.8).
If options
is an Integer
, it should be one or more of the constants Regexp::EXTENDED
, Regexp::IGNORECASE
, and Regexp::MULTILINE
, or-ed together. Otherwise, if options
is not nil
or false
, the regexp will be case insensitive.
r1 = Regexp.new('^a-z+:\\s+\w+') #=> /^a-z+:\s+\w+/ r2 = Regexp.new('cat', true) #=> /cat/i r3 = Regexp.new(r2) #=> /cat/i r4 = Regexp.new('dog', Regexp::EXTENDED | Regexp::IGNORECASE) #=> /dog/ix
Calls allocate
to create a new object of class’s class, then invokes that object’s initialize
method, passing it args. This is the method that ends up getting called whenever an object is constructed using .new.
Creates a new anonymous (unnamed) class with the given superclass (or Object
if no parameter is given). You can give a class a name by assigning the class object to a constant.
If a block is given, it is passed the class object, and the block is evaluated in the context of this class like class_eval
.
fred = Class.new do def meth1 "hello" end def meth2 "bye" end end a = fred.new #=> #<#<Class:0x100381890>:0x100376b98> a.meth1 #=> "hello" a.meth2 #=> "bye"
Assign the class to a constant (name starting uppercase) if you want to treat it like a regular class.
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 serv
.
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
. 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.