Returns the effective group ID for the current process:
Process.egid # => 500
Not available on all platforms.
Attempts to return an array, based on the given object
.
If object
is an array, returns object
.
Otherwise if object
responds to :to_ary
. calls object.to_ary
: if the return value is an array or nil
, returns that value; if not, raises TypeError
.
Otherwise returns nil
.
Iterates backwards over array elements.
When a block given, passes, in reverse order, each element to the block; returns self
:
a = [:foo, 'bar', 2] a.reverse_each {|element| puts "#{element.class} #{element}" }
Output:
Integer 2 String bar Symbol foo
Allows the array to be modified during iteration:
a = [:foo, 'bar', 2] a.reverse_each {|element| puts element; a.clear if element.to_s.start_with?('b') }
Output:
2 bar
When no block given, returns a new Enumerator:
a = [:foo, 'bar', 2] e = a.reverse_each e # => #<Enumerator: [:foo, "bar", 2]:reverse_each> a1 = e.each {|element| puts "#{element.class} #{element}" }
Output:
Integer 2 String bar Symbol foo
Related: each
, each_index
.
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]]
If object
is an Integer object, returns object
.
Integer.try_convert(1) # => 1
Otherwise if object
responds to :to_int
, calls object.to_int
and returns the result.
Integer.try_convert(1.25) # => 1
Returns nil
if object
does not respond to :to_int
Integer.try_convert([]) # => nil
Raises an exception unless object.to_int
returns an Integer object.
If object
is a String
object, returns object
.
Otherwise if object
responds to :to_str
, calls object.to_str
and returns the result.
Returns nil
if object
does not respond to :to_str
.
Raises an exception unless object.to_str
returns a String
object.
Returns an array of the grapheme clusters in self
(see Unicode Grapheme Cluster Boundaries):
s = "\u0061\u0308-pqr-\u0062\u0308-xyz-\u0063\u0308" # => "ä-pqr-b̈-xyz-c̈" s.grapheme_clusters # => ["ä", "-", "p", "q", "r", "-", "b̈", "-", "x", "y", "z", "-", "c̈"]
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 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.
Returns the message string with enhancements:
Includes the exception class name in the first line.
If the value of keyword highlight
is true
, includes bolding and underlining ANSI codes (see below) to enhance the appearance of the message.
Examples:
begin 1 / 0 rescue => x p x.message p x.detailed_message # Class name added. p x.detailed_message(highlight: true) # Class name, bolding, and underlining added. end
Output:
"divided by 0" "divided by 0 (ZeroDivisionError)" "\e[1mdivided by 0 (\e[1;4mZeroDivisionError\e[m\e[1m)\e[m"
This method is overridden by some gems in the Ruby standard library to add information:
An overriding method must be tolerant of passed keyword arguments, which may include (but may not be limited to):
:highlight
.
:did_you_mean
.
:error_highlight
.
:syntax_suggest
.
An overrriding method should also be careful with ANSI code enhancements; see Messages.
Returns an enhanced message string:
Includes the exception class name.
If the value of keyword highlight
is true (not nil
or false
), includes bolding ANSI codes (see below) to enhance the appearance of the message.
Includes the backtrace:
If the value of keyword order
is :top
(the default), lists the error message and the innermost backtrace entry first.
If the value of keyword order
is :bottom
, lists the error message the the innermost entry last.
Example:
def baz begin 1 / 0 rescue => x pp x.message pp x.full_message(highlight: false).split("\n") pp x.full_message.split("\n") end end def bar; baz; end def foo; bar; end foo
Output:
"divided by 0" ["t.rb:3:in `/': divided by 0 (ZeroDivisionError)", "\tfrom t.rb:3:in `baz'", "\tfrom t.rb:10:in `bar'", "\tfrom t.rb:11:in `foo'", "\tfrom t.rb:12:in `<main>'"] ["t.rb:3:in `/': \e[1mdivided by 0 (\e[1;4mZeroDivisionError\e[m\e[1m)\e[m", "\tfrom t.rb:3:in `baz'", "\tfrom t.rb:10:in `bar'", "\tfrom t.rb:11:in `foo'", "\tfrom t.rb:12:in `<main>'"]
An overrriding method should be careful with ANSI code enhancements; see backtrace.
Returns a backtrace value for self
; the returned value depends on the form of the stored backtrace value:
Array of Thread::Backtrace::Location
objects: returns that array.
Array of strings or nil
: returns nil
.
Example:
begin 1 / 0 rescue => x x.backtrace_locations.take(2) end # => ["(irb):150:in `/'", "(irb):150:in `<top (required)>'"]
See Backtraces.
Return a list of the local variable names defined where this NameError
exception was raised.
Internal use only.