Replaces the elements of self
with the elements of other_array
, which must be an array-convertible object; returns self
:
a = ['a', 'b', 'c'] # => ["a", "b", "c"] a.replace(['d', 'e']) # => ["d", "e"]
Related: see Methods for Assigning.
Returns elements from self
in a new array; does not modify self
.
The objects included in the returned array are the elements of self
selected by the given specifiers
, each of which must be a numeric index or a Range
.
In brief:
a = ['a', 'b', 'c', 'd'] # Index specifiers. a.values_at(2, 0, 2, 0) # => ["c", "a", "c", "a"] # May repeat. a.values_at(-4, -3, -2, -1) # => ["a", "b", "c", "d"] # Counts backwards if negative. a.values_at(-50, 50) # => [nil, nil] # Outside of self. # Range specifiers. a.values_at(1..3) # => ["b", "c", "d"] # From range.begin to range.end. a.values_at(1...3) # => ["b", "c"] # End excluded. a.values_at(3..1) # => [] # No such elements. a.values_at(-3..3) # => ["b", "c", "d"] # Negative range.begin counts backwards. a.values_at(-50..3) # Raises RangeError. a.values_at(1..-2) # => ["b", "c"] # Negative range.end counts backwards. a.values_at(1..-50) # => [] # No such elements. # Mixture of specifiers. a.values_at(2..3, 3, 0..1, 0) # => ["c", "d", "d", "a", "b", "a"]
With no specifiers
given, returns a new empty array:
a = ['a', 'b', 'c', 'd'] a.values_at # => []
For each numeric specifier index
, includes an element:
For each non-negative numeric specifier index
that is in-range (less than self.size
), includes the element at offset index
:
a.values_at(0, 2) # => ["a", "c"] a.values_at(0.1, 2.9) # => ["a", "c"]
For each negative numeric index
that is in-range (greater than or equal to - self.size
), counts backwards from the end of self
:
a.values_at(-1, -4) # => ["d", "a"]
The given indexes may be in any order, and may repeat:
a.values_at(2, 0, 1, 0, 2) # => ["c", "a", "b", "a", "c"]
For each index
that is out-of-range, includes nil
:
a.values_at(4, -5) # => [nil, nil]
For each Range
specifier range
, includes elements according to range.begin
and range.end
:
If both range.begin
and range.end
are non-negative and in-range (less than self.size
), includes elements from index range.begin
through range.end - 1
(if range.exclude_end?
), or through range.end
(otherwise):
a.values_at(1..2) # => ["b", "c"] a.values_at(1...2) # => ["b"]
If range.begin
is negative and in-range (greater than or equal to - self.size
), counts backwards from the end of self
:
a.values_at(-2..3) # => ["c", "d"]
If range.begin
is negative and out-of-range, raises an exception:
a.values_at(-5..3) # Raises RangeError.
If range.end
is positive and out-of-range, extends the returned array with nil
elements:
a.values_at(1..5) # => ["b", "c", "d", nil, nil]
If range.end
is negative and in-range, counts backwards from the end of self
:
a.values_at(1..-2) # => ["b", "c"]
If range.end
is negative and out-of-range, returns an empty array:
a.values_at(1..-5) # => []
The given ranges may be in any order and may repeat:
a.values_at(2..3, 0..1, 2..3) # => ["c", "d", "a", "b", "c", "d"]
The given specifiers may be any mixture of indexes and ranges:
a.values_at(3, 1..2, 0, 2..3) # => ["d", "b", "c", "a", "c", "d"]
Related: see Methods for Fetching.
With a block given, calls the block with each repeated permutation of length size
of the elements of self
; each permutation is an array; returns self
. The order of the permutations is indeterminate.
If a positive integer argument size
is given, calls the block with each size
-tuple repeated permutation of the elements of self
. The number of permutations is self.size**size
.
Examples:
size
is 1:
p = [] [0, 1, 2].repeated_permutation(1) {|permutation| p.push(permutation) } p # => [[0], [1], [2]]
size
is 2:
p = [] [0, 1, 2].repeated_permutation(2) {|permutation| p.push(permutation) } p # => [[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2], [2, 0], [2, 1], [2, 2]]
If size
is zero, calls the block once with an empty array.
If size
is negative, does not call the block:
[0, 1, 2].repeated_permutation(-1) {|permutation| fail 'Cannot happen' }
With no block given, returns a new Enumerator
.
Related: see Methods for Combining.
With a block given, calls the block with each successive element of self
; stops iterating if the block returns false
or nil
; returns a new array containing those elements for which the block returned a truthy value:
a = [0, 1, 2, 3, 4, 5] a.take_while {|element| element < 3 } # => [0, 1, 2] a.take_while {|element| true } # => [0, 1, 2, 3, 4, 5] a.take_while {|element| false } # => []
With no block given, returns a new Enumerator
.
Does not modify self
.
Related: see Methods for Fetching.
With no block given, returns a new array containing the elements of self
at the offsets specified by indexes
. Each of the indexes
must be an integer-convertible object:
a = [:foo, :bar, :baz] a.fetch_values(2, 0) # => [:baz, :foo] a.fetch_values(2.1, 0) # => [:baz, :foo] a.fetch_values # => []
For a negative index, counts backwards from the end of the array:
a.fetch_values(-2, -1) # [:bar, :baz]
When no block is given, raises an exception if any index is out of range.
With a block given, for each index:
If the index is in range, uses an element of self
(as above).
Otherwise, calls the block with the index and uses the block’s return value.
Example:
a = [:foo, :bar, :baz] a.fetch_values(1, 0, 42, 777) { |index| index.to_s } # => [:bar, :foo, "42", "777"]
Related: see Methods for Fetching.
Replaces the contents of self
with the contents of other_string
:
s = 'foo' # => "foo" s.replace('bar') # => "bar"
Returns whether self
starts with any of the given string_or_regexp
.
Matches patterns against the beginning of self
. For each given string_or_regexp
, the pattern is:
string_or_regexp
itself, if it is a Regexp
.
Regexp.quote(string_or_regexp)
, if string_or_regexp
is a string.
Returns true
if any pattern matches the beginning, false
otherwise:
'hello'.start_with?('hell') # => true 'hello'.start_with?(/H/i) # => true 'hello'.start_with?('heaven', 'hell') # => true 'hello'.start_with?('heaven', 'paradise') # => false 'тест'.start_with?('т') # => true 'こんにちは'.start_with?('こ') # => true
Related: String#end_with?
.
Returns true
if self
is encoded correctly, false
otherwise:
"\xc2\xa1".force_encoding("UTF-8").valid_encoding? # => true "\xc2".force_encoding("UTF-8").valid_encoding? # => false "\x80".force_encoding("UTF-8").valid_encoding? # => false
Returns true
if self
contains only ASCII characters, false
otherwise:
'abc'.ascii_only? # => true "abc\u{6666}".ascii_only? # => false
Returns a copy of self
with Unicode normalization applied.
Argument form
must be one of the following symbols (see Unicode normalization forms):
:nfc
: Canonical decomposition, followed by canonical composition.
:nfd
: Canonical decomposition.
:nfkc
: Compatibility decomposition, followed by canonical composition.
:nfkd
: Compatibility decomposition.
The encoding of self
must be one of:
Encoding::UTF_8
Encoding::UTF_16BE
Encoding::UTF_16LE
Encoding::UTF_32BE
Encoding::UTF_32LE
Encoding::GB18030
Encoding::UCS_2BE
Encoding::UCS_4BE
Examples:
"a\u0300".unicode_normalize # => "a" "\u00E0".unicode_normalize(:nfd) # => "a "
Related: String#unicode_normalize!
, String#unicode_normalized?
.
Like String#unicode_normalize
, except that the normalization is performed on self
.
Related String#unicode_normalized?
.
Returns true
if self
is in the given form
of Unicode normalization, false
otherwise. The form
must be one of :nfc
, :nfd
, :nfkc
, or :nfkd
.
Examples:
"a\u0300".unicode_normalized? # => false "a\u0300".unicode_normalized?(:nfd) # => true "\u00E0".unicode_normalized? # => true "\u00E0".unicode_normalized?(:nfd) # => false
Raises an exception if self
is not in a Unicode encoding:
s = "\xE0".force_encoding('ISO-8859-1') s.unicode_normalized? # Raises Encoding::CompatibilityError.
Related: String#unicode_normalize
, String#unicode_normalize!
.
Returns true
if the named file is readable by the real user and group id of this process. See access(3).
Note that some OS-level security features may cause this to return true even though the file is not readable by the real user/group.
If file_name is writable by others, returns an integer representing the file permission bits of file_name. Returns nil
otherwise. The meaning of the bits is platform dependent; on Unix systems, see stat(2)
.
file_name can be an IO
object.
File.world_writable?("/tmp") #=> 511 m = File.world_writable?("/tmp") sprintf("%o", m) #=> "777"
Returns default external encoding.
The default external encoding is used by default for strings created from the following locations:
CSV
File
data read from disk
SDBM
While strings created from these locations will have this encoding, the encoding may not be valid. Be sure to check String#valid_encoding?
.
File
data written to disk will be transcoded to the default external encoding when written, if default_internal
is not nil.
The default external encoding is initialized by the -E option. If -E isn’t set, it is initialized to UTF-8 on Windows and the locale on other operating systems.
Sets default external encoding. You should not set Encoding::default_external
in ruby code as strings created before changing the value may have a different encoding from strings created after the value was changed., instead you should use ruby -E
to invoke ruby with the correct default_external.
See Encoding::default_external
for information on how the default external encoding is used.
Returns default internal encoding. Strings will be transcoded to the default internal encoding in the following places if the default internal encoding is not nil:
CSV
File
data read from disk
Strings returned from Readline
Strings returned from SDBM
Values from ENV
Values in ARGV including $PROGRAM_NAME
Additionally String#encode
and String#encode!
use the default internal encoding if no encoding is given.
The script encoding (__ENCODING__), not default_internal
, is used as the encoding of created strings.
Encoding::default_internal
is initialized with -E option or nil otherwise.
Sets default internal encoding or removes default internal encoding when passed nil. You should not set Encoding::default_internal
in ruby code as strings created before changing the value may have a different encoding from strings created after the change. Instead you should use ruby -E
to invoke ruby with the correct default_internal.
See Encoding::default_internal
for information on how the default internal encoding is used.
Returns the locale charmap name. It returns nil if no appropriate information.
Debian GNU/Linux LANG=C Encoding.locale_charmap #=> "ANSI_X3.4-1968" LANG=ja_JP.EUC-JP Encoding.locale_charmap #=> "EUC-JP" SunOS 5 LANG=C Encoding.locale_charmap #=> "646" LANG=ja Encoding.locale_charmap #=> "eucJP"
The result is highly platform dependent. So Encoding.find(Encoding.locale_charmap)
may cause an error. If you need some encoding object even for unknown locale, Encoding.find
(“locale”) can be used.