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.
Iterates the given block for each element with an index, which starts from offset
. If no block is given, returns a new Enumerator
that includes the index, starting from offset
offset
the starting index to use
Iterates the given block for each element with an arbitrary object, obj
, and returns obj
If no block is given, returns a new Enumerator
.
to_three = Enumerator.new do |y| 3.times do |x| y << x end end to_three_with_string = to_three.with_object("foo") to_three_with_string.each do |x,string| puts "#{string}: #{x}" end # => foo: 0 # => foo: 1 # => foo: 2
Convert an object to YAML
. See Psych.dump
for more information on the available options
.
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 true if the caused method was called as private.
When this module is included in another, Ruby calls append_features
in this module, passing it the receiving module in mod. Ruby’s default implementation is to add the constants, methods, and module variables of this module to mod if this module has not already been added to mod or one of its ancestors. See also Module#include
.
When this module is prepended in another, Ruby calls prepend_features
in this module, passing it the receiving module in mod. Ruby’s default implementation is to overlay the constants, methods, and module variables of this module to mod if this module has not already been added to mod or one of its ancestors. See also Module#prepend
.
Returns an array of all modules used in the current scope. The ordering of modules in the resulting array is not defined.
module A refine Object do end end module B refine Object do end end using A using B p Module.used_refinements
produces:
[#<refinement:Object@B>, #<refinement:Object@A>]
Creates instance variables and corresponding methods that return the value of each instance variable. Equivalent to calling “attr
:name” on each name in turn. String
arguments are converted to symbols. Returns an array of defined method names as symbols.
Creates an accessor method to allow assignment to the attribute symbol.id2name
. String
arguments are converted to symbols. Returns an array of defined method names as symbols.
Defines a named attribute for this module, where the name is symbol.id2name
, creating an instance variable (@name
) and a corresponding access method to read it. Also creates a method called name=
to set the attribute. String
arguments are converted to symbols. Returns an array of defined method names as symbols.
module Mod attr_accessor(:one, :two) #=> [:one, :one=, :two, :two=] end Mod.instance_methods.sort #=> [:one, :one=, :two, :two=]
Makes a list of existing constants private.
Makes a list of existing constants deprecated. Attempt to refer to them will produce a warning.
module HTTP NotFound = Exception.new NOT_FOUND = NotFound # previous version of the library used this name deprecate_constant :NOT_FOUND end HTTP::NOT_FOUND # warning: constant HTTP::NOT_FOUND is deprecated
Returns true
if the arguments define a valid ordinal date, false
otherwise:
Date.valid_ordinal?(2001, 34) # => true Date.valid_ordinal?(2001, 366) # => false
See argument start.
Related: Date.jd
, Date.ordinal
.
Returns true
if the arguments define a valid ordinal date, false
otherwise:
Date.valid_date?(2001, 2, 3) # => true Date.valid_date?(2001, 2, 29) # => false Date.valid_date?(2001, 2, -1) # => true
See argument start.
Returns true
if the arguments define a valid commercial date, false
otherwise:
Date.valid_commercial?(2001, 5, 6) # => true Date.valid_commercial?(2001, 5, 8) # => false
See Date.commercial
.
See argument start.
Related: Date.jd
, Date.commercial
.
Equivalent to >>
with argument n
.
Equivalent to <<
with argument n
.
Returns a new Time
object with the same value as self
; if self
is a Julian date, derives its Gregorian date for conversion to the Time object:
Date.new(2001, 2, 3).to_time # => 2001-02-03 00:00:00 -0600 Date.new(2001, 2, 3, Date::JULIAN).to_time # => 2001-02-16 00:00:00 -0600