Changes permission bits on file to the bit pattern represented by mode_int. Actual effects are platform dependent; on Unix systems, see chmod(2)
for details. Follows symbolic links. Also see File#lchmod.
f = File.new("out", "w"); f.chmod(0644) #=> 0
Creates an infinite enumerator from any block, just called over and over. The result of the previous iteration is passed to the next one. If initial
is provided, it is passed to the first iteration, and becomes the first element of the enumerator; if it is not provided, the first iteration receives nil
, and its result becomes the first element of the iterator.
Raising StopIteration
from the block stops an iteration.
Enumerator.produce(1, &:succ) # => enumerator of 1, 2, 3, 4, .... Enumerator.produce { rand(10) } # => infinite random number sequence ancestors = Enumerator.produce(node) { |prev| node = prev.parent or raise StopIteration } enclosing_section = ancestors.find { |n| n.type == :section }
Using ::produce
together with Enumerable
methods like Enumerable#detect
, Enumerable#slice_after
, Enumerable#take_while
can provide Enumerator-based alternatives for while
and until
cycles:
# Find next Tuesday require "date" Enumerator.produce(Date.today, &:succ).detect(&:tuesday?) # Simple lexer: require "strscan" scanner = StringScanner.new("7+38/6") PATTERN = %r{\d+|[-/+*]} Enumerator.produce { scanner.scan(PATTERN) }.slice_after { scanner.eos? }.first # => ["7", "+", "38", "/", "6"]
Returns an integer representing the mode settings for exception handling and rounding.
These modes control exception handling:
BigDecimal::EXCEPTION_NaN.
BigDecimal::EXCEPTION_INFINITY.
BigDecimal::EXCEPTION_UNDERFLOW.
BigDecimal::EXCEPTION_OVERFLOW.
BigDecimal::EXCEPTION_ZERODIVIDE.
BigDecimal::EXCEPTION_ALL.
Values for setting
for exception handling:
true
: sets the given mode
to true
.
false
: sets the given mode
to false
.
nil
: does not modify the mode settings.
You can use method BigDecimal.save_exception_mode
to temporarily change, and then automatically restore, exception modes.
For clarity, some examples below begin by setting all exception modes to false
.
This mode controls the way rounding is to be performed:
BigDecimal::ROUND_MODE
You can use method BigDecimal.save_rounding_mode
to temporarily change, and then automatically restore, the rounding mode.
NaNs
Mode BigDecimal::EXCEPTION_NaN controls behavior when a BigDecimal NaN is created.
Settings:
false
(default): Returns BigDecimal('NaN')
.
true
: Raises FloatDomainError
.
Examples:
BigDecimal.mode(BigDecimal::EXCEPTION_ALL, false) # => 0 BigDecimal('NaN') # => NaN BigDecimal.mode(BigDecimal::EXCEPTION_NaN, true) # => 2 BigDecimal('NaN') # Raises FloatDomainError
Infinities
Mode BigDecimal::EXCEPTION_INFINITY controls behavior when a BigDecimal Infinity or -Infinity is created. Settings:
false
(default): Returns BigDecimal('Infinity')
or BigDecimal('-Infinity')
.
true
: Raises FloatDomainError
.
Examples:
BigDecimal.mode(BigDecimal::EXCEPTION_ALL, false) # => 0 BigDecimal('Infinity') # => Infinity BigDecimal('-Infinity') # => -Infinity BigDecimal.mode(BigDecimal::EXCEPTION_INFINITY, true) # => 1 BigDecimal('Infinity') # Raises FloatDomainError BigDecimal('-Infinity') # Raises FloatDomainError
Underflow
Mode BigDecimal::EXCEPTION_UNDERFLOW controls behavior when a BigDecimal underflow occurs. Settings:
false
(default): Returns BigDecimal('0')
or BigDecimal('-Infinity')
.
true
: Raises FloatDomainError
.
Examples:
BigDecimal.mode(BigDecimal::EXCEPTION_ALL, false) # => 0 def flow_under x = BigDecimal('0.1') 100.times { x *= x } end flow_under # => 100 BigDecimal.mode(BigDecimal::EXCEPTION_UNDERFLOW, true) # => 4 flow_under # Raises FloatDomainError
Overflow
Mode BigDecimal::EXCEPTION_OVERFLOW controls behavior when a BigDecimal overflow occurs. Settings:
false
(default): Returns BigDecimal('Infinity')
or BigDecimal('-Infinity')
.
true
: Raises FloatDomainError
.
Examples:
BigDecimal.mode(BigDecimal::EXCEPTION_ALL, false) # => 0 def flow_over x = BigDecimal('10') 100.times { x *= x } end flow_over # => 100 BigDecimal.mode(BigDecimal::EXCEPTION_OVERFLOW, true) # => 1 flow_over # Raises FloatDomainError
Zero Division
Mode BigDecimal::EXCEPTION_ZERODIVIDE controls behavior when a zero-division occurs. Settings:
false
(default): Returns BigDecimal('Infinity')
or BigDecimal('-Infinity')
.
true
: Raises FloatDomainError
.
Examples:
BigDecimal.mode(BigDecimal::EXCEPTION_ALL, false) # => 0 one = BigDecimal('1') zero = BigDecimal('0') one / zero # => Infinity BigDecimal.mode(BigDecimal::EXCEPTION_ZERODIVIDE, true) # => 16 one / zero # Raises FloatDomainError
All Exceptions
Mode BigDecimal::EXCEPTION_ALL controls all of the above:
BigDecimal.mode(BigDecimal::EXCEPTION_ALL, false) # => 0 BigDecimal.mode(BigDecimal::EXCEPTION_ALL, true) # => 23
Rounding
Mode BigDecimal::ROUND_MODE controls the way rounding is to be performed; its setting
values are:
ROUND_UP
: Round away from zero. Aliased as :up
.
ROUND_DOWN
: Round toward zero. Aliased as :down
and :truncate
.
ROUND_HALF_UP
: Round toward the nearest neighbor; if the neighbors are equidistant, round away from zero. Aliased as :half_up
and :default
.
ROUND_HALF_DOWN
: Round toward the nearest neighbor; if the neighbors are equidistant, round toward zero. Aliased as :half_down
.
ROUND_HALF_EVEN
(Banker’s rounding): Round toward the nearest neighbor; if the neighbors are equidistant, round toward the even neighbor. Aliased as :half_even
and :banker
.
ROUND_CEILING
: Round toward positive infinity. Aliased as :ceiling
and :ceil
.
ROUND_FLOOR
: Round toward negative infinity. Aliased as :floor:
.
Divides by the specified value, and returns the quotient and modulus as BigDecimal
numbers. The quotient is rounded towards negative infinity.
For example:
require 'bigdecimal' a = BigDecimal("42") b = BigDecimal("9") q, m = a.divmod(b) c = q * b + m a == c #=> true
The quotient q is (a/b).floor, and the modulus is the amount that must be added to q * b to get a.
Sets the stream’s data mode as binary (see Data Mode).
A stream’s data mode may not be changed from binary to text.
Returns true
if the stream is on binary mode, false
otherwise. See Data Mode.
Sets the data mode in self
to binary mode; see Data Mode.
Puts ARGF
into binary mode. Once a stream is in binary mode, it cannot be reset to non-binary mode. This option has the following effects:
Newline conversion is disabled.
Encoding
conversion is disabled.
Content is treated as ASCII-8BIT.
Returns true if ARGF
is being read in binary mode; false otherwise. To enable binary mode use ARGF.binmode
.
For example:
ARGF.binmode? #=> false ARGF.binmode ARGF.binmode? #=> true
Returns a BubbleBabble encoded version of a given string.
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:
Modifies each entry that is a regular file using File.chmod
.
Modifies each entry that is a symbolic link using File.lchmod
.
Argument list
or its elements should be interpretable as paths.
Argument mode
may be either an integer or a string:
Integer mode
: represents the permission bits to be set:
FileUtils.chmod(0755, 'src0.txt') FileUtils.chmod(0644, ['src0.txt', 'src0.dat'])
String mode
: represents the permissions to be set:
The string is of the form [targets][[operator][perms[,perms]]
, where:
targets
may be any combination of these letters:
'u'
: permissions apply to the file’s owner.
'g'
: permissions apply to users in the file’s group.
'o'
: permissions apply to other users not in the file’s group.
'a'
(the default): permissions apply to all users.
operator
may be one of these letters:
'+'
: adds permissions.
'-'
: removes permissions.
'='
: sets (replaces) permissions.
perms
(may be repeated, with separating commas) may be any combination of these letters:
'r'
: Read.
'w'
: Write.
'x'
: Execute (search, for a directory).
'X'
: Search (for a directories only; must be used with '+'
)
's'
: Uid or gid.
't'
: Sticky bit.
Examples:
FileUtils.chmod('u=wrx,go=rx', 'src1.txt') FileUtils.chmod('u=wrx,go=rx', '/usr/bin/ruby')
Keyword arguments:
noop: true
- does not change permissions; returns nil
.
verbose: true
- prints an equivalent command:
FileUtils.chmod(0755, 'src0.txt', noop: true, verbose: true) FileUtils.chmod(0644, ['src0.txt', 'src0.dat'], noop: true, verbose: true) FileUtils.chmod('u=wrx,go=rx', 'src1.txt', noop: true, verbose: true) FileUtils.chmod('u=wrx,go=rx', '/usr/bin/ruby', noop: true, verbose: true)
Output:
chmod 755 src0.txt chmod 644 src0.txt src0.dat chmod u=wrx,go=rx src1.txt chmod u=wrx,go=rx /usr/bin/ruby
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:
Modifies each entry that is a regular file using File.chmod
.
Modifies each entry that is a symbolic link using File.lchmod
.
Argument list
or its elements should be interpretable as paths.
Argument mode
may be either an integer or a string:
Integer mode
: represents the permission bits to be set:
FileUtils.chmod(0755, 'src0.txt') FileUtils.chmod(0644, ['src0.txt', 'src0.dat'])
String mode
: represents the permissions to be set:
The string is of the form [targets][[operator][perms[,perms]]
, where:
targets
may be any combination of these letters:
'u'
: permissions apply to the file’s owner.
'g'
: permissions apply to users in the file’s group.
'o'
: permissions apply to other users not in the file’s group.
'a'
(the default): permissions apply to all users.
operator
may be one of these letters:
'+'
: adds permissions.
'-'
: removes permissions.
'='
: sets (replaces) permissions.
perms
(may be repeated, with separating commas) may be any combination of these letters:
'r'
: Read.
'w'
: Write.
'x'
: Execute (search, for a directory).
'X'
: Search (for a directories only; must be used with '+'
)
's'
: Uid or gid.
't'
: Sticky bit.
Examples:
FileUtils.chmod('u=wrx,go=rx', 'src1.txt') FileUtils.chmod('u=wrx,go=rx', '/usr/bin/ruby')
Keyword arguments:
noop: true
- does not change permissions; returns nil
.
verbose: true
- prints an equivalent command:
FileUtils.chmod(0755, 'src0.txt', noop: true, verbose: true) FileUtils.chmod(0644, ['src0.txt', 'src0.dat'], noop: true, verbose: true) FileUtils.chmod('u=wrx,go=rx', 'src1.txt', noop: true, verbose: true) FileUtils.chmod('u=wrx,go=rx', '/usr/bin/ruby', noop: true, verbose: true)
Output:
chmod 755 src0.txt chmod 644 src0.txt src0.dat chmod u=wrx,go=rx src1.txt chmod u=wrx,go=rx /usr/bin/ruby
Related: FileUtils.chmod_R
.
Like FileUtils.chmod
, but changes permissions recursively.
Like FileUtils.chmod
, but changes permissions recursively.
Calls the block with each repeated permutation of length n
of the elements of self
; each permutation is an Array; returns self
. The order of the permutations is indeterminate.
When a block and a positive Integer argument n
are given, calls the block with each n
-tuple repeated permutation of the elements of self
. The number of permutations is self.size**n
.
n
= 1:
a = [0, 1, 2] a.repeated_permutation(1) {|permutation| p permutation }
Output:
[0] [1] [2]
n
= 2:
a.repeated_permutation(2) {|permutation| p permutation }
Output:
[0, 0] [0, 1] [0, 2] [1, 0] [1, 1] [1, 2] [2, 0] [2, 1] [2, 2]
If n
is zero, calls the block once with an empty Array.
If n
is negative, does not call the block:
a.repeated_permutation(-1) {|permutation| fail 'Cannot happen' }
Returns a new Enumerator if no block given:
a = [0, 1, 2] a.repeated_permutation(2) # => #<Enumerator: [0, 1, 2]:permutation(2)>
Using Enumerators, it’s convenient to show the permutations and counts for some values of n
:
e = a.repeated_permutation(0) e.size # => 1 e.to_a # => [[]] e = a.repeated_permutation(1) e.size # => 3 e.to_a # => [[0], [1], [2]] e = a.repeated_permutation(2) e.size # => 9 e.to_a # => [[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2], [2, 0], [2, 1], [2, 2]]
Calls the block with each repeated combination of length n
of the elements of self
; each combination is an Array; returns self
. The order of the combinations is indeterminate.
When a block and a positive Integer argument n
are given, calls the block with each n
-tuple repeated combination of the elements of self
. The number of combinations is (n+1)(n+2)/2
.
n
= 1:
a = [0, 1, 2] a.repeated_combination(1) {|combination| p combination }
Output:
[0] [1] [2]
n
= 2:
a.repeated_combination(2) {|combination| p combination }
Output:
[0, 0] [0, 1] [0, 2] [1, 1] [1, 2] [2, 2]
If n
is zero, calls the block once with an empty Array.
If n
is negative, does not call the block:
a.repeated_combination(-1) {|combination| fail 'Cannot happen' }
Returns a new Enumerator if no block given:
a = [0, 1, 2] a.repeated_combination(2) # => #<Enumerator: [0, 1, 2]:combination(2)>
Using Enumerators, it’s convenient to show the combinations and counts for some values of n
:
e = a.repeated_combination(0) e.size # => 1 e.to_a # => [[]] e = a.repeated_combination(1) e.size # => 3 e.to_a # => [[0], [1], [2]] e = a.repeated_combination(2) e.size # => 6 e.to_a # => [[0, 0], [0, 1], [0, 2], [1, 1], [1, 2], [2, 2]]
Like backtrace
, but returns each line of the execution stack as a Thread::Backtrace::Location
. Accepts the same arguments as backtrace
.
f = Fiber.new { Fiber.yield } f.resume loc = f.backtrace_locations.first loc.label #=> "yield" loc.path #=> "test.rb" loc.lineno #=> 1
Returns any backtrace associated with the exception. This method is similar to Exception#backtrace
, but the backtrace is an array of Thread::Backtrace::Location
.
This method is not affected by Exception#set_backtrace()
.
Requests a connection to be made on the given remote_sockaddr
after O_NONBLOCK is set for the underlying file descriptor. Returns 0 if successful, otherwise an exception is raised.
# +remote_sockaddr+ - the +struct+ sockaddr contained in a string or Addrinfo object
# Pull down Google's web page require 'socket' include Socket::Constants socket = Socket.new(AF_INET, SOCK_STREAM, 0) sockaddr = Socket.sockaddr_in(80, 'www.google.com') begin # emulate blocking connect socket.connect_nonblock(sockaddr) rescue IO::WaitWritable IO.select(nil, [socket]) # wait 3-way handshake completion begin socket.connect_nonblock(sockaddr) # check connection failure rescue Errno::EISCONN end end socket.write("GET / HTTP/1.0\r\n\r\n") results = socket.read
Refer to Socket#connect
for the exceptions that may be thrown if the call to connect_nonblock fails.
Socket#connect_nonblock
may raise any error corresponding to connect(2) failure, including Errno::EINPROGRESS.
If the exception is Errno::EINPROGRESS, it is extended by IO::WaitWritable
. So IO::WaitWritable
can be used to rescue the exceptions for retrying connect_nonblock.
By specifying a keyword argument exception to false
, you can indicate that connect_nonblock
should not raise an IO::WaitWritable
exception, but return the symbol :wait_writable
instead.
# Socket#connect
Returns the value that determines whether unconverted fields are to be available; used for parsing; see {Option unconverted_fields
}:
CSV.new('').unconverted_fields? # => nil