Generate a string that randomly draws from a source array of characters.
The argument source specifies the array of characters from which to generate the string. The argument n specifies the length, in characters, of the string to be generated.
The result may contain whatever characters are in the source array.
require 'random/formatter' prng.choose([*'l'..'r'], 16) #=> "lmrqpoonmmlqlron" prng.choose([*'0'..'9'], 5) #=> "27309"
Simple deprecation method that deprecates name
by wrapping it up in a dummy method. It warns on each call to the dummy method telling the user of repl
(unless repl
is :none) and the year/month that it is planned to go away.
A Zlib::Inflate#inflate
wrapper
With a block given, iterates over the elements of self
, passing each array index to the block; returns self
:
a = [:foo, 'bar', 2] a.each_index {|index| puts "#{index} #{a[index]}" }
Output:
0 foo 1 bar 2 2
Allows the array to be modified during iteration:
a = [:foo, 'bar', 2] a.each_index {|index| puts index; a.clear if index > 0 } a # => []
Output:
0 1
With no block given, returns a new Enumerator
.
Related: see Methods for Iterating.
When a block given, iterates backwards over the elements of self
, passing, in reverse order, each element to the block; returns self
:
a = [] [0, 1, 2].reverse_each {|element| a.push(element) } a # => [2, 1, 0]
Allows the array to be modified during iteration:
a = ['a', 'b', 'c'] a.reverse_each {|element| a.clear if element.start_with?('b') } a # => []
When no block given, returns a new Enumerator
.
Related: see Methods for Iterating.
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.
Removes the element of self
at the given index
, which must be an integer-convertible object.
When index
is non-negative, deletes the element at offset index
:
a = [:foo, 'bar', 2] a.delete_at(1) # => "bar" a # => [:foo, 2]
When index
is negative, counts backward from the end of the array:
a = [:foo, 'bar', 2] a.delete_at(-2) # => "bar" a # => [:foo, 2]
When index
is out of range, returns nil
.
a = [:foo, 'bar', 2] a.delete_at(3) # => nil a.delete_at(-4) # => nil
Related: see Methods for Deleting.
Returns the integer index of the element from self
found by a binary search, or nil
if the search found no suitable element.
See Binary Searching.
Related: see Methods for Fetching.
With a block given, forms the substrings (“lines”) that are the result of splitting self
at each occurrence of the given line separator line_sep
; passes each line to the block; returns self
:
s = <<~EOT This is the first line. This is line two. This is line four. This is line five. EOT s.each_line {|line| p line }
Output:
"This is the first line.\n" "This is line two.\n" "\n" "This is line four.\n" "This is line five.\n"
With a different line_sep
:
s.each_line(' is ') {|line| p line }
Output:
"This is " "the first line.\nThis is " "line two.\n\nThis is " "line four.\nThis is " "line five.\n"
With chomp
as true
, removes the trailing line_sep
from each line:
s.each_line(chomp: true) {|line| p line }
Output:
"This is the first line." "This is line two." "" "This is line four." "This is line five."
With an empty string as line_sep
, forms and passes “paragraphs” by splitting at each occurrence of two or more newlines:
s.each_line('') {|line| p line }
Output:
"This is the first line.\nThis is line two.\n\n" "This is line four.\nThis is line five.\n"
With no block given, returns an enumerator.
Calls the given block with each successive byte from self
; returns self
:
'hello'.each_byte {|byte| print byte, ' ' } print "\n" 'тест'.each_byte {|byte| print byte, ' ' } print "\n" 'こんにちは'.each_byte {|byte| print byte, ' ' } print "\n"
Output:
104 101 108 108 111 209 130 208 181 209 129 209 130 227 129 147 227 130 147 227 129 171 227 129 161 227 129 175
Returns an enumerator if no block is given.
Calls the given block with each successive codepoint from self
; each codepoint is the integer value for a character; returns self
:
'hello'.each_codepoint {|codepoint| print codepoint, ' ' } print "\n" 'тест'.each_codepoint {|codepoint| print codepoint, ' ' } print "\n" 'こんにちは'.each_codepoint {|codepoint| print codepoint, ' ' } print "\n"
Output:
104 101 108 108 111 1090 1077 1089 1090 12371 12435 12395 12385 12399
Returns an enumerator if no block is given.
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(Encoding::ISO_8859_1) s.unicode_normalized? # Raises Encoding::CompatibilityError.
Related: String#unicode_normalize
, String#unicode_normalize!
.
Returns the next-larger representable Float.
These examples show the internally stored values (64-bit hexadecimal) for each Float f
and for the corresponding f.next_float
:
f = 0.0 # 0x0000000000000000 f.next_float # 0x0000000000000001 f = 0.01 # 0x3f847ae147ae147b f.next_float # 0x3f847ae147ae147c
In the remaining examples here, the output is shown in the usual way (result to_s
):
0.01.next_float # => 0.010000000000000002 1.0.next_float # => 1.0000000000000002 100.0.next_float # => 100.00000000000001 f = 0.01 (0..3).each_with_index {|i| printf "%2d %-20a %s\n", i, f, f.to_s; f = f.next_float }
Output:
0 0x1.47ae147ae147bp-7 0.01 1 0x1.47ae147ae147cp-7 0.010000000000000002 2 0x1.47ae147ae147dp-7 0.010000000000000004 3 0x1.47ae147ae147ep-7 0.010000000000000005 f = 0.0; 100.times { f += 0.1 } f # => 9.99999999999998 # should be 10.0 in the ideal world. 10-f # => 1.9539925233402755e-14 # the floating point error. 10.0.next_float-10 # => 1.7763568394002505e-15 # 1 ulp (unit in the last place). (10-f)/(10.0.next_float-10) # => 11.0 # the error is 11 ulp. (10-f)/(10*Float::EPSILON) # => 8.8 # approximation of the above. "%a" % 10 # => "0x1.4p+3" "%a" % f # => "0x1.3fffffffffff5p+3" # the last hex digit is 5. 16 - 5 = 11 ulp.
Related: Float#prev_float
Returns the next-smaller representable Float.
These examples show the internally stored values (64-bit hexadecimal) for each Float f
and for the corresponding f.pev_float
:
f = 5e-324 # 0x0000000000000001 f.prev_float # 0x0000000000000000 f = 0.01 # 0x3f847ae147ae147b f.prev_float # 0x3f847ae147ae147a
In the remaining examples here, the output is shown in the usual way (result to_s
):
0.01.prev_float # => 0.009999999999999998 1.0.prev_float # => 0.9999999999999999 100.0.prev_float # => 99.99999999999999 f = 0.01 (0..3).each_with_index {|i| printf "%2d %-20a %s\n", i, f, f.to_s; f = f.prev_float }
Output:
0 0x1.47ae147ae147bp-7 0.01 1 0x1.47ae147ae147ap-7 0.009999999999999998 2 0x1.47ae147ae1479p-7 0.009999999999999997 3 0x1.47ae147ae1478p-7 0.009999999999999995
Related: Float#next_float
.
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
Sets the Fiber
scheduler for the current thread. If the scheduler is set, non-blocking fibers (created by Fiber.new
with blocking: false
, or by Fiber.schedule
) call that scheduler’s hook methods on potentially blocking operations, and the current thread will call scheduler’s close
method on finalization (allowing the scheduler to properly manage all non-finished fibers).
scheduler
can be an object of any class corresponding to Fiber::Scheduler
. Its implementation is up to the user.
See also the “Non-blocking fibers” section in class docs.
Returns the Fiber
scheduler, that was last set for the current thread with Fiber.set_scheduler
if and only if the current fiber is non-blocking.
Returns the dirpath
string that was used to create self
(or nil
if created by method Dir.for_fd
):
Dir.new('example').path # => "example"
Converts a pathname to an absolute pathname. Relative paths are referenced from the current working directory of the process unless dir_string
is given, in which case it will be used as the starting point. The given pathname may start with a “~
”, which expands to the process owner’s home directory (the environment variable HOME
must be set correctly). “~
user” expands to the named user’s home directory.
File.expand_path("~oracle/bin") #=> "/home/oracle/bin"
A simple example of using dir_string
is as follows.
File.expand_path("ruby", "/usr/bin") #=> "/usr/bin/ruby"
A more complex example which also resolves parent directory is as follows. Suppose we are in bin/mygem and want the absolute path of lib/mygem.rb.
File.expand_path("../../lib/mygem.rb", __FILE__) #=> ".../path/to/project/lib/mygem.rb"
So first it resolves the parent of __FILE__, that is bin/, then go to the parent, the root of the project and appends lib/mygem.rb
.
Converts a pathname to an absolute pathname. Relative paths are referenced from the current working directory of the process unless dir_string is given, in which case it will be used as the starting point. If the given pathname starts with a “~
” it is NOT expanded, it is treated as a normal directory name.
File.absolute_path("~oracle/bin") #=> "<relative_path>/~oracle/bin"
Returns true
if file_name
is an absolute path, and false
otherwise.
File.absolute_path?("c:/foo") #=> false (on Linux), true (on Windows)
Returns whether ASCII-compatible or not.
Encoding::UTF_8.ascii_compatible? #=> true Encoding::UTF_16BE.ascii_compatible? #=> false