Returns a new ipaddr built by masking IP address with the given prefixlen/netmask. (e.g. 8, 64, “255.255.255.0”, etc.)
Returns true if the ipaddr is a private address. IPv4 addresses in 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16 as defined in RFC 1918 and IPv6 Unique Local Addresses in fc00::/7 as defined in RFC 4193 are considered private. Private IPv4 addresses in the IPv4-mapped IPv6 address range are also considered private.
Returns a new ipaddr built by converting the IPv6 address into a native IPv4 address. If the IP address is not an IPv4-mapped or IPv4-compatible IPv6 address, returns self.
Returns the netmask in string format e.g. 255.255.0.0
Set
current netmask to given mask.
Terminates option parsing. Optional parameter arg
is a string pushed back to be the first non-option argument.
Puts option summary into to
and returns to
. Yields each line if a block is given.
to
Output destination, which must have method <<. Defaults to [].
width
Width of left side, defaults to @summary_width.
max
Maximum length allowed for left side, defaults to width
- 1.
indent
Indentation, defaults to @summary_indent.
Add separator in summary.
Return candidates for word
.
Parses environment variable env
or its uppercase with splitting like a shell.
env
defaults to the basename of the program.
Returns the full path name of the temporary file. This will be nil if unlink
has been called.
Creates a file in the underlying file system; returns a new File object based on that file.
With no block given and no arguments, creates and returns file whose:
Directory is the system temporary directory (system-dependent).
Generated filename is unique in that directory.
Permissions are 0600
; see File Permissions.
Mode is 'w+'
(read/write mode, positioned at the end).
The temporary file removal depends on the keyword argument anonymous
and whether a block is given or not. See the description about the anonymous
keyword argument later.
Example:
f = Tempfile.create # => #<File:/tmp/20220505-9795-17ky6f6> f.class # => File f.path # => "/tmp/20220505-9795-17ky6f6" f.stat.mode.to_s(8) # => "100600" f.close File.exist?(f.path) # => true File.unlink(f.path) File.exist?(f.path) # => false Tempfile.create {|f| f.puts "foo" f.rewind f.read # => "foo\n" f.path # => "/tmp/20240524-380207-oma0ny" File.exist?(f.path) # => true } # The file is removed at block exit. f = Tempfile.create(anonymous: true) # The file is already removed because anonymous f.path # => "/tmp/" (no filename since no file) f.puts "foo" f.rewind f.read # => "foo\n" f.close Tempfile.create(anonymous: true) {|f| # The file is already removed because anonymous f.path # => "/tmp/" (no filename since no file) f.puts "foo" f.rewind f.read # => "foo\n" }
The argument basename
, if given, may be one of the following:
A string: the generated filename begins with basename
:
Tempfile.create('foo') # => #<File:/tmp/foo20220505-9795-1gok8l9>
An array of two strings [prefix, suffix]
: the generated filename begins with prefix
and ends with suffix
:
Tempfile.create(%w/foo .jpg/) # => #<File:/tmp/foo20220505-17839-tnjchh.jpg>
With arguments basename
and tmpdir
, the file is created in the directory tmpdir
:
Tempfile.create('foo', '.') # => #<File:./foo20220505-9795-1emu6g8>
Keyword arguments mode
and options
are passed directly to the method File.open
:
The value given for mode
must be an integer and may be expressed as the logical OR of constants defined in File::Constants
.
For options
, see Open Options.
The keyword argument anonymous
specifies when the file is removed.
anonymous=false
(default) without a block: the file is not removed.
anonymous=false
(default) with a block: the file is removed after the block exits.
anonymous=true
without a block: the file is removed before returning.
anonymous=true
with a block: the file is removed before the block is called.
In the first case (anonymous=false
without a block), the file is not removed automatically. It should be explicitly closed. It can be used to rename to the desired filename. If the file is not needed, it should be explicitly removed.
The File#path
method of the created file object returns the temporary directory with a trailing slash when anonymous
is true.
When a block is given, it creates the file as described above, passes it to the block, and returns the block’s value. Before the returning, the file object is closed and the underlying file is removed:
Tempfile.create {|file| file.path } # => "/tmp/20220505-9795-rkists"
Implementation note:
The keyword argument +anonymous=true+ is implemented using FILE_SHARE_DELETE on Windows. O_TMPFILE is used on Linux.
Related: Tempfile.new
.
returns main ractor
return true if the current ractor is main ractor
Returns the main thread.
Terminates thr
and schedules another thread to be run, returning the terminated Thread
. If this is the main thread, or the last thread, exits the process.
Returns the status of thr
.
"sleep"
Returned if this thread is sleeping or waiting on I/O
"run"
When this thread is executing
"aborting"
If this thread is aborting
false
When this thread is terminated normally
nil
If terminated with an exception.
a = Thread.new { raise("die now") } b = Thread.new { Thread.stop } c = Thread.new { Thread.exit } d = Thread.new { sleep } d.kill #=> #<Thread:0x401b3678 aborting> a.status #=> nil b.status #=> "sleep" c.status #=> false d.status #=> "aborting" Thread.current.status #=> "run"
Returns internal information of TracePoint
.
The contents of the returned value are implementation-specific and may change in the future.
This method is only for debugging TracePoint
itself.
Returns the path of the file being executed.
Creates a new Pathname
object from the given string, path
, and returns pathname object.
In order to use this constructor, you must first require the Pathname
standard library extension.
require 'pathname' Pathname("/home/zzak") #=> #<Pathname:/home/zzak>
See also Pathname::new
for more information.
Equivalent to:
$stdout.putc(int)
See IO#putc
for important information regarding multi-byte characters.
Returns arg converted to a float. Numeric
types are converted directly, and with exception to String
and nil
, the rest are converted using arg.to_f
. Converting a String
with invalid characters will result in an ArgumentError
. Converting nil
generates a TypeError
. Exceptions can be suppressed by passing exception: false
.
Float(1) #=> 1.0 Float("123.456") #=> 123.456 Float("123.0_badstring") #=> ArgumentError: invalid value for Float(): "123.0_badstring" Float(nil) #=> TypeError: can't convert nil into Float Float("123.0_badstring", exception: false) #=> nil
Returns x/y
or arg
as a Rational
.
Rational(2, 3) #=> (2/3) Rational(5) #=> (5/1) Rational(0.5) #=> (1/2) Rational(0.3) #=> (5404319552844595/18014398509481984) Rational("2/3") #=> (2/3) Rational("0.3") #=> (3/10) Rational("10 cents") #=> ArgumentError Rational(nil) #=> TypeError Rational(1, nil) #=> TypeError Rational("10 cents", exception: false) #=> nil
Syntax of the string form:
string form = extra spaces , rational , extra spaces ; rational = [ sign ] , unsigned rational ; unsigned rational = numerator | numerator , "/" , denominator ; numerator = integer part | fractional part | integer part , fractional part ; denominator = digits ; integer part = digits ; fractional part = "." , digits , [ ( "e" | "E" ) , [ sign ] , digits ] ; sign = "-" | "+" ; digits = digit , { digit | "_" , digit } ; digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" ; extra spaces = ? \s* ? ;
See also String#to_r
.
Equivalent to ($_.dup).chop!
, except nil
is never returned. See String#chop!
. Available only when -p/-n command line option specified.