Removes the last List
.
Returns option summary list.
Parses command line arguments argv
in order. When a block is given, each non-option argument is yielded. When optional into
keyword argument is provided, the parsed option values are stored there via []=
method (so it can be Hash
, or OpenStruct
, or other similar object).
Returns the rest of argv
left unparsed.
Same as order
, but removes switches destructively. Non-option arguments remain in argv
.
Wrapper method for getopts.rb.
params = ARGV.getopts("ab:", "foo", "bar:", "zot:Z;zot option") # params["a"] = true # -a # params["b"] = "1" # -b1 # params["foo"] = "1" # --foo # params["bar"] = "x" # --bar x # params["zot"] = "z" # --zot Z
Option symbolize_names
(boolean) specifies whether returned Hash
keys should be Symbols; defaults to false
(use Strings).
params = ARGV.getopts("ab:", "foo", "bar:", "zot:Z;zot option", symbolize_names: true) # params[:a] = true # -a # params[:b] = "1" # -b1 # params[:foo] = "1" # --foo # params[:bar] = "x" # --bar x # params[:zot] = "z" # --zot Z
Returns Hash
representation of the data object.
Measure = Data.define(:amount, :unit) distance = Measure[10, 'km'] distance.to_h #=> {:amount=>10, :unit=>"km"}
Like Enumerable#to_h
, if the block is provided, it is expected to produce key-value pairs to construct a hash:
distance.to_h { |name, val| [name.to_s, val.to_s] } #=> {"amount"=>"10", "unit"=>"km"}
Note that there is a useful symmetry between to_h
and initialize:
distance2 = Measure.new(**distance.to_h) #=> #<data Measure amount=10, unit="km"> distance2 == distance #=> true
Returns the values in self
as an array, to use in pattern matching:
Measure = Data.define(:amount, :unit) distance = Measure[10, 'km'] distance.deconstruct #=> [10, "km"] # usage case distance in n, 'km' # calls #deconstruct underneath puts "It is #{n} kilometers away" else puts "Don't know how to handle it" end # prints "It is 10 kilometers away"
Or, with checking the class, too:
case distance in Measure(n, 'km') puts "It is #{n} kilometers away" # ... end
Returns a string representation of self
:
Measure = Data.define(:amount, :unit) distance = Measure[10, 'km'] p distance # uses #inspect underneath #<data Measure amount=10, unit="km"> puts distance # uses #to_s underneath, same representation #<data Measure amount=10, unit="km">
Returns the regexp that produced the match:
m = /a.*b/.match("abc") # => #<MatchData "ab"> m.regexp # => /a.*b/
Returns the array of matches:
m = /(.)(.)(\d+)(\d)/.match("THX1138.") # => #<MatchData "HX1138" 1:"H" 2:"X" 3:"113" 4:"8"> m.to_a # => ["HX1138", "H", "X", "113", "8"]
Related: MatchData#captures
.
Returns the array of captures, which are all matches except m[0]
:
m = /(.)(.)(\d+)(\d)/.match("THX1138.") # => #<MatchData "HX1138" 1:"H" 2:"X" 3:"113" 4:"8"> m[0] # => "HX1138" m.captures # => ["H", "X", "113", "8"]
Related: MatchData.to_a
.
Returns the matched string:
m = /(.)(.)(\d+)(\d)/.match("THX1138.") # => #<MatchData "HX1138" 1:"H" 2:"X" 3:"113" 4:"8"> m.to_s # => "HX1138" m = /(?<foo>.)(.)(?<bar>.+)/.match("hoge") # => #<MatchData "hoge" foo:"h" bar:"ge"> m.to_s # => "hoge"
Related: MatchData.inspect
.
Returns the target string if it was frozen; otherwise, returns a frozen copy of the target string:
m = /(.)(.)(\d+)(\d)/.match("THX1138.") # => #<MatchData "HX1138" 1:"H" 2:"X" 3:"113" 4:"8"> m.string # => "THX1138."
Returns the array of captures, which are all matches except m[0]
:
m = /(.)(.)(\d+)(\d)/.match("THX1138.") # => #<MatchData "HX1138" 1:"H" 2:"X" 3:"113" 4:"8"> m[0] # => "HX1138" m.captures # => ["H", "X", "113", "8"]
Related: MatchData.to_a
.
This is a convenience method which is same as follows:
begin q = PrettyPrint.new(output, maxwidth, newline, &genspace) ... q.flush output end
This says “you can break a line here if necessary”, and a width
-column text sep
is inserted if a line is not broken at the point.
If sep
is not specified, “ ” is used.
If width
is not specified, sep.length
is used. You will have to specify this when sep
is a multibyte character, for example.
Increases left margin after newline with indent
for line breaks added in the block.
Looks up the first IP address for name
.
Looks up all IP address for name
.
Looks up the first IP address for name
.
Looks up all IP address for name
.
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
.