With a block given, may call the block, depending on the value of argument count
; count
must be an integer-convertible object, or nil
.
When count
is positive, calls the block with each element, then does so repeatedly, until it has done so count
times; returns nil
:
output = [] [0, 1].cycle(2) {|element| output.push(element) } # => nil output # => [0, 1, 0, 1]
When count
is zero or negative, does not call the block:
[0, 1].cycle(0) {|element| fail 'Cannot happen' } # => nil [0, 1].cycle(-1) {|element| fail 'Cannot happen' } # => nil
When count
is nil
, cycles forever:
# Prints 0 and 1 forever. [0, 1].cycle {|element| puts element } [0, 1].cycle(nil) {|element| puts element }
With no block given, returns a new Enumerator
.
Related: see Methods for Iterating.
Shuffles all elements in self
into a random order, as selected by the object given by the keyword argument random
. Returns self
:
a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] a.shuffle! # => [5, 3, 8, 7, 6, 1, 9, 4, 2, 0] a.shuffle! # => [9, 4, 0, 6, 2, 8, 1, 5, 3, 7]
Duplicate elements are included:
a = [0, 1, 0, 1, 0, 1, 0, 1, 0, 1] a.shuffle! # => [1, 0, 0, 1, 1, 0, 1, 0, 0, 1] a.shuffle! # => [0, 1, 0, 1, 1, 0, 1, 0, 1, 0]
The object given with the keyword argument random
is used as the random number generator.
Related: see Methods for Assigning.
Returns a new array containing all elements from self
in a random order, as selected by the object given by the keyword argument random
:
a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] a.shuffle # => [0, 8, 1, 9, 6, 3, 4, 7, 2, 5] a.shuffle # => [8, 9, 0, 5, 1, 2, 6, 4, 7, 3]
Duplicate elements are included:
a = [0, 1, 0, 1, 0, 1, 0, 1, 0, 1] a.shuffle # => [1, 0, 1, 1, 0, 0, 1, 0, 0, 1] a.shuffle # => [1, 1, 0, 0, 0, 1, 1, 0, 0, 1]
The object given with the keyword argument random
is used as the random number generator.
Related: see Methods for Fetching.
Returns random elements from self
, as selected by the object given by the keyword argument random
.
With no argument count
given, returns one random element from self
:
a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] a.sample # => 3 a.sample # => 8
Returns nil
if self
is empty:
[].sample # => nil
With a non-negative numeric argument count
given, returns a new array containing count
random elements from self
:
a.sample(3) # => [8, 9, 2] a.sample(6) # => [9, 6, 0, 3, 1, 4]
The order of the result array is unrelated to the order of self
.
Returns a new empty array if self
is empty:
[].sample(4) # => []
May return duplicates in self
:
a = [1, 1, 1, 2, 2, 3] a.sample(a.size) # => [1, 1, 3, 2, 1, 2]
Returns no more than a.size
elements (because no new duplicates are introduced):
a.sample(50) # => [6, 4, 1, 8, 5, 9, 0, 2, 3, 7]
The object given with the keyword argument random
is used as the random number generator:
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] a.sample(random: Random.new(1)) # => 6 a.sample(4, random: Random.new(1)) # => [6, 10, 9, 2]
Related: see Methods for Fetching.
Returns true
if self
is an odd number, false
otherwise.
Returns the argument (angle) for self
in radians; see polar coordinates:
Complex.polar(3, Math::PI/2).arg # => 1.57079632679489660
If self
was created with rectangular coordinates, the returned value is computed, and may be inexact:
Complex.polar(1, 1.0/3).arg # => 0.33333333333333326
Returns zero if self
is positive, Math::PI otherwise.
Escapes str
so that it can be safely used in a Bourne shell command line.
See Shellwords.shellescape
for details.
Returns the count of characters (not bytes) in self
:
'foo'.length # => 3 'тест'.length # => 4 'こんにちは'.length # => 5
Contrast with String#bytesize
:
'foo'.bytesize # => 3 'тест'.bytesize # => 8 'こんにちは'.bytesize # => 15
Returns a printable version of self
, enclosed in double-quotes, with special characters escaped, and with non-printing characters replaced by hexadecimal notation:
"hello \n ''".dump # => "\"hello \\n ''\"" "\f\x00\xff\\\"".dump # => "\"\\f\\x00\\xFF\\\\\\\"\""
Related: String#undump
(inverse of String#dump
).
Returns an array of the codepoints in self
; each codepoint is the integer value for a character:
'hello'.codepoints # => [104, 101, 108, 108, 111] 'тест'.codepoints # => [1090, 1077, 1089, 1090] 'こんにちは'.codepoints # => [12371, 12435, 12395, 12385, 12399]
Returns a copy of self
with characters specified by selectors
removed (see Multiple Character Selectors):
"hello".delete "l","lo" #=> "heo" "hello".delete "lo" #=> "he" "hello".delete "aeiou", "^e" #=> "hell" "hello".delete "ej-m" #=> "ho"
Like String#delete
, but modifies self
in place. Returns self
if any changes were made, nil
otherwise.
Returns a frozen string equal to self
.
The returned string is self
if and only if all of the following are true:
self
is already frozen.
self
is an instance of String (rather than of a subclass of String)
self
has no instance variables set on it.
Otherwise, the returned string is a frozen copy of self
.
Returning self
, when possible, saves duplicating self
; see Data deduplication.
It may also save duplicating other, already-existing, strings:
s0 = 'foo' s1 = 'foo' s0.object_id == s1.object_id # => false (-s0).object_id == (-s1).object_id # => true
Note that method -@
is convenient for defining a constant:
FileName = -'config/database.yml'
While its alias dedup
is better suited for chaining:
'foo'.dedup.gsub!('o')
Related: see Freezing/Unfreezing.
Returns 0 if self
is positive, Math::PI otherwise.
Returns the file descriptor used in dir.
d = Dir.new('..') d.fileno # => 8
This method uses the dirfd() function defined by POSIX 2008; the method is not implemented on non-POSIX platforms (raises NotImplementedError
).
Removes the directory at dirpath
from the underlying file system:
Dir.rmdir('foo') # => 0
Raises an exception if the directory is not empty.
Deletes the named files, returning the number of names passed as arguments. Raises an exception on any error. Since the underlying implementation relies on the unlink(2)
system call, the type of exception raised depends on its error type (see linux.die.net/man/2/unlink) and has the form of e.g. Errno::ENOENT.
See also Dir::rmdir
.
Returns true
if the named file is readable by the effective user and group id of this process. See eaccess(3).
Note that some OS-level security features may cause this to return true even though the file is not readable by the effective user/group.
Returns true
if the named file is writable by the effective user and group id of this process. See eaccess(3).
Note that some OS-level security features may cause this to return true even though the file is not writable by the effective user/group.
Returns true
if the named file is executable by the effective user and group id of this process. See eaccess(3).
Windows does not support execute permissions separately from read permissions. On Windows, a file is only considered executable if it ends in .bat, .cmd, .com, or .exe.
Note that some OS-level security features may cause this to return true even though the file is not executable by the effective user/group.
Returns true
if the named file
exists and is a regular file.
file
can be an IO
object.
If the file
argument is a symbolic link, it will resolve the symbolic link and use the file referenced by the link.
Returns true for dummy encodings. A dummy encoding is an encoding for which character handling is not properly implemented. It is used for stateful encodings.
Encoding::ISO_2022_JP.dummy? #=> true Encoding::UTF_8.dummy? #=> false