Convert an object to YAML
. See Psych.dump
for more information on the available options
.
Returns the number of bits of the value of int
.
“Number of bits” means the bit position of the highest bit which is different from the sign bit (where the least significant bit has bit position 1). If there is no such bit (zero or minus one), zero is returned.
I.e. this method returns ceil(log2(int < 0 ? -int : int+1)).
(-2**1000-1).bit_length #=> 1001 (-2**1000).bit_length #=> 1000 (-2**1000+1).bit_length #=> 1000 (-2**12-1).bit_length #=> 13 (-2**12).bit_length #=> 12 (-2**12+1).bit_length #=> 12 -0x101.bit_length #=> 9 -0x100.bit_length #=> 8 -0xff.bit_length #=> 8 -2.bit_length #=> 1 -1.bit_length #=> 0 0.bit_length #=> 0 1.bit_length #=> 1 0xff.bit_length #=> 8 0x100.bit_length #=> 9 (2**12-1).bit_length #=> 12 (2**12).bit_length #=> 13 (2**12+1).bit_length #=> 13 (2**1000-1).bit_length #=> 1000 (2**1000).bit_length #=> 1001 (2**1000+1).bit_length #=> 1001
This method can be used to detect overflow in Array#pack
as follows:
if n.bit_length < 32 [n].pack("l") # no overflow else raise "overflow" end
Returns the factorization of self
.
See Prime#prime_division
for more details.
Iterates the given block over all prime numbers.
See Prime
#each for more details.
Returns an array of grapheme clusters in str. This is a shorthand for str.each_grapheme_cluster.to_a
.
If a block is given, which is a deprecated form, works the same as each_grapheme_cluster
.
Returns true if str
starts with one of the prefixes
given. Each of the prefixes
should be a String
or a Regexp
.
"hello".start_with?("hell") #=> true "hello".start_with?(/H/i) #=> true # returns true if one of the prefixes matches. "hello".start_with?("heaven", "hell") #=> true "hello".start_with?("heaven", "paradise") #=> false
Returns true if str
ends with one of the suffixes
given.
"hello".end_with?("ello") #=> true # returns true if one of the +suffixes+ matches. "hello".end_with?("heaven", "ello") #=> true "hello".end_with?("heaven", "paradise") #=> false
Returns the next representable floating point number.
Float::MAX.next_float and Float::INFINITY.next_float is Float::INFINITY
.
Float::NAN.next_float is Float::NAN
.
For example:
0.01.next_float #=> 0.010000000000000002 1.0.next_float #=> 1.0000000000000002 100.0.next_float #=> 100.00000000000001 0.01.next_float - 0.01 #=> 1.734723475976807e-18 1.0.next_float - 1.0 #=> 2.220446049250313e-16 100.0.next_float - 100.0 #=> 1.4210854715202004e-14 f = 0.01; 20.times { printf "%-20a %s\n", f, f.to_s; f = f.next_float } #=> 0x1.47ae147ae147bp-7 0.01 # 0x1.47ae147ae147cp-7 0.010000000000000002 # 0x1.47ae147ae147dp-7 0.010000000000000004 # 0x1.47ae147ae147ep-7 0.010000000000000005 # 0x1.47ae147ae147fp-7 0.010000000000000007 # 0x1.47ae147ae148p-7 0.010000000000000009 # 0x1.47ae147ae1481p-7 0.01000000000000001 # 0x1.47ae147ae1482p-7 0.010000000000000012 # 0x1.47ae147ae1483p-7 0.010000000000000014 # 0x1.47ae147ae1484p-7 0.010000000000000016 # 0x1.47ae147ae1485p-7 0.010000000000000018 # 0x1.47ae147ae1486p-7 0.01000000000000002 # 0x1.47ae147ae1487p-7 0.010000000000000021 # 0x1.47ae147ae1488p-7 0.010000000000000023 # 0x1.47ae147ae1489p-7 0.010000000000000024 # 0x1.47ae147ae148ap-7 0.010000000000000026 # 0x1.47ae147ae148bp-7 0.010000000000000028 # 0x1.47ae147ae148cp-7 0.01000000000000003 # 0x1.47ae147ae148dp-7 0.010000000000000031 # 0x1.47ae147ae148ep-7 0.010000000000000033 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.
Returns the previous representable floating point number.
(-Float::MAX).prev_float and (-Float::INFINITY).prev_float is -Float::INFINITY.
Float::NAN.prev_float is Float::NAN
.
For example:
0.01.prev_float #=> 0.009999999999999998 1.0.prev_float #=> 0.9999999999999999 100.0.prev_float #=> 99.99999999999999 0.01 - 0.01.prev_float #=> 1.734723475976807e-18 1.0 - 1.0.prev_float #=> 1.1102230246251565e-16 100.0 - 100.0.prev_float #=> 1.4210854715202004e-14 f = 0.01; 20.times { printf "%-20a %s\n", f, f.to_s; f = f.prev_float } #=> 0x1.47ae147ae147bp-7 0.01 # 0x1.47ae147ae147ap-7 0.009999999999999998 # 0x1.47ae147ae1479p-7 0.009999999999999997 # 0x1.47ae147ae1478p-7 0.009999999999999995 # 0x1.47ae147ae1477p-7 0.009999999999999993 # 0x1.47ae147ae1476p-7 0.009999999999999992 # 0x1.47ae147ae1475p-7 0.00999999999999999 # 0x1.47ae147ae1474p-7 0.009999999999999988 # 0x1.47ae147ae1473p-7 0.009999999999999986 # 0x1.47ae147ae1472p-7 0.009999999999999985 # 0x1.47ae147ae1471p-7 0.009999999999999983 # 0x1.47ae147ae147p-7 0.009999999999999981 # 0x1.47ae147ae146fp-7 0.00999999999999998 # 0x1.47ae147ae146ep-7 0.009999999999999978 # 0x1.47ae147ae146dp-7 0.009999999999999976 # 0x1.47ae147ae146cp-7 0.009999999999999974 # 0x1.47ae147ae146bp-7 0.009999999999999972 # 0x1.47ae147ae146ap-7 0.00999999999999997 # 0x1.47ae147ae1469p-7 0.009999999999999969 # 0x1.47ae147ae1468p-7 0.009999999999999967
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
Returns default external encoding.
The default external encoding is used by default for strings created from the following locations:
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:
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
Returns formatted string of exception. The returned string is formatted using the same format that Ruby uses when printing an uncaught exceptions to stderr.
If highlight is true
the default error handler will send the messages to a tty.
order must be either of :top
or :bottom
, and places the error message and the innermost backtrace come at the top or the bottom.
The default values of these options depend on $stderr
and its tty?
at the timing of a call.
Returns any backtrace associated with the exception. This method is similar to Exception#backtrace
, but the backtrace is an array of Thread::Backtrace::Location
.
This method is not affected by Exception#set_backtrace()
.
Deserializes JSON
string by constructing new Exception
object with message m
and backtrace b
serialized with to_json
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
.
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.