Results for: "Dir.chdir"

Calls the given block with each option; each option is a 2-element array containing:

Example:

require 'getoptlong'

options = GetoptLong.new(
  ['--xxx', '-x', GetoptLong::REQUIRED_ARGUMENT],
  ['--yyy', '-y', GetoptLong::OPTIONAL_ARGUMENT],
  ['--zzz', '-z',GetoptLong::NO_ARGUMENT]
)
puts "Original ARGV: #{ARGV}"
options.each do |option, argument|
  p [option, argument]
end
puts "Remaining ARGV: #{ARGV}"

Command line:

ruby each.rb -xxx Foo -x Bar --yyy Baz -y Bat --zzz

Output:

Original ARGV: ["-xxx", "Foo", "-x", "Bar", "--yyy", "Baz", "-y", "Bat", "--zzz"]
["--xxx", "xx"]
["--xxx", "Bar"]
["--yyy", "Baz"]
["--yyy", "Bat"]
["--zzz", ""]
Remaining ARGV: ["Foo"]

Returns the matched substring corresponding to the given argument.

When non-negative argument n is given, returns the matched substring for the nth match:

m = /(.)(.)(\d+)(\d)(\w)?/.match("THX1138.")
# => #<MatchData "HX1138" 1:"H" 2:"X" 3:"113" 4:"8" 5:nil>
m.match(0) # => "HX1138"
m.match(4) # => "8"
m.match(5) # => nil

When string or symbol argument name is given, returns the matched substring for the given name:

m = /(?<foo>.)(.)(?<bar>.+)/.match("hoge")
# => #<MatchData "hoge" foo:"h" bar:"ge">
m.match('foo') # => "h"
m.match(:bar)  # => "ge"

Like [], except that it accepts a default value for the store. If the key does not exist:

Raises an exception if called outside a transaction block.

Returns a fiber-local for the given key. If the key can’t be found, there are several options: With no other arguments, it will raise a KeyError exception; if default is given, then that will be returned; if the optional code block is specified, then that will be run and its result returned. See Thread#[] and Hash#fetch.

Equivalent to ($_.dup).chop!, except nil is never returned. See String#chop!. Available only when -p/-n command line option specified.

Equivalent to $_ = $_.chomp(string). See String#chomp. Available only when -p/-n command line option specified.

catch executes its block. If throw is not called, the block executes normally, and catch returns the value of the last expression evaluated.

catch(1) { 123 }            # => 123

If throw(tag2, val) is called, Ruby searches up its stack for a catch block whose tag has the same object_id as tag2. When found, the block stops executing and returns val (or nil if no second argument was given to throw).

catch(1) { throw(1, 456) }  # => 456
catch(1) { throw(1) }       # => nil

When tag is passed as the first argument, catch yields it as the parameter of the block.

catch(1) {|x| x + 2 }       # => 3

When no tag is given, catch yields a new unique object (as from Object.new) as the block parameter. This object can then be used as the argument to throw, and will match the correct catch block.

catch do |obj_A|
  catch do |obj_B|
    throw(obj_B, 123)
    puts "This puts is not reached"
  end

  puts "This puts is displayed"
  456
end

# => 456

catch do |obj_A|
  catch do |obj_B|
    throw(obj_A, 123)
    puts "This puts is still not reached"
  end

  puts "Now this puts is also not reached"
  456
end

# => 123

Each element in the returned enumerator is a 2-element array consisting of:

So that:

Example:

e = (0..10).chunk {|i| (i / 3).floor } # => #<Enumerator: ...>
# The enumerator elements.
e.next # => [0, [0, 1, 2]]
e.next # => [1, [3, 4, 5]]
e.next # => [2, [6, 7, 8]]
e.next # => [3, [9, 10]]

Method chunk is especially useful for an enumerable that is already sorted. This example counts words for each initial letter in a large array of words:

# Get sorted words from a web page.
url = 'https://raw.githubusercontent.com/eneko/data-repository/master/data/words.txt'
words = URI::open(url).readlines
# Make chunks, one for each letter.
e = words.chunk {|word| word.upcase[0] } # => #<Enumerator: ...>
# Display 'A' through 'F'.
e.each {|c, words| p [c, words.length]; break if c == 'F' }

Output:

["A", 17096]
["B", 11070]
["C", 19901]
["D", 10896]
["E", 8736]
["F", 6860]

You can use the special symbol :_alone to force an element into its own separate chuck:

a = [0, 0, 1, 1]
e = a.chunk{|i| i.even? ? :_alone : true }
e.to_a # => [[:_alone, [0]], [:_alone, [0]], [true, [1, 1]]]

For example, you can put each line that contains a URL into its own chunk:

pattern = /http/
open(filename) { |f|
  f.chunk { |line| line =~ pattern ? :_alone : true }.each { |key, lines|
    pp lines
  }
}

You can use the special symbol :_separator or nil to force an element to be ignored (not included in any chunk):

a = [0, 0, -1, 1, 1]
e = a.chunk{|i| i < 0 ? :_separator : true }
e.to_a # => [[true, [0, 0]], [true, [1, 1]]]

Note that the separator does end the chunk:

a = [0, 0, -1, 1, -1, 1]
e = a.chunk{|i| i < 0 ? :_separator : true }
e.to_a # => [[true, [0, 0]], [true, [1]], [true, [1]]]

For example, the sequence of hyphens in svn log can be eliminated as follows:

sep = "-"*72 + "\n"
IO.popen("svn log README") { |f|
  f.chunk { |line|
    line != sep || nil
  }.each { |_, lines|
    pp lines
  }
}
#=> ["r20018 | knu | 2008-10-29 13:20:42 +0900 (Wed, 29 Oct 2008) | 2 lines\n",
#    "\n",
#    "* README, README.ja: Update the portability section.\n",
#    "\n"]
#   ["r16725 | knu | 2008-05-31 23:34:23 +0900 (Sat, 31 May 2008) | 2 lines\n",
#    "\n",
#    "* README, README.ja: Add a note about default C flags.\n",
#    "\n"]
#   ...

Paragraphs separated by empty lines can be parsed as follows:

File.foreach("README").chunk { |line|
  /\A\s*\z/ !~ line || nil
}.each { |_, lines|
  pp lines
}

Returns an enumerator object generated from this enumerator and given enumerables.

e = (1..3).chain([4, 5])
e.to_a #=> [1, 2, 3, 4, 5]
No documentation available

Checks the status of the child process specified by pid. Returns nil if the process is still alive.

If the process is not alive, and raise was true, a PTY::ChildExited exception will be raised. Otherwise it will return a Process::Status instance.

pid

The process id of the process to check

raise

If true and the process identified by pid is no longer alive a PTY::ChildExited is raised.

Returns true if filepath points to a character device, false otherwise.

File.chardev?($stdin)     # => true
File.chardev?('t.txt')     # => false

Invokes the block with a Benchmark::Report object, which may be used to collect and report on the results of individual benchmark tests. Reserves label_width leading spaces for labels on each line. Prints caption at the top of the report, and uses format to format each line. (Note: caption must contain a terminating newline character, see the default Benchmark::Tms::CAPTION for an example.)

Returns an array of Benchmark::Tms objects.

If the block returns an array of Benchmark::Tms objects, these will be used to format additional lines of output. If labels parameter are given, these are used to label these extra lines.

Note: Other methods provide a simpler interface to this one, and are suitable for nearly all benchmarking requirements. See the examples in Benchmark, and the bm and bmbm methods.

Example:

require 'benchmark'
include Benchmark          # we need the CAPTION and FORMAT constants

n = 5000000
Benchmark.benchmark(CAPTION, 7, FORMAT, ">total:", ">avg:") do |x|
  tf = x.report("for:")   { for i in 1..n; a = "1"; end }
  tt = x.report("times:") { n.times do   ; a = "1"; end }
  tu = x.report("upto:")  { 1.upto(n) do ; a = "1"; end }
  [tf+tt+tu, (tf+tt+tu)/3]
end

Generates:

              user     system      total        real
for:      0.970000   0.000000   0.970000 (  0.970493)
times:    0.990000   0.000000   0.990000 (  0.989542)
upto:     0.970000   0.000000   0.970000 (  0.972854)
>total:   2.930000   0.000000   2.930000 (  2.932889)
>avg:     0.976667   0.000000   0.976667 (  0.977630)

Invokes the block with a Benchmark::Report object, which may be used to collect and report on the results of individual benchmark tests. Reserves label_width leading spaces for labels on each line. Prints caption at the top of the report, and uses format to format each line. (Note: caption must contain a terminating newline character, see the default Benchmark::Tms::CAPTION for an example.)

Returns an array of Benchmark::Tms objects.

If the block returns an array of Benchmark::Tms objects, these will be used to format additional lines of output. If labels parameter are given, these are used to label these extra lines.

Note: Other methods provide a simpler interface to this one, and are suitable for nearly all benchmarking requirements. See the examples in Benchmark, and the bm and bmbm methods.

Example:

require 'benchmark'
include Benchmark          # we need the CAPTION and FORMAT constants

n = 5000000
Benchmark.benchmark(CAPTION, 7, FORMAT, ">total:", ">avg:") do |x|
  tf = x.report("for:")   { for i in 1..n; a = "1"; end }
  tt = x.report("times:") { n.times do   ; a = "1"; end }
  tu = x.report("upto:")  { 1.upto(n) do ; a = "1"; end }
  [tf+tt+tu, (tf+tt+tu)/3]
end

Generates:

              user     system      total        real
for:      0.970000   0.000000   0.970000 (  0.970493)
times:    0.990000   0.000000   0.990000 (  0.989542)
upto:     0.970000   0.000000   0.970000 (  0.972854)
>total:   2.930000   0.000000   2.930000 (  2.932889)
>avg:     0.976667   0.000000   0.976667 (  0.977630)

Changes permissions on the entries at the paths given in list (a single path or an array of paths) to the permissions given by mode; returns list if it is an array, [list] otherwise:

Argument list or its elements should be interpretable as paths.

Argument mode may be either an integer or a string:

Keyword arguments:

Related: FileUtils.chmod_R.

Changes permissions on the entries at the paths given in list (a single path or an array of paths) to the permissions given by mode; returns list if it is an array, [list] otherwise:

Argument list or its elements should be interpretable as paths.

Argument mode may be either an integer or a string:

Keyword arguments:

Related: FileUtils.chmod_R.

Like FileUtils.chmod, but changes permissions recursively.

Like FileUtils.chmod, but changes permissions recursively.

Changes the owner and group on the entries at the paths given in list (a single path or an array of paths) to the given user and group; returns list if it is an array, [list] otherwise:

Argument list or its elements should be interpretable as paths.

User and group:

Examples:

# One path.
# User and group as string names.
File.stat('src0.txt').uid # => 1004
File.stat('src0.txt').gid # => 1004
FileUtils.chown('user2', 'group1', 'src0.txt')
File.stat('src0.txt').uid # => 1006
File.stat('src0.txt').gid # => 1005

# User and group as uid and gid.
FileUtils.chown(1004, 1004, 'src0.txt')
File.stat('src0.txt').uid # => 1004
File.stat('src0.txt').gid # => 1004

# Array of paths.
FileUtils.chown(1006, 1005, ['src0.txt', 'src0.dat'])

# Directory (not recursive).
FileUtils.chown('user2', 'group1', '.')

Keyword arguments:

Related: FileUtils.chown_R.

Changes the owner and group on the entries at the paths given in list (a single path or an array of paths) to the given user and group; returns list if it is an array, [list] otherwise:

Argument list or its elements should be interpretable as paths.

User and group:

Examples:

# One path.
# User and group as string names.
File.stat('src0.txt').uid # => 1004
File.stat('src0.txt').gid # => 1004
FileUtils.chown('user2', 'group1', 'src0.txt')
File.stat('src0.txt').uid # => 1006
File.stat('src0.txt').gid # => 1005

# User and group as uid and gid.
FileUtils.chown(1004, 1004, 'src0.txt')
File.stat('src0.txt').uid # => 1004
File.stat('src0.txt').gid # => 1004

# Array of paths.
FileUtils.chown(1006, 1005, ['src0.txt', 'src0.dat'])

# Directory (not recursive).
FileUtils.chown('user2', 'group1', '.')

Keyword arguments:

Related: FileUtils.chown_R.

Like FileUtils.chown, but changes owner and group recursively.

Like FileUtils.chown, but changes owner and group recursively.

Updates modification times (mtime) and access times (atime) of the entries given by the paths in list (a single path or an array of paths); returns list if it is an array, [list] otherwise.

By default, creates an empty file for any path to a non-existent entry; use keyword argument nocreate to raise an exception instead.

Argument list or its elements should be interpretable as paths.

Examples:

# Single path.
f = File.new('src0.txt') # Existing file.
f.atime # => 2022-06-10 11:11:21.200277 -0700
f.mtime # => 2022-06-10 11:11:21.200277 -0700
FileUtils.touch('src0.txt')
f = File.new('src0.txt')
f.atime # => 2022-06-11 08:28:09.8185343 -0700
f.mtime # => 2022-06-11 08:28:09.8185343 -0700

# Array of paths.
FileUtils.touch(['src0.txt', 'src0.dat'])

Keyword arguments:

Related: FileUtils.uptodate?.

Updates modification times (mtime) and access times (atime) of the entries given by the paths in list (a single path or an array of paths); returns list if it is an array, [list] otherwise.

By default, creates an empty file for any path to a non-existent entry; use keyword argument nocreate to raise an exception instead.

Argument list or its elements should be interpretable as paths.

Examples:

# Single path.
f = File.new('src0.txt') # Existing file.
f.atime # => 2022-06-10 11:11:21.200277 -0700
f.mtime # => 2022-06-10 11:11:21.200277 -0700
FileUtils.touch('src0.txt')
f = File.new('src0.txt')
f.atime # => 2022-06-11 08:28:09.8185343 -0700
f.mtime # => 2022-06-11 08:28:09.8185343 -0700

# Array of paths.
FileUtils.touch(['src0.txt', 'src0.dat'])

Keyword arguments:

Related: FileUtils.uptodate?.

Set the changed state of this object. Notifications will be sent only if the changed state is true.

state

Boolean indicating the changed state of this Observable.

Search took: 7ms  ·  Total Results: 1079